summaryrefslogtreecommitdiff
path: root/src/chef/pages
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-06-07 13:09:24 +0200
committerTim <contact@bytim.eu>2025-06-07 13:09:24 +0200
commit294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12 (patch)
treea9e0ca6460ce9361b967b42c59572bc053633775 /src/chef/pages
parent82da261f168454cd606f4f150313844b9d4b9eea (diff)
downloadchef-294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12.tar.xz
chef-294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12.zip
Add features to upload and delete thumbnails
Diffstat (limited to 'src/chef/pages')
-rw-r--r--src/chef/pages/admin.clj2
-rw-r--r--src/chef/pages/admin/api.clj37
-rw-r--r--src/chef/pages/admin/recipe_editor.clj14
3 files changed, 48 insertions, 5 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 9696eb5..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
@@ -81,7 +84,22 @@
ingredients (get-in req [:params "ingredients"])]
(if (and (some? id)
(cutils/valid-ingredients? ingredients))
- (do (jdbc/execute! @cdb/db
+ (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"])
@@ -91,3 +109,18 @@
: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))]
+ (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"}