(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-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 " > ")))