/
notes.clj
63 lines (57 loc) · 2.08 KB
/
notes.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
(ns clojuredocs.internal-api.notes
(:require [clojuredocs.internal-api.common :as c]
[schema.core :as s]
[somnium.congomongo :as mon]
[slingshot.slingshot :refer [throw+]]))
(defn body-not-empty [m]
(when (empty? (:body m))
{:message "Whoops, looks like your note is empty."}))
(def Note
{:body s/Str
:var c/Var
:author c/User
:created-at s/Int
:updated-at s/Int
:_id org.bson.types.ObjectId})
(defn post-note-handler [{:keys [edn-body user]}]
(c/require-login! user)
(let [new-note (-> edn-body
(assoc :_id (org.bson.types.ObjectId.))
c/update-timestamps
(assoc :author user))]
(c/validate! new-note [body-not-empty])
(c/validate-schema! new-note Note)
(mon/insert! :notes new-note)
{:status 200
:body (assoc new-note :can-edit? true :can-delete? true)}))
(defn patch-note-handler [id]
(fn [{:keys [edn-body user]}]
(c/require-login! user)
(let [_id (c/parse-mongo-id! id)
note (mon/fetch-one :notes :where {:_id _id})
new-note (-> note
(assoc :body (:body edn-body))
c/update-timestamps)]
(c/validate! new-note [body-not-empty])
(c/validate-schema! new-note Note)
(mon/update! :notes {:_id (:_id note)} new-note)
{:status 200
:body (assoc new-note :can-edit? true :can-delete? true)})))
(defn is-author [user]
(fn [m]
(when-not (= (select-keys user [:login :account-source])
(select-keys (:author m) [:login :account-source]))
{:message "You must be the author of a note to delete it."})))
(defn delete-note-handler [id]
(fn [{:keys [user]}]
(c/require-login! user)
(let [_id (c/parse-mongo-id! id)
note (mon/fetch-one :notes :where {:_id _id})]
(when-not note
(throw+
{:status 404
:body {:message (str "Note with id " id " not found.")}}))
(c/validate! note [(is-author user)])
(mon/destroy! :notes {:_id _id})
{:status 200
:body {:message "Note deleted"}})))