Skip to content

Instantly share code, notes, and snippets.

@WorldsEndless
Last active June 11, 2019 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WorldsEndless/8419a0da48ff43b6cf58cae7ebae55da to your computer and use it in GitHub Desktop.
Save WorldsEndless/8419a0da48ff43b6cf58cae7ebae55da to your computer and use it in GitHub Desktop.
Reitit for auto-CRUD
;; CRUD routes are automatically generated for each of CREATE, READ, UPDATE, and DELETE fns
;; that exist (with those names) in the `humhelp.db.<table-name>` name-spaces. Note that READ
;; fns have multiple arities. With 0 args, they read all; with 1 arg, they read entry `id`
(ns humhelp.routes.admin
(:require [humhelp.layout :as layout]
[humhelp.middleware :as middleware]
[humhelp.db.user-groups :as user_groups]
[humhelp.db.attachments :as attachments]
[humhelp.db.categories :as categories]
;[humhelp.db.category-links :as category_links]
[humhelp.db.clients :as clients]
[humhelp.db.computers :as computers]
[humhelp.db.core :as core]
[humhelp.db.departments :as departments]
[humhelp.db.devices :as devices]
[humhelp.db.flaglinks :as flag_links]
[humhelp.db.flags :as flags]
;[humhelp.db.grouplinks :as group_links]
[humhelp.db.history :as histories]
[humhelp.db.jobs :as jobs]
[humhelp.db.knowledge-base :as knowledge_base]
[humhelp.db.log :as logs]
[humhelp.db.memoes :as memoes]
[humhelp.db.migratus :as migratus]
[humhelp.db.permission-links :as permission_links]
[humhelp.db.permissions :as permissions]
[humhelp.db.pictures :as pictures]
[humhelp.db.queries :as queries]
[humhelp.db.searches :as searches]
[humhelp.db.stats :as stats]
[humhelp.db.techs :as techs]
[humhelp.db.users :as users]
[humhelp.db.work :as works]
[ring.util.http-response :as response]))
#_(def ^{:private true} admin-paths
[
"/"
"/profile"
"/new-csr"
"/update-csr"
"/delete-csr"
"/csr-view"
"/all-csrs"
"/client"
"/device"
"/user"
"/memoes"
])
;;;;;;;;;;;;;;;;;;
;; DB QUERY FNS ;;
;;;;;;;;;;;;;;;;;;
(defn get-db-fn
"Get the `fn-name` (symbol) from the right `ns-name`"
[fn-name ns-name]
(-> (symbol (str "humhelp.db." ns-name)) ns-publics (get fn-name)))
(def db-create-fn
"Get the CREATE function from the given `ns-name` string"
(partial get-db-fn 'CREATE))
(def db-read-fn
"Get the READ function from the given `ns-name` string"
(partial get-db-fn 'READ))
(def db-update-fn
"Get the UPDATE function from the given `ns-name` string"
(partial get-db-fn 'UPDATE))
(def db-delete-fn
"Get the DELETE function from the given `ns-name` string"
(partial get-db-fn 'DELETE))
(def db-clone-fn
"Get the CLONE function from the given `ns-name` string"
(partial get-db-fn 'CLONE))
(defn respond-read-f
"Directly calls the rest api read function on the specified table [ns-name] and calls the response function on the results of that read and checks whether the call was valid. The id value in it, is optional"
[ns-name]
(let [f (db-read-fn ns-name)]
(fn [{{:keys [id]} :params}]
(response/ok (f (Integer/parseInt id))))))
(defn respond-read-f-page
"Directly calls the rest api read function on the specified table [ns-name] and calls the response function on the results of that read and checks whether the call was valid. The id value in it, is optional"
[ns-name]
(let [f (db-read-fn ns-name)]
(fn [{{:keys [idmin idmax]} :params}]
(response/ok (f (Integer/parseInt idmin) (Integer/parseInt idmax))))))
(defn respond-create-f
"Directly calls the rest api insert function on the specified table [ns-name] and calls the response function on the results of that insert and checks whether the call was valid. Needs a value map to insert"
[ns-name]
(let [f (db-create-fn ns-name)]
(fn [{{:keys [valmap]} :params}] ;[{{:keys [id]} :params}]
(response/ok (f valmap)))))
(defn respond-update-f
"Directly calls the rest api patch function on the specified table [ns-name] and calls the response function on the results of that patch and checks whether the call was valid. The id value in it is mandatory and will cause the call to fail without it"
[ns-name]
(let [f (db-update-fn ns-name)]
(fn [{{:keys [id valmap]} :params}]
(response/ok (f id valmap)))))
(defn respond-delete-f
"Directly calls the rest api delete function on the specified table [ns-name] and calls the response function on the results of that delete and checks whether the call was valid. The id value in it is mandatory and will cause the call to fail without it"
[ns-name]
(let [f (db-delete-fn ns-name)]
(fn [{{:keys [id]} :params}]
(response/ok (f id)))))
(defn crud-routes
"Looping through all namespaces, we create a dynamic function call on all db namespaces. Assumes that they all contain the READ, CREATE, UPDATE, and DELETE partial functions in them"
[& _]
(into ["/admin"]
(for [ns-name ["attachments" "categories" "clients" "computers" "core" "departments" "devices" "flags" "history" "jobs" "log" "memoes" "permissions" "pictures" "searches" "techs" "users" "work"]]
[(str "/" ns-name) {:get (respond-read-f ns-name)
:post (respond-create-f ns-name)
:patch (respond-update-f ns-name)
:delete (respond-delete-f ns-name)}])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment