summaryrefslogtreecommitdiff
path: root/src/chef/components
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-05-31 17:03:51 +0200
committerTim <contact@bytim.eu>2025-05-31 17:03:51 +0200
commitffb5d0b740e3fa23143ad89dab29a44d5b0acd34 (patch)
tree1c4ff1d69bbf81a0a3dfe7c2c2bf1f5cd3e867ad /src/chef/components
parentea7b0078478ba7925f2db3cb1fa038e8a3d85ab8 (diff)
downloadchef-ffb5d0b740e3fa23143ad89dab29a44d5b0acd34.tar.xz
chef-ffb5d0b740e3fa23143ad89dab29a44d5b0acd34.zip
Add real data to home page + add search logic + add recipe pages
Diffstat (limited to 'src/chef/components')
-rw-r--r--src/chef/components/search.clj43
1 files changed, 39 insertions, 4 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)