blob: e2725cee1bb3ac75fa7672a1eff9703ff856491e (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
(ns chef.utils
(:require [chef.database :as cdb]
[honey.sql :as sql]
[next.jdbc :as jdbc]
[ring.util.response :as ruresp]
[clojure.string :as cstr])
(:import java.io.File))
(defn gen-page [title & content]
[:html
[:head
[:meta {:name "viewport" :content "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"}]
[:title title]
[:link {:rel :stylesheet :href "/static/style.css"}]
[:meta {:http-equiv "content-type" :content "text/html; charset=utf-8"}]
[:meta {:name "robots" :content "noindex,nofollow"}]]
(apply conj [:body] content [[:script {:src "/static/htmx.js"}]])])
(defmacro auth-only [request & body]
`(if (some? (get-in ~request [:session :oauth-token]))
(do ~@body)
~(-> (ruresp/response "Unauthorized.")
(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]
(->> category
category-parents
(map #(:categories/name %))
(cstr/join " > ")))
(defn parse-ingredients [s]
(->> s
(re-seq #"([A-z0-9 ]*)=([0-9]*) ?([A-z]*)")
(map #(hash-map :description (nth % 1)
:amount (Integer/parseInt (nth % 2))
:unit (nth % 3)))))
(defn valid-ingredients? [s]
(and (string? s)
(->> s
(re-matches #"(([A-z0-9 ]*)=([0-9]*) ?([A-z]*)\n?)*")
some?)))
(defn get-thumbnail-file [recipe]
(let [thumbnails-folder (File. "./thumbnails/")]
(->> thumbnails-folder
.listFiles
(filter #(cstr/starts-with? (.getName ^File %)
(str (:recipes/id recipe) ".")))
first)))
|