blob: 3afa029a3b2e25d21f97ec090d8b05fd9248e7e3 (
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
|
(ns chef.frontend.visitor.search
(:require [chef.utils :as cutils]
[clojure.string :as cstr]
[hiccup2.core :as html]
[ring.util.response :as ruresp]
[chef.logic.categories :as clcategories]
[chef.logic.recipes :as clrecipes]))
(defn render [query category]
[:table {:style {:margin "1em"}}
[:tr
[:th "Rezept"]
[:th "Kategorie"]]
(for [recipe (clrecipes/get-all-recipes)
:let [recipe-category (clcategories/get-category (:recipes/category recipe))]]
(when (or (= category -1)
(and (cstr/includes? (-> recipe
:recipes/title
cstr/lower-case)
query)
(some #(= (:categories/id %) category)
(clcategories/get-parents recipe-category))))
[:tr
[:td
[:b [:a {:href (str "/recipes/" (:recipes/id recipe))} (:recipes/title recipe)]]]
[:td
(-> (:recipes/category recipe)
clcategories/get-category
clcategories/generate-path)]]))])
(defn handler [req]
(if-let [query (get-in req [:params "query"])]
(-> (render query (or (cutils/s->int-or-nil (get-in req [:params "category"])) -1))
html/html
str
ruresp/response)
(ruresp/bad-request "No search query provide.")))
|