diff options
author | Tim <contact@bytim.eu> | 2025-05-31 17:03:51 +0200 |
---|---|---|
committer | Tim <contact@bytim.eu> | 2025-05-31 17:03:51 +0200 |
commit | ffb5d0b740e3fa23143ad89dab29a44d5b0acd34 (patch) | |
tree | 1c4ff1d69bbf81a0a3dfe7c2c2bf1f5cd3e867ad /src/chef | |
parent | ea7b0078478ba7925f2db3cb1fa038e8a3d85ab8 (diff) | |
download | chef-ffb5d0b740e3fa23143ad89dab29a44d5b0acd34.tar.xz chef-ffb5d0b740e3fa23143ad89dab29a44d5b0acd34.zip |
Add real data to home page + add search logic + add recipe pages
Diffstat (limited to 'src/chef')
-rw-r--r-- | src/chef/components/search.clj | 43 | ||||
-rw-r--r-- | src/chef/pages/admin.clj | 2 | ||||
-rw-r--r-- | src/chef/pages/admin/api.clj | 6 | ||||
-rw-r--r-- | src/chef/pages/home.clj | 66 | ||||
-rw-r--r-- | src/chef/pages/recipe.clj | 34 | ||||
-rw-r--r-- | src/chef/routes.clj | 4 | ||||
-rw-r--r-- | src/chef/utils.clj | 31 |
7 files changed, 140 insertions, 46 deletions
diff --git a/src/chef/components/search.clj b/src/chef/components/search.clj index 843f907..e428a23 100644 --- a/src/chef/components/search.clj +++ b/src/chef/components/search.clj @@ -1,13 +1,48 @@ (ns chef.components.search - (:require [hiccup2.core :as html] + (: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])) -(defn render [query] - [:p "Results; Query: " query]) +(defn render [query category] + [:table + [:tr + [:th "Rezept"] + [:th "Kategorie"]] + (for [recipe (jdbc/execute! @cdb/db + (sql/format {:select [:*] + :from [:recipes]})) + :let [recipe-category (->> {:select [:*] + :from [:categories] + :where [:= :id (:recipes/category recipe)]} + sql/format + (jdbc/execute! @cdb/db) + first)]] + (when (or (= category -1) + (and (cstr/includes? (-> recipe + :recipes/title + cstr/lower-case) + query) + (some #(= (:categories/id %) category) + (cutils/category-parents recipe-category)))) + [:tr + [:td + [:b [:a {:href (str "/recipes/" (:recipes/id recipe))} (:recipes/title recipe)]]] + [:td + (cutils/category-path (->> {:select [:*] + :from [:categories] + :where [:= :id (:recipes/category recipe)]} + sql/format + (jdbc/execute! @cdb/db) + first))]]))]) (defn handler [req] (if-let [query (get-in req [:params "query"])] - (-> (render query) + (-> (render query (try (Integer/parseInt (get-in req [:params "category"])) + (catch Exception _ -1))) html/html str ruresp/response) diff --git a/src/chef/pages/admin.clj b/src/chef/pages/admin.clj index 25f99a6..8ffadcc 100644 --- a/src/chef/pages/admin.clj +++ b/src/chef/pages/admin.clj @@ -89,7 +89,7 @@ (jdbc/execute! @cdb/db))) (->> (sql/format {:select [:*] :from [:categories] - :where [:and [:is :parent :null] [:> :id 0]]}) + :where [:= :parent -1]}) (jdbc/execute! @cdb/db)))] [:h2 "Rezepte"] [:table diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj index 8b2723a..f1b7226 100644 --- a/src/chef/pages/admin/api.clj +++ b/src/chef/pages/admin/api.clj @@ -9,9 +9,9 @@ (cutils/auth-only req (jdbc/execute! @cdb/db (sql/format {:insert-into [:categories] - :values [(merge {:name "New category"} - (when-let [parent (get-in req [:params "parent"])] - {:parent parent}))]})) + :values [(merge {:name "New category" + :parent (or (get-in req [:params "parent"]) + -1)})]})) (-> (ruresp/created "Created.") (ruresp/header "HX-Refresh" "true")))) diff --git a/src/chef/pages/home.clj b/src/chef/pages/home.clj index 748c60c..9bc82fb 100644 --- a/src/chef/pages/home.clj +++ b/src/chef/pages/home.clj @@ -1,33 +1,51 @@ (ns chef.pages.home - (:require [hiccup2.core :as html] + (:require [chef.database :as cdb] + [hiccup2.core :as html] + [honey.sql :as sql] + [next.jdbc :as jdbc] [ring.util.response :as ruresp] - [chef.utils :as cutils])) + [chef.utils :as cutils] -(defn- render [] + [chef.components.search :as ccsearch])) + +(defn- render [req] (cutils/gen-page "chef" - [:div {:style {:text-align :center}} - [:h1 "chef"] - [:h2 "Finde das perfekte Gericht für dich!"] - [:b "Welchen Gang suchst du?"] - ;; TODO: Dummy data; replace with data from db - [:div - [:button {:style {:margin-bottom "1em"}} "Vorspeise"] - [:br] - [:button {:style {:margin-bottom "1em"}} "Hauptgang"] - [:br] - [:button {:style {:margin-bottom "1em"}} "Nachtisch"] - [:br]] - [:input {:type :text - :style {:width "90%" :margin :auto} - :placeholder "Suche" - :hx-get "/components/search" - :name "query" - :hx-swap "innerHTML" - :hx-target "#search-results"}] - [:div {:id "search-results"}]])) + (let [category (->> {:select [:*] + :from [:categories] + :where [:= :id (or (get-in req [:params "category"]) -1)]} + sql/format + (jdbc/execute! @cdb/db) + first)] + [:div {:style {:text-align :center}} + [:h1 "chef"] + [:h2 "Finde das perfekte Gericht für dich!"] + [:b (:categories/question category)] + [:div + (for [child-category (->> {:select [:*] + :from [:categories] + :where [:= :parent (:categories/id category)]} + sql/format + (jdbc/execute! @cdb/db))] + [:div + [:button {:style {:margin-bottom "1em"} + :onclick (str "window.location = \"/?category=" (:categories/id child-category) "\"")} + (:categories/name child-category)] + [:br]])] + (when (pos? (:categories/id category)) + [:h3 (cutils/category-path category)]) + [:input {:type :text + :style {:width "90%" :margin :auto} + :placeholder "Suche" + :hx-get (str "/components/search?category=" (:categories/id category)) + :name "query" + :hx-swap "innerHTML" + :hx-target "#search-results"}] + [:div {:id "search-results"} + (ccsearch/render "" (:categories/id category))]]))) (defn handler [req] - (-> (render) + (-> req + render html/html str ruresp/response)) diff --git a/src/chef/pages/recipe.clj b/src/chef/pages/recipe.clj new file mode 100644 index 0000000..71dd4b2 --- /dev/null +++ b/src/chef/pages/recipe.clj @@ -0,0 +1,34 @@ +(ns chef.pages.recipe + (:require [chef.database :as cdb] + [chef.utils :as cutils] + [hiccup2.core :as html] + [honey.sql :as sql] + [next.jdbc :as jdbc] + [ring.util.response :as ruresp])) + +(defn- render [recipe] + (cutils/gen-page (str "chef - " (:recipes/title recipe)) + [:div {:style {:margin-left "1em"}} + [:div + [: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"]])) + +(defn handler [req] + (->> {:select [:*] + :from [:recipes] + :where [:= :id (get-in req [:path-params :id])]} + sql/format + (jdbc/execute! @cdb/db) + first + render + html/html + str + ruresp/response)) diff --git a/src/chef/routes.clj b/src/chef/routes.clj index 9b16565..6b57f4f 100644 --- a/src/chef/routes.clj +++ b/src/chef/routes.clj @@ -8,6 +8,7 @@ [chef.pages.home :as cphome] [chef.pages.admin :as cpadmin] + [chef.pages.recipe :as cprecipe] [chef.components.search :as ccsearch] @@ -15,10 +16,11 @@ [chef.pages.admin.recipe-editor :as cparecipe-editor])) (def router [["/" {:get {:handler cphome/handler}}] + ["/recipes/:id" {:get cprecipe/handler}] + ["/static/*" (rring/create-resource-handler)] ["/admin" ["/" {:get {:handler cpadmin/handler}}] ["/recipe-editor/:id" {:get {:handler cparecipe-editor/handler}}]] - ["/static/*" (rring/create-resource-handler)] ["/components" ["/search" {:get {:handler ccsearch/handler}}]] diff --git a/src/chef/utils.clj b/src/chef/utils.clj index 1ea5136..c2c6bc1 100644 --- a/src/chef/utils.clj +++ b/src/chef/utils.clj @@ -21,17 +21,22 @@ (ruresp/status 302) (ruresp/header "Location" "/auth")))) +(defn category-parents [category] + (loop [parents (list) + category category] + (let [updated-parents (conj parents category)] + (if (not= -1 (:categories/parent category)) + (recur updated-parents + (->> {:select [:*] + :from [:categories] + :where [:= :id (:categories/parent category)]} + sql/format + (jdbc/execute! @cdb/db) + first)) + updated-parents)))) + (defn category-path [category] - (cstr/join " > " - (loop [path (list) - category category] - (let [new-path (conj path (:categories/name category))] - (if (some? (:categories/parent category)) - (recur new-path - (->> {:select [:*] - :from [:categories] - :where [:= :id (:categories/parent category)]} - sql/format - (jdbc/execute! @cdb/db) - first)) - new-path))))) + (->> category + category-parents + (map #(:categories/name %)) + (cstr/join " > "))) |