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

122 lines
5 KiB
Clojure
Raw Normal View History

2017-03-15 00:28:27 +01:00
(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]
2017-04-15 22:45:43 +02:00
[hiccup.core :as h]
2017-04-23 22:33:28 +02:00
[hiccup.page :as p]
[clj-time.format :as time-format]
[clj-time.core :as time]
[yenu.config :refer [env]]
2017-03-15 00:28:27 +01:00
[yenu.db.core :as db]))
2017-03-17 00:23:13 +01:00
(defn number-of-pages [images-on-page]
(int (Math/ceil
(/ (:count (db/get-image-count)) images-on-page))))
2017-04-15 22:45:43 +02:00
(defn page [number is-current? additional-class content]
(h/html
[:li {:class (cond-> "page-item" is-current? (str " " additional-class))}
2017-04-15 22:45:43 +02:00
[:a.page-link {:href (str "/page/" number)} content]]))
(defn dots []
(h/html [:li.page-item.disabled [:p.page-link "..."]]))
(defn pagelist [from to current]
(for [p (range from (inc to))] (page p (= p current) "active" p)))
(defn pagination [num current]
2017-05-11 21:45:45 +02:00
(if (< num 2)
""
(h/html
2018-03-22 00:33:26 +01:00
[:nav.float-right.clearfix {:aria-label "Page navigation"}
2018-05-04 16:22:20 +02:00
[:ul.pagination.d-none.d-sm-none.d-xs-none.d-md-flex
2017-05-11 21:45:45 +02:00
(page (dec current) (= current 1) "disabled" (h/html [:span.fa.fa-arrow-left] " Zurück"))
(cond
(< num 13) (pagelist 1 num current)
(<= current 7) (h/html (pagelist 1 10 current) (dots) (pagelist (- num 1) num current))
(>= current (- num 6)) (h/html (pagelist 1 2 current) (dots) (pagelist (- num 9) num current))
:else (h/html (pagelist 1 2 current) (dots) (pagelist (- current 3) (+ current 3) current) (dots) (pagelist (- num 1) num current)))
(page (inc current) (= current num) "disabled" (h/html "Weiter " [:span.fa.fa-arrow-right]))]]
[:div.clearfix])))
2017-04-15 22:45:43 +02:00
(defn pagination-mobile [num current]
2017-05-11 21:45:45 +02:00
(if (< num 2)
""
(h/html
2018-05-04 16:22:20 +02:00
[:ul.pagination.justify-content-end.d-md-none
2017-05-11 21:45:45 +02:00
(page (dec current) (= current 1) "disabled" (h/html [:span.fa.fa-arrow-left] " Zurück"))
[:form.form-inline
[:select#page-switcher.form-control
(for [p (range 1 (inc num))]
[:option (cond-> {:value p} (= p current) (assoc :selected "selected")) p])]]
(page (inc current) (= current num) "disabled" (h/html "Weiter " [:span.fa.fa-arrow-right]))])))
2017-04-15 23:08:11 +02:00
(defn index-page [current request]
2018-05-04 14:21:31 +02:00
(let [image-count (:images-per-page env)
2017-04-15 23:08:11 +02:00
offset (* (dec current) image-count)
2017-03-17 00:23:13 +01:00
pages (number-of-pages image-count)
images (db/get-all-images {:offset offset :count image-count})]
2017-04-23 22:33:28 +02:00
(layout/render-file "index.html"
{:images images
:flash (:flash request)
:pagination (pagination pages current)
:pagination-mobile (pagination-mobile pages current)})))
2017-03-15 00:28:27 +01:00
2017-04-03 21:21:48 +02:00
(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)})
2017-04-03 21:21:48 +02:00
prev-img (db/get-prev-image {:image-date (:created_at image)})
saved-author (get-in request [:cookies "author" :value])]
2018-05-04 14:21:31 +02:00
(if (not (nil? image))
(layout/render-file "detail.html"
{:image image
:flash (:flash request)
: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)})})
(layout/error-page {:status 404
:title "page not found"}))))
2017-04-03 21:21:48 +02:00
(defn add-comment! [image-id request]
(let [author (:author (:params request))
comment (:comment (:params request))]
2017-04-15 22:45:43 +02:00
(if (not= comment "")
(db/create-comment! {:image_id image-id
:author author
:comment comment}))
2017-04-03 21:21:48 +02:00
(-> (redirect (str "/show/" image-id))
2017-04-15 22:45:43 +02:00
(assoc :flash (if (not= comment "")
{:message "Danke für deinen Kommentar" :type "success"}
{:message "Der kommentar war leider leer." :type "danger"}))
2017-04-03 21:21:48 +02:00
(assoc-in [:cookies "author"]
{:value author
:path "/"
:max-age (* 60 60 24 30 12)}))))
2017-03-15 00:28:27 +01:00
(defn image-file [type hash ext]
(let [filename (str hash "." ext)]
(file-response (images/data-path "gallery" type filename))))
(defroutes core-routes
(GET "/" []
2017-03-17 00:23:13 +01:00
(redirect "/page/1"))
2017-03-29 22:14:55 +02:00
(GET "/page/:page-number" [page-number :as request]
(index-page (Integer. page-number) request))
2017-04-03 21:21:48 +02:00
(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))
2017-03-15 00:28:27 +01:00
(GET ["/images/:type/:hash.:ext"
;;:type #"(normal|raw|thumbnails)"
;;:hash #"[0-9]+-[^.]+"
;;:ext #"(png|jpg)"
]
[type hash ext]
2017-03-29 14:39:03 +02:00
(image-file type hash ext)))