blob: e428a23d79f432955d18f26bd301bd36b618e9f9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
(ns chef.components.search
(: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 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 (try (Integer/parseInt (get-in req [:params "category"]))
(catch Exception _ -1)))
html/html
str
ruresp/response)
(ruresp/bad-request "No search query provide.")))
|