yenu/src/clj/yenu/routes/core.clj

71 lines
2.6 KiB
Clojure

(ns yenu.routes.core
(:require [yenu.layout :as layout]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response]
[ring.util.response :refer [redirect file-response]]
[yenu.helpers.images :as images]
[yenu.db.core :as db]))
(defn number-of-pages [images-on-page]
(int (Math/ceil
(/ (:count (db/get-image-count)) images-on-page))))
(defn index-page [current-page request]
(let [image-count 15
offset (* (dec current-page) image-count)
pages (number-of-pages image-count)
images (db/get-all-images {:offset offset :count image-count})]
(layout/render "index.html"
{:images images
:current-page current-page
:pages (range 1 (inc pages))
:flash (:flash request)})))
(defn detail-page [image-id request]
(let [image (db/get-image {:id image-id})
next-img (db/get-next-image {:image-date (:created_at image)})
prev-img (db/get-prev-image {:image-date (:created_at image)})
saved-author (get-in request [:cookies "author" :value])]
(layout/render "detail.html"
{:image image
:next-image next-img
:prev-image prev-img
:tags (db/get-tags-for-image {:id (:id image)})
:saved-author saved-author
:comments (db/get-comments-for-image {:image_id (:id image)})})))
(defn add-comment! [image-id request]
(let [author (:author (:params request))
comment (:comment (:params request))]
(db/create-comment! {:image_id image-id
:author author
:comment comment})
(-> (redirect (str "/show/" image-id))
(assoc-in [:cookies "author"]
{:value author
:path "/"
:max-age (* 60 60 24 30 12)}))))
(defn image-file [type hash ext]
(let [filename (str hash "." ext)]
(file-response (images/data-path "gallery" type filename))))
(defroutes core-routes
(GET "/" []
(redirect "/page/1"))
(GET "/page/:page-number" [page-number :as request]
(index-page (Integer. page-number) request))
(GET "/show/:image-id" [image-id :as request]
(detail-page image-id request))
(POST "/add-comment/:image-id" [image-id :as request]
(add-comment! image-id request))
(GET ["/images/:type/:hash.:ext"
;;:type #"(normal|raw|thumbnails)"
;;:hash #"[0-9]+-[^.]+"
;;:ext #"(png|jpg)"
]
[type hash ext]
(image-file type hash ext)))