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. --- README.md | 3 +++ src/birka-web.rs | 22 +++++++++++++++++++++- templates/image.hbs | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cf8a74..069f8d9 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,9 @@ GET /api/posts/[id] ImageResult ``` +To add tags to an image, POST a comma-separated list of new tags to the same +endpoint. + To upload an image to the database, post a multipart/form-data encoded message to the following endpoint with the following fields: `tags`, a comma-separated list of tags for the files, and `image`, the file to be uploaded. 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(); diff --git a/templates/image.hbs b/templates/image.hbs index 8c3ceb6..fab65d5 100644 --- a/templates/image.hbs +++ b/templates/image.hbs @@ -29,6 +29,11 @@
  • {{ . }}
  • {{/each}} + + + + + -- cgit v1.3