2018-02-10 01:00:55 +01:00
|
|
|
use rocket_contrib::{Json, Value};
|
|
|
|
|
|
|
|
use db::DbConn;
|
|
|
|
use db::models::*;
|
|
|
|
|
2018-02-17 20:47:13 +01:00
|
|
|
use api::{JsonResult, EmptyResult};
|
2018-02-10 01:00:55 +01:00
|
|
|
use auth::Headers;
|
|
|
|
|
|
|
|
#[get("/folders")]
|
2018-02-17 20:47:13 +01:00
|
|
|
fn get_folders(headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-10 01:00:55 +01:00
|
|
|
let folders = Folder::find_by_user(&headers.user.uuid, &conn);
|
|
|
|
|
|
|
|
let folders_json: Vec<Value> = folders.iter().map(|c| c.to_json()).collect();
|
|
|
|
|
|
|
|
Ok(Json(json!({
|
|
|
|
"Data": folders_json,
|
|
|
|
"Object": "list",
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/folders/<uuid>")]
|
2018-02-17 20:47:13 +01:00
|
|
|
fn get_folder(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-15 00:53:11 +01:00
|
|
|
let folder = match Folder::find_by_uuid(&uuid, &conn) {
|
2018-02-10 01:00:55 +01:00
|
|
|
Some(folder) => folder,
|
|
|
|
_ => err!("Invalid folder")
|
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct FolderData {
|
|
|
|
pub name: String
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
#[post("/folders", data = "<data>")]
|
|
|
|
fn post_folders(data: Json<FolderData>, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
let data: FolderData = data.into_inner();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
let mut folder = Folder::new(headers.user.uuid.clone(), data.name);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
folder.save(&conn);
|
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/folders/<uuid>", data = "<data>")]
|
2018-02-23 00:38:54 +01:00
|
|
|
fn post_folder(uuid: String, data: Json<FolderData>, headers: Headers, conn: DbConn) -> JsonResult {
|
2018-02-10 01:00:55 +01:00
|
|
|
put_folder(uuid, data, headers, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/folders/<uuid>", data = "<data>")]
|
2018-02-23 00:38:54 +01:00
|
|
|
fn put_folder(uuid: String, data: Json<FolderData>, headers: Headers, conn: DbConn) -> JsonResult {
|
|
|
|
let data: FolderData = data.into_inner();
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
let mut folder = match Folder::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(folder) => folder,
|
|
|
|
_ => err!("Invalid folder")
|
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
folder.name = data.name;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
folder.save(&conn);
|
|
|
|
|
|
|
|
Ok(Json(folder.to_json()))
|
|
|
|
}
|
|
|
|
|
2018-02-17 23:38:55 +01:00
|
|
|
#[post("/folders/<uuid>/delete")]
|
|
|
|
fn delete_folder_post(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult {
|
2018-02-10 01:00:55 +01:00
|
|
|
delete_folder(uuid, headers, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/folders/<uuid>")]
|
2018-02-17 20:47:13 +01:00
|
|
|
fn delete_folder(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult {
|
2018-02-10 01:00:55 +01:00
|
|
|
let folder = match Folder::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(folder) => folder,
|
|
|
|
_ => err!("Invalid folder")
|
|
|
|
};
|
|
|
|
|
|
|
|
if folder.user_uuid != headers.user.uuid {
|
|
|
|
err!("Folder belongs to another user")
|
|
|
|
}
|
|
|
|
|
2018-05-04 19:02:19 +02:00
|
|
|
// Delete the actual folder entry
|
2018-05-16 18:19:52 +02:00
|
|
|
match folder.delete(&conn) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => err!("Failed deleting folder")
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|