Finish the image upload with tags

This commit is contained in:
Aaron Fischer 2017-03-04 00:43:17 +01:00
parent 5077560a6e
commit d446a0e6e6
3 changed files with 39 additions and 9 deletions

View file

@ -1,4 +1,4 @@
-- :name create-image! :! :n -- :name create-image! :i!
INSERT INTO images (hash, title, description) INSERT INTO images (hash, title, description)
VALUES (:hash, :title, :description) VALUES (:hash, :title, :description)
@ -14,18 +14,30 @@ SELECT * FROM images WHERE id = :id
DELETE FROM image DELETE FROM image
WHERE id = :id 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) INSERT OR IGNORE INTO tags (tagname)
VALUES (: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 SELECT tags.tagname FROM image_tags
INNER JOIN tags ON tags.id = image_tags.tag_id INNER JOIN tags ON tags.id = image_tags.tag_id
WHERE image_id = :id WHERE image_id = :id
ORDER BY tags.tagname ASC ORDER BY tags.tagname ASC
-- :name get-images-for-tagname :? :n -- :name get-images-for-tagname :? :*
SELECT images.* FROM image_tags SELECT images.* FROM image_tags
INNER JOIN images ON images.id = image_tags.image_id INNER JOIN images ON images.id = image_tags.image_id
INNER JOIN tags on tags.id = image_tags.tag_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) INSERT INTO comments (image_id, author, comment)
VALUES (:image_id, :author, :comment) VALUES (:image_id, :author, :comment)
-- :name get-comments-for-image :? :n -- :name get-comments-for-image :? :*
SELECT * FROM comments SELECT * FROM comments
WHERE image_id = :image_id WHERE image_id = :image_id
ORDER BY created_at DESC ORDER BY created_at DESC
-- :name get-recent-comments -- :name get-recent-comments :? :*
SELECT * FROM comments SELECT * FROM comments
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT :num-comments LIMIT :num-comments

View file

@ -2,5 +2,8 @@
<form action="/upload" enctype="multipart/form-data" method="POST"> <form action="/upload" enctype="multipart/form-data" method="POST">
{% csrf-field %} {% csrf-field %}
<input id="file" name="file" type="file" /> <input id="file" name="file" type="file" />
<input type="text" name="title">
<textarea name="description"></textarea>
<input type="text" name="tags">
<input type="submit" value="upload" /> <input type="submit" value="upload" />
</form> </form>

View file

@ -4,7 +4,9 @@
[ring.util.http-response :as response] [ring.util.http-response :as response]
[ring.util.response :refer [redirect]] [ring.util.response :refer [redirect]]
[clojure.java.io :as io] [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])) (:import [java.io File FileInputStream FileOutputStream]))
(defn decoded-url-file [filepath] (defn decoded-url-file [filepath]
@ -21,11 +23,24 @@
(.flush out)))) (.flush out))))
target-filepath)) 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 (defroutes upload-routes
(GET "/upload" [] (GET "/upload" []
(layout/render "upload.html")) (layout/render "upload.html"))
(POST "/upload" [file] (POST "/upload" [file title description tags]
(-> (upload-file file) (images/process-image)) (-> (upload-file file)
(images/process-image)
(add-to-database title description tags))
(redirect "/"))) (redirect "/")))
(defroutes home-routes (defroutes home-routes