summaryrefslogtreecommitdiff
path: root/src/chef/pages/admin/recipe_editor.clj
blob: cc309422025fd84235667d28bde869c9e594386d (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
69
70
71
(ns chef.pages.admin.recipe-editor
  (:require [hiccup2.core :as html]
            [hiccup.util :as hutil]
            [honey.sql :as sql]
            [next.jdbc :as jdbc]
            [chef.database :as cdb]
            [ring.util.response :as ruresp]
            [chef.utils :as cutils]))

(defn render [recipe]
  (cutils/gen-page "chef - Rezept bearbeiten"
                   [:div {:style {:margin-left "1em"}}
                    [:h1 "Rezept bearbeiten"]
                    [:form {:style   {:width "50%"}
                            :hx-post (str "/api/admin/edit-recipe/" (:recipes/id recipe))
                            :hx-swap "none"
                            :enctype "multipart/form-data"}
                     [:input {:type  :text :name "title" :placeholder "Titel"
                              :value (:recipes/title recipe)}]
                     [:div {:style {:display :flex}}
                      [:p {:style {:margin-right "0.5em"}} "Thumbnail: "]
                      [:input {:type  :file :name "thumbnail"
                               :style {:height  :fit-content
                                       :padding "0.3em"}}]]
                     [:button {:class      ["button" "error"]
                               :hx-trigger "click"
                               :hx-delete  (str "/api/admin/delete-thumbnail/" (:recipes/id recipe))
                               :hx-swap    :none}
                      "Thumbnail entfernen"]
                     [:h2 "Kategorie"]
                     [:select {:name "category"}
                      (for [category (->> (sql/format {:select [:*]
                                                       :from   [:categories]})
                                          (jdbc/execute! @cdb/db)
                                          (filter #(pos? (:categories/id %))))]
                        [:option {:value    (:categories/id category)
                                  :selected (= (:categories/id category) (:recipes/category recipe))}
                         (cutils/category-path category)])]
                     [:h2 "Zutaten"]
                     [:div {:style {:display :flex}}
                      [:p {:style {:margin-right "0.5em"}} "Pro"]
                      [:select {:name  "ingredients-unit"
                                :style {:height  :fit-content
                                        :padding "0.3em"}}
                       [:option {:value 0 :selected (= (:recipes/unit recipe) 0)} "Portion"]
                       [:option {:value 1 :selected (= (:recipes/unit recipe) 1)} "Person"]]
                      [:p ":"]]
                     [:textarea {:name "ingredients"}
                      (:recipes/ingredients recipe)]
                     [:p "(Je Zeile eine Zutat, in dem Format " [:code "[Beschreibung der Zutat]=[Menge als Zahl][Einheit der Menge]"] ".)"]
                     [:h2 "Zubereitung"]
                     [:textarea {:name "preparation"}
                      (:recipes/preparation recipe)]
                     [:button {:type  :submit
                               :style {:margin-top "1em"}} "Speichern"]]]
                   [:script (hutil/raw-string "window.addEventListener(\"htmx:afterRequest\", () => window.close())")]))

(defn handler [req]
  (cutils/auth-only req
                    (if-let [id (try (Integer/parseInt (get-in req [:path-params :id]))
                                     (catch Exception _ nil))]
                      (->> (sql/format {:select [:*]
                                        :from   [:recipes]
                                        :where  [:= :id id]})
                           (jdbc/execute! @cdb/db)
                           first
                           render
                           html/html
                           str
                           ruresp/response)
                      (ruresp/bad-request "Bad request."))))