summaryrefslogtreecommitdiff
path: root/src/chef/pages/recipe.clj
blob: 84f999c1620c88fe5397f977afa9de2d3d41e2a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns chef.pages.recipe
  (:require [chef.database :as cdb]
            [chef.utils :as cutils]
            [clojure.string :as cstr]
            [hiccup2.core :as html]
            [honey.sql :as sql]
            [next.jdbc :as jdbc]
            [ring.util.response :as ruresp])
  (:import java.io.File))

(defn- render [recipe]
  (cutils/gen-page (str "chef - " (:recipes/title recipe))
                   [:div {:style {:margin-left "1em"}}
                    [:div
                     [:h1 {:style {:display      :inline-block
                                   :margin-right "0.5em"}}
                      (:recipes/title recipe)]
                     [:i (str "(" (cutils/category-path (->> {:select [:*]
                                                              :from   [:categories]
                                                              :where  [:= :id (:recipes/category recipe)]}
                                                             sql/format
                                                             (jdbc/execute! @cdb/db)
                                                             first)) ")")]]
                    (when (some? (cutils/get-thumbnail-file recipe))
                      [:img {:src    (str "/recipes/" (:recipes/id recipe) "/thumbnail")
                             :width "50%"}])
                    [:h2 (str "Zutaten"
                              (condp = (:recipes/unit recipe)
                                0 " pro Portion"
                                1 " pro Person"
                                "")
                              ":")]
                    [:ul (for [ingredient (-> recipe
                                              :recipes/ingredients
                                              cutils/parse-ingredients)]
                           [:li
                            [:b (:description ingredient)] ": "
                            (:amount ingredient) (:unit ingredient)])]
                    [:h2 "Zubereitung"]
                    (->> (:recipes/preparation recipe)
                         cstr/split-lines
                         (map #(if (cstr/blank? %)
                                 [:br]
                                 [:p %])))]))

(defn handler [req]
  (->> {:select [:*]
        :from   [:recipes]
        :where  [:= :id (get-in req [:path-params :id])]}
       sql/format
       (jdbc/execute! @cdb/db)
       first
       render
       html/html
       str
       ruresp/response))

(defn thumbnail-handler [req]
  (if-let [id (get-in req [:path-params :id])]
    (when-let [thumbnail-file (->> {:select [:*]
                                    :from   [:recipes]
                                    :where  [:= :id id]}
                                   sql/format
                                   (jdbc/execute! @cdb/db)
                                   first
                                   cutils/get-thumbnail-file)]
      (ruresp/file-response (.getPath ^File thumbnail-file)))
    (ruresp/bad-request "Bad request.")))