summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/birka-web.rs22
-rw-r--r--templates/image.hbs5
3 files changed, 29 insertions, 1 deletions
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<ImageResult> {
})
}
+#[derive(FromForm)]
+struct UpdateTags {
+ tags: String,
+}
+
+#[post("/posts/<id>", data = "<form>")]
+fn update_post(conn: SiteState, id: i64, form: Form<UpdateTags>) -> Json<String> {
+ 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 = "<data>")]
fn put_post(conn: SiteState, content_type: &ContentType, data: Data) -> Json<String> {
let conn = conn.inner().lock().unwrap();
@@ -246,6 +264,7 @@ fn display_posts(conn: SiteState, tags: String, last: Option<i64>) -> Template {
fn display_post(conn: SiteState, id: i64) -> Template {
#[derive(Serialize)]
struct Context {
+ id: i64,
tags: Vec<String>,
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 @@
<li><a href="/posts?tags={{ . }}">{{ . }}</a></li>
{{/each}}
</ul>
+
+ <form id="update-prompt" method="POST" action="/api/posts/{{id}}">
+ <input id="tags" name="tags" type="text">
+ <button id="search-box-submit" type="submit">Add Tags</button>
+ </form>
</section>
</aside>
</body>