-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
user.clj
87 lines (77 loc) · 3.19 KB
/
user.clj
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;; copyright (c) 2019-2023 Sean Corfield, all rights reserved
(ns usermanager.controllers.user
"The main controller for the user management portion of this app."
(:require [ring.util.response :as resp]
[selmer.parser :as tmpl]
[usermanager.model.user-manager :as model]))
(def ^:private changes
"Count the number of changes (since the last reload)."
(atom 0))
(defn render-page
"Each handler function here adds :application/view to the request
data to indicate which view file they want displayed. This allows
us to put the rendering logic in one place instead of repeating it
for every handler."
[req]
(let [data (assoc (:params req) :changes @changes)
view (:application/view req "default")
html (tmpl/render-file (str "views/user/" view ".html") data)]
(-> (resp/response (tmpl/render-file "layouts/default.html"
(assoc data :body [:safe html])))
(resp/content-type "text/html"))))
(defn reset-changes
[req]
(reset! changes 0)
(assoc-in req [:params :message] "The change tracker has been reset."))
(defn default
[req]
(assoc-in req [:params :message]
(str "Welcome to the User Manager application demo! "
"This uses just Compojure, Ring, and Selmer.")))
(defn delete-by-id
"Compojure has already coerced the :id parameter to an int."
[req]
(swap! changes inc)
(model/delete-user-by-id (-> req :application/component :database)
(get-in req [:params :id]))
(resp/redirect "/user/list"))
(defn edit
"Display the add/edit form.
If the :id parameter is present, Compojure will have coerced it to an
int and we can use it to populate the edit form by loading that user's
data from the addressbook."
[req]
(let [db (-> req :application/component :database)
user (when-let [id (get-in req [:params :id])]
(model/get-user-by-id db id))]
(-> req
(update :params assoc
:user user
:departments (model/get-departments db))
(assoc :application/view "form"))))
(defn get-users
"Render the list view with all the users in the addressbook."
[req]
(let [users (model/get-users (-> req :application/component :database))]
(-> req
(assoc-in [:params :users] users)
(assoc :application/view "list"))))
(defn save
"This works for saving new users as well as updating existing users, by
delegatin to the model, and either passing nil for :addressbook/id or
the numeric value that was passed to the edit form."
[req]
(swap! changes inc)
(-> req
:params
;; get just the form fields we care about:
(select-keys [:id :first_name :last_name :email :department_id])
;; convert form fields to numeric:
(update :id #(some-> % not-empty Long/parseLong))
(update :department_id #(some-> % not-empty Long/parseLong))
;; qualify their names for domain model:
(->> (reduce-kv (fn [m k v] (assoc! m (keyword "addressbook" (name k)) v))
(transient {}))
(persistent!)
(model/save-user (-> req :application/component :database))))
(resp/redirect "/user/list"))