summaryrefslogtreecommitdiff
path: root/src/chef/frontend/visitor/recipe.clj
blob: f76b58ae3141908f2077cab2ede00d69c95d2e8f (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
(ns chef.frontend.visitor.recipe
  (:require [chef.utils :as cutils]
            [clojure.string :as cstr]
            [hiccup2.core :as html]
            [ring.util.response :as ruresp]

            [chef.logic.recipes :as clrecipes]
            [chef.logic.categories :as clcategories]))

(defn- render [portions recipe]
  (cutils/gen-page (str "chef - " (:recipes/title recipe))
                   [:div {:style {:margin "2em auto"
                                  :width  :fit-content}
                          :class "card"}
                    [:div {:style {:width :inherit}}
                     [:h1 {:style {:display      :inline-block
                                   :margin-right "0.5em"}}
                      (:recipes/title recipe)]
                     [:i (str "(" (-> (:recipes/category recipe)
                                      clcategories/get-category
                                      clcategories/generate-path) ")")]]
                    (when (some? (clrecipes/get-recipe-thumbnail (:recipes/id recipe)))
                      [:img {:src   (str "/recipes/" (:recipes/id recipe) "/thumbnail")
                             :width "50%"
                             :style {:border-radius "10px"}}])
                    [:h2 {:style {:width :inherit}}
                     "Zutaten pro "
                     [:input {:type  :number
                              :value portions
                              :style {:width   "3em"
                                      :display :inline-block}
                              "_"    "on change go to url `?portions=${value of me}`"}]
                     (condp = (:recipes/unit recipe)
                       0 " Portion(en)"
                       1 " Person(en)"
                       "")
                     ":"]
                    [:ul {:style {:width :inherit}}
                     (for [ingredient (-> recipe
                                          :recipes/ingredients
                                          cutils/parse-ingredients)]
                       [:li
                        [:b (:description ingredient)] ": "
                        (* (:amount ingredient) portions)
                        (:unit ingredient)])]
                    [:h2 {:style {:width :inherit}} "Zubereitung"]
                    (->> (:recipes/preparation recipe)
                         cstr/split-lines
                         (map #(if (cstr/blank? %)
                                 [:br]
                                 [:p {:style {:width :inherit}} %])))]))

(defn handler [req]
  (->> (clrecipes/get-recipe (get-in req [:path-params :id]))
       (render (or (cutils/s->int-or-nil (get-in req [:params "portions"]))
                   1))
       html/html
       str
       ruresp/response))