From f71975e5a89b031248d848aa553e4810178e7279 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Sat, 6 Jun 2020 19:11:26 -0400 Subject: Implement tag updates. --- src/birka-web.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/birka-web.rs b/src/birka-web.rs index 22ace80..c145625 100644 --- a/src/birka-web.rs +++ b/src/birka-web.rs @@ -37,6 +37,7 @@ use multipart::{MultipartFormData, MultipartFormDataField, MultipartFormDataOpti use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; use rocket::http::ContentType; +use rocket::request::Form; use rocket::{Data, State}; use rocket_contrib::json::Json; use rocket_contrib::serve::StaticFiles; @@ -145,6 +146,23 @@ fn get_post(conn: SiteState, id: i64) -> Json { }) } +#[derive(FromForm)] +struct UpdateTags { + tags: String, +} + +#[post("/posts/", data = "
")] +fn update_post(conn: SiteState, id: i64, form: Form) -> Json { + let tb = &conn.inner().lock().unwrap().tb; + + for tag in form.tags.split(",") { + tb.add_tag(tag).unwrap(); + tb.tag_image(id, tag).unwrap(); + } + + Json(String::from("Updated!")) +} + #[post("/posts", data = "")] fn put_post(conn: SiteState, content_type: &ContentType, data: Data) -> Json { let conn = conn.inner().lock().unwrap(); @@ -246,6 +264,7 @@ fn display_posts(conn: SiteState, tags: String, last: Option) -> Template { fn display_post(conn: SiteState, id: i64) -> Template { #[derive(Serialize)] struct Context { + id: i64, tags: Vec, filename: String, orig_filename: String, @@ -257,6 +276,7 @@ fn display_post(conn: SiteState, id: i64) -> Template { Template::render( "image", Context { + id, tags: tb.tags_for_image(image.id).unwrap(), filename: image_store_filename(image.id, &image.filename), orig_filename: image.filename, @@ -296,7 +316,7 @@ fn main() { .mount("/public", StaticFiles::from("./static/")) .mount("/image", StaticFiles::from(&conn.image_dir)) .mount("/", routes![index, upload, display_posts, display_post]) - .mount("/api", routes![get_posts, get_post, put_post]) + .mount("/api", routes![get_posts, get_post, put_post, update_post]) .attach(Template::fairing()) .manage(Mutex::new(conn)) .launch(); -- cgit v1.3