Delete comments

This commit is contained in:
Aaron Fischer 2017-04-15 23:08:11 +02:00
parent b7ea1bb5f7
commit 6c6054ed38
4 changed files with 24 additions and 4 deletions

View file

@ -84,6 +84,15 @@ SELECT * FROM comments
WHERE image_id = :image_id WHERE image_id = :image_id
ORDER BY created_at ASC ORDER BY created_at ASC
-- :name get-comment :? :1
SELECT * FROM comments
WHERE id = :id
LIMIT 1
-- :name delete-comment! :! :1
DELETE FROM comments
WHERE id = :id
-- :name get-recent-comments :? :* -- :name get-recent-comments :? :*
SELECT comments.*, images.title FROM comments SELECT comments.*, images.title FROM comments
LEFT JOIN images ON images.id = comments.image_id LEFT JOIN images ON images.id = comments.image_id

View file

@ -72,6 +72,9 @@
<img src="https://robohash.org/{{ comment.author }}?size=50x50" class="d-flex mr-3"/> <img src="https://robohash.org/{{ comment.author }}?size=50x50" class="d-flex mr-3"/>
<div class="media-body"> <div class="media-body">
<h5 class="mt-0 mb-1">{{ comment.author }}</h5> <h5 class="mt-0 mb-1">{{ comment.author }}</h5>
{% ifequal identity ":creator" %}
<a class="btn btn-sm btn-danger" href="/delete-comment/{{ comment.id}}">Löschen</a>
{% endifequal %}
{{ comment.comment|markdown-to-html|safe }} {{ comment.comment|markdown-to-html|safe }}
</div> </div>
</li> </li>

View file

@ -58,6 +58,9 @@
(db/delete-image-tags! {:id id}) (db/delete-image-tags! {:id id})
(add-tags-for-image id tags)) (add-tags-for-image id tags))
(defn image-id-from-comment [id]
(:image_id (db/get-comment {:id id})))
(defroutes admin-routes (defroutes admin-routes
(GET "/upload" [:as request] (GET "/upload" [:as request]
(-> (layout/render (-> (layout/render
@ -75,6 +78,12 @@
(-> (redirect "/page/1") (-> (redirect "/page/1")
(assoc :flash {:message "Bild wurde gelöscht" :type "danger"}))) (assoc :flash {:message "Bild wurde gelöscht" :type "danger"})))
(GET "/delete-comment/:id" [id]
(let [image-id (image-id-from-comment id)]
(db/delete-comment! {:id id})
(-> (redirect (str "/show/" image-id))
(assoc :flash {:message "Kommentar wurde gelöscht" :type "danger"}))))
(GET "/edit/:id" [id :as request] (GET "/edit/:id" [id :as request]
(edit-image-form id request)) (edit-image-form id request))

View file

@ -35,16 +35,15 @@
(page (inc current) (= current num) "disabled" (h/html "Weiter " [:span.fa.fa-arrow-right]))]] (page (inc current) (= current num) "disabled" (h/html "Weiter " [:span.fa.fa-arrow-right]))]]
[:div.clearfix])) [:div.clearfix]))
(defn index-page [current-page request] (defn index-page [current request]
(let [image-count 25 (let [image-count 25
offset (* (dec current-page) image-count) offset (* (dec current) image-count)
pages (number-of-pages image-count) pages (number-of-pages image-count)
images (db/get-all-images {:offset offset :count image-count})] images (db/get-all-images {:offset offset :count image-count})]
(layout/render "index.html" (layout/render "index.html"
{:images images {:images images
:flash (:flash request) :flash (:flash request)
:current-page current-page :pagination (pagination pages current)})))
:pagination (pagination pages current-page)})))
(defn detail-page [image-id request] (defn detail-page [image-id request]
(let [image (db/get-image {:id image-id}) (let [image (db/get-image {:id image-id})