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/pages | |
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/pages')
-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 |
4 files changed, 80 insertions, 28 deletions
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)) |