diff options
Diffstat (limited to 'src/chef/pages')
-rw-r--r-- | src/chef/pages/admin.clj | 2 | ||||
-rw-r--r-- | src/chef/pages/admin/api.clj | 57 | ||||
-rw-r--r-- | src/chef/pages/admin/recipe_editor.clj | 14 | ||||
-rw-r--r-- | src/chef/pages/recipe.clj | 50 |
4 files changed, 101 insertions, 22 deletions
diff --git a/src/chef/pages/admin.clj b/src/chef/pages/admin.clj index 8ffadcc..ea19cc3 100644 --- a/src/chef/pages/admin.clj +++ b/src/chef/pages/admin.clj @@ -69,7 +69,7 @@ [:button {:class ["button" "primary"] :onclick (str "window.open(\"/admin/recipe-editor/" (:recipes/id recipe) - "\", \"\", \"width=500,height=500\")")} + "\", \"\", \"width=900,height=900\")")} "Bearbeiten"] [:button {:class ["button error"] :hx-trigger "click" diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj index f1b7226..1119607 100644 --- a/src/chef/pages/admin/api.clj +++ b/src/chef/pages/admin/api.clj @@ -1,9 +1,12 @@ (ns chef.pages.admin.api (:require [chef.utils :as cutils] [chef.database :as cdb] + [clojure.string :as cstr] [next.jdbc :as jdbc] [honey.sql :as sql] - [ring.util.response :as ruresp])) + [ring.util.response :as ruresp] + [clojure.java.io :as cjio]) + (:import java.io.File)) (defn create-category [req] (cutils/auth-only req @@ -74,18 +77,50 @@ (ruresp/header "HX-Refresh" "true"))) (ruresp/bad-request "Bad request.")))) -;;TODO: validate request (defn edit-recipe [req] (cutils/auth-only req + (let [id (try (Integer/parseInt (get-in req [:path-params :id])) + (catch Exception _ nil)) + ingredients (get-in req [:params "ingredients"])] + (if (and (some? id) + (cutils/valid-ingredients? ingredients)) + (do (when-let [thumbnail (get-in req [:params "thumbnail"])] + (when-let [existing-thumbnail-file (->> {:select [:*] + :from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first + cutils/get-thumbnail-file)] + (.delete ^File existing-thumbnail-file)) + (cjio/copy (:tempfile thumbnail) + (File. (str "./thumbnails/" id "." + (-> thumbnail + :filename + (cstr/split #"\.") + last))))) + (jdbc/execute! @cdb/db + (sql/format {:update :recipes + :set {:title (get-in req [:params "title"]) + :category (get-in req [:params "category"]) + :unit (get-in req [:params "ingredients-unit"]) + :ingredients ingredients + :preparation (get-in req [:params "preparation"])} + :where [:= :id id]})) + (ruresp/response "Saved.")) + (ruresp/bad-request "Bad request."))))) + +(defn delete-thumbnail [req] + (cutils/auth-only req (if-let [id (try (Integer/parseInt (get-in req [:path-params :id])) (catch Exception _ nil))] - (do (jdbc/execute! @cdb/db - (sql/format {:update :recipes - :set {:title (get-in req [:params "title"]) - :category (get-in req [:params "category"]) - :unit (get-in req [:params "ingredients-unit"]) - :ingredients (get-in req [:params "ingredients"]) - :preparation (get-in req [:params "preparation"])} - :where [:= :id id]})) - (ruresp/response "Saved.")) + (when-let [thumbnail-file (->> {:select [:*] + :from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first + cutils/get-thumbnail-file)] + (.delete ^File thumbnail-file) + (ruresp/response "Done.")) (ruresp/bad-request "Bad request.")))) diff --git a/src/chef/pages/admin/recipe_editor.clj b/src/chef/pages/admin/recipe_editor.clj index 6afd591..cc30942 100644 --- a/src/chef/pages/admin/recipe_editor.clj +++ b/src/chef/pages/admin/recipe_editor.clj @@ -13,9 +13,20 @@ [:h1 "Rezept bearbeiten"] [:form {:style {:width "50%"} :hx-post (str "/api/admin/edit-recipe/" (:recipes/id recipe)) - :hx-swap "none"} + :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 [:*] @@ -36,7 +47,6 @@ [:p ":"]] [:textarea {:name "ingredients"} (:recipes/ingredients recipe)] - ;; Regex: ([A-z0-9 ]*)=([0-9]*) ?([A-z]*) [:p "(Je Zeile eine Zutat, in dem Format " [:code "[Beschreibung der Zutat]=[Menge als Zahl][Einheit der Menge]"] ".)"] [:h2 "Zubereitung"] [:textarea {:name "preparation"} diff --git a/src/chef/pages/recipe.clj b/src/chef/pages/recipe.clj index 71dd4b2..84f999c 100644 --- a/src/chef/pages/recipe.clj +++ b/src/chef/pages/recipe.clj @@ -1,10 +1,12 @@ (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])) + [ring.util.response :as ruresp]) + (:import java.io.File)) (defn- render [recipe] (cutils/gen-page (str "chef - " (:recipes/title recipe)) @@ -13,13 +15,33 @@ [:h1 {:style {:display :inline-block :margin-right "0.5em"}} (:recipes/title recipe)] - [:i (cutils/category-path (->> {:select [:*] - :from [:categories] - :where [:= :id (:recipes/category recipe)]} - sql/format - (jdbc/execute! @cdb/db) - first))]] - [:b "TODO"]])) + [: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 [:*] @@ -32,3 +54,15 @@ 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."))) |