summaryrefslogtreecommitdiff
path: root/src/chef/utils.clj
blob: 1ea51363add61588fa66fd39ece0a1d7f537272f (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
(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]))

(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"}]]
   (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-path [category]
  (cstr/join " > "
             (loop [path (list)
                    category category]
               (let [new-path (conj path (:categories/name category))]
                 (if (some? (:categories/parent category))
                   (recur new-path
                          (->> {:select [:*]
                                :from   [:categories]
                                :where  [:= :id (:categories/parent category)]}
                               sql/format
                               (jdbc/execute! @cdb/db)
                               first))
                   new-path)))))