2018-10-10 18:40:39 +00:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde_json::Value;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::db::models::*;
|
2018-12-30 22:34:31 +00:00
|
|
|
use crate::db::DbConn;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use crate::api::{EmptyResult, JsonResult, JsonUpcase, Notify, UpdateType};
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::auth::Headers;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-10-10 18:40:39 +00:00
|
|
|
use rocket::Route;
|
|
|
|
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
|
|
routes![
|
|
|
|
get_folders,
|
|
|
|
get_folder,
|
|
|
|
post_folders,
|
|
|
|
post_folder,
|
|
|
|
put_folder,
|
|
|
|
delete_folder_post,
|
|
|
|
delete_folder,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
#[get("/folders")]
|
2018-02-17 19:47:13 +00:00
|
|
|
fn get_folders(headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-10 00:00:55 +00:00
|
|
|
let folders = Folder::find_by_user(&headers.user.uuid, &conn);
|
|
|
|
|
2019-02-27 16:21:04 +00:00
|
|
|
let folders_json: Vec<Value> = folders.iter().map(Folder::to_json).collect();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
Ok(Json(json!({
|
|
|
|
"Data": folders_json,
|
|
|
|
"Object": "list",
|
2018-10-01 16:02:58 +00:00
|
|
|
"ContinuationToken": null,
|
2018-02-10 00:00:55 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/folders/<uuid>")]
|
2018-02-17 19:47:13 +00:00
|
|
|
fn get_folder(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-14 23:53:11 +00:00
|
|
|
let folder = match Folder::find_by_uuid(&uuid, &conn) {
|
2018-02-10 00:00:55 +00:00
|
|
|
Some(folder) => folder,
|
2018-12-30 22:34:31 +00:00
|
|
|
_ => err!("Invalid folder"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-02-22 23:38:54 +00:00
|
|
|
#[derive(Deserialize)]
|
2018-05-31 22:18:50 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
2018-02-22 23:38:54 +00:00
|
|
|
pub struct FolderData {
|
2018-12-30 22:34:31 +00:00
|
|
|
pub Name: String,
|
2018-02-22 23:38:54 +00:00
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-02-22 23:38:54 +00:00
|
|
|
#[post("/folders", data = "<data>")]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn post_folders(data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2018-05-31 22:18:50 +00:00
|
|
|
let data: FolderData = data.into_inner().data;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-05-31 22:18:50 +00:00
|
|
|
let mut folder = Folder::new(headers.user.uuid.clone(), data.Name);
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
folder.save(&conn)?;
|
2018-12-30 22:34:31 +00:00
|
|
|
nt.send_folder_update(UpdateType::FolderCreate, &folder);
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/folders/<uuid>", data = "<data>")]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn post_folder(uuid: String, data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
|
|
|
put_folder(uuid, data, headers, conn, nt)
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/folders/<uuid>", data = "<data>")]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn put_folder(uuid: String, data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult {
|
2018-05-31 22:18:50 +00:00
|
|
|
let data: FolderData = data.into_inner().data;
|
2018-02-22 23:38:54 +00:00
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
let mut folder = match Folder::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(folder) => folder,
|
2018-12-30 22:34:31 +00:00
|
|
|
_ => err!("Invalid folder"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
2018-05-31 22:18:50 +00:00
|
|
|
folder.name = data.Name;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
folder.save(&conn)?;
|
2018-12-30 22:34:31 +00:00
|
|
|
nt.send_folder_update(UpdateType::FolderUpdate, &folder);
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:38:55 +00:00
|
|
|
#[post("/folders/<uuid>/delete")]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn delete_folder_post(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
|
|
|
delete_folder(uuid, headers, conn, nt)
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/folders/<uuid>")]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn delete_folder(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult {
|
2018-02-10 00:00:55 +00:00
|
|
|
let folder = match Folder::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(folder) => folder,
|
2018-12-30 22:34:31 +00:00
|
|
|
_ => err!("Invalid folder"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:02:19 +00:00
|
|
|
// Delete the actual folder entry
|
2018-12-19 20:52:53 +00:00
|
|
|
folder.delete(&conn)?;
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
nt.send_folder_update(UpdateType::FolderDelete, &folder);
|
2018-12-19 20:52:53 +00:00
|
|
|
Ok(())
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|