From d446a0e6e6f97742fdfd72a15975029766949a39 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sat, 4 Mar 2017 00:43:17 +0100 Subject: [PATCH] Finish the image upload with tags --- resources/sql/queries.sql | 24 ++++++++++++++++++------ resources/templates/upload.html | 3 +++ src/clj/yenu/routes/home.clj | 21 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index ded7372..76fe1ae 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -1,4 +1,4 @@ --- :name create-image! :! :n +-- :name create-image! :i! INSERT INTO images (hash, title, description) VALUES (:hash, :title, :description) @@ -14,18 +14,30 @@ SELECT * FROM images WHERE id = :id DELETE FROM image WHERE id = :id +-- :name get-all-images :? :* +SELECT * FROM images +LIMIT :count OFFSET :offset --- :name create-tag! :! :n + +-- :name create-tag! :i! INSERT OR IGNORE INTO tags (tagname) VALUES (:tagname) --- :name get-tags-for-image :? :n +-- :name add-tag-to-image! :! +INSERT OR IGNORE INTO image_tags (image_id, tag_id) +VALUES (:imageid, :tagid) + +-- :name get-tag :? :1 +SELECT * FROM tags +WHERE tagname = :tagname + +-- :name get-tags-for-image :? :* SELECT tags.tagname FROM image_tags INNER JOIN tags ON tags.id = image_tags.tag_id WHERE image_id = :id ORDER BY tags.tagname ASC --- :name get-images-for-tagname :? :n +-- :name get-images-for-tagname :? :* SELECT images.* FROM image_tags INNER JOIN images ON images.id = image_tags.image_id INNER JOIN tags on tags.id = image_tags.tag_id @@ -37,12 +49,12 @@ ORDER BY images.created_at DESC INSERT INTO comments (image_id, author, comment) VALUES (:image_id, :author, :comment) --- :name get-comments-for-image :? :n +-- :name get-comments-for-image :? :* SELECT * FROM comments WHERE image_id = :image_id ORDER BY created_at DESC --- :name get-recent-comments +-- :name get-recent-comments :? :* SELECT * FROM comments ORDER BY created_at DESC LIMIT :num-comments diff --git a/resources/templates/upload.html b/resources/templates/upload.html index 53e2e8e..f783c58 100644 --- a/resources/templates/upload.html +++ b/resources/templates/upload.html @@ -2,5 +2,8 @@
{% csrf-field %} + + +
diff --git a/src/clj/yenu/routes/home.clj b/src/clj/yenu/routes/home.clj index ab9453d..d0b812c 100644 --- a/src/clj/yenu/routes/home.clj +++ b/src/clj/yenu/routes/home.clj @@ -4,7 +4,9 @@ [ring.util.http-response :as response] [ring.util.response :refer [redirect]] [clojure.java.io :as io] - [yenu.helpers.images :as images]) + [yenu.helpers.images :as images] + [yenu.db.core :as db] + [clojure.string :as str]) (:import [java.io File FileInputStream FileOutputStream])) (defn decoded-url-file [filepath] @@ -21,11 +23,24 @@ (.flush out)))) target-filepath)) +(defn add-tag [image-id tag] + (db/create-tag! {:tagname tag}) + (let [{tag-id :id} (db/get-tag {:tagname tag})] + (db/add-tag-to-image! {:imageid image-id :tagid tag-id}))) + +(defn add-to-database [filehash title description tags] + (let [img (db/create-image! {:hash filehash :title title :description description}) + image-id ((keyword "last_insert_rowid()") img) + all-tags (str/split tags, #"(\s*,\s*|\s+)")] + (map #(add-tag image-id %) all-tags))) + (defroutes upload-routes (GET "/upload" [] (layout/render "upload.html")) - (POST "/upload" [file] - (-> (upload-file file) (images/process-image)) + (POST "/upload" [file title description tags] + (-> (upload-file file) + (images/process-image) + (add-to-database title description tags)) (redirect "/"))) (defroutes home-routes