From f030a32679e7291eeb3ec4925a6354767bed3694 Mon Sep 17 00:00:00 2001
From: Tim <contact@bytim.eu>
Date: Sun, 1 Jun 2025 22:02:24 +0200
Subject: Add content to recipe page

---
 src/chef/pages/admin/api.clj | 28 +++++++++++++++-------------
 src/chef/pages/recipe.clj    | 20 +++++++++++++++++++-
 src/chef/utils.clj           | 13 +++++++++++++
 3 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj
index f1b7226..9696eb5 100644
--- a/src/chef/pages/admin/api.clj
+++ b/src/chef/pages/admin/api.clj
@@ -74,18 +74,20 @@
                               (ruresp/header "HX-Refresh" "true")))
                       (ruresp/bad-request "Bad request."))))
 
-;;TODO: validate request
 (defn edit-recipe [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."))
-                      (ruresp/bad-request "Bad request."))))
+                    (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 (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.")))))
diff --git a/src/chef/pages/recipe.clj b/src/chef/pages/recipe.clj
index 71dd4b2..f1c1a21 100644
--- a/src/chef/pages/recipe.clj
+++ b/src/chef/pages/recipe.clj
@@ -1,6 +1,7 @@
 (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]
@@ -19,7 +20,24 @@
                                                     sql/format
                                                     (jdbc/execute! @cdb/db)
                                                     first))]]
-                    [:b "TODO"]]))
+                    [: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 [:*]
diff --git a/src/chef/utils.clj b/src/chef/utils.clj
index c2c6bc1..7f476a2 100644
--- a/src/chef/utils.clj
+++ b/src/chef/utils.clj
@@ -40,3 +40,16 @@
        category-parents
        (map #(:categories/name %))
        (cstr/join " > ")))
+
+(defn parse-ingredients [s]
+  (->> s
+       (re-seq #"([A-z0-9 ]*)=([0-9]*) ?([A-z]*)")
+       (map #(hash-map :description (nth % 1)
+                       :amount (Integer/parseInt (nth % 2))
+                       :unit (nth % 3)))))
+
+(defn valid-ingredients? [s]
+  (and (string? s)
+       (->> s
+            (re-matches #"(([A-z0-9 ]*)=([0-9]*) ?([A-z]*)\n?)*")
+            some?)))
-- 
cgit v1.2.3