-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussions.clj
73 lines (63 loc) · 3.07 KB
/
discussions.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
(ns clorum-core.discussions
(:refer-clojure :exclude [get])
(:require [clojure.java.jdbc :as jdbc]
[java-jdbc.sql :as sql]
[clorum-core.util.security :as security]
[clorum-core.util.sanitization :as sanitize]
[clorum-core.users :as users-model]))
(defn all
"Returns all rows in the discussions table."
[db]
(jdbc/query db
(sql/select * :discussions)))
(defn get
"Returns the discussion with the specified id."
[db id]
(first (jdbc/query db
(sql/select * :discussions (sql/where {:id (sanitize/parse-int id)})))))
(defn get-replies
"Returns all rows in the replies table with the specified parent id."
[db parent]
(jdbc/query db
(sql/select * :replies (sql/where {:parent (sanitize/parse-int parent)}))))
(defn get-recent
"Returns the newest n discussions."
[db n]
(jdbc/query db
[(clojure.string/join ["SELECT * FROM discussions ORDER BY id DESC LIMIT " n])]))
(defn create
"Inserts a new discussion with the passed parameters, sanitizing blank author and category fields."
[db params]
(def db-user (users-model/get-by-name db [(:author params)]))
(if db-user
(def verified? (security/encrypt-verify (:password params) (:password db-user)))
(def verified? false))
(jdbc/insert! db :discussions (merge (apply dissoc params [:password])
{:author (sanitize/author (:author params))
:category (sanitize/category (:category params))
:verified verified?}))
;; Rough way of getting the inserted thread's ID. Should probably also check time.
(last (jdbc/query db
(sql/select :id :discussions (sql/where {:author (sanitize/author (:author params))
:category (sanitize/category (:category params))
:verified verified?})))))
(defn create-reply
"Inserts a new reply with the passed parameters, sanitizing blank author fields."
[db params]
(def db-user (users-model/get-by-name db [(:author params)]))
(if db-user
(def verified? (security/encrypt-verify (:password params) (:password db-user)))
(def verified? false))
(jdbc/insert! db :replies (merge (apply dissoc params [:password])
{:author (sanitize/author (:author params))
:parent (sanitize/parse-int (:parent params))
:verified verified?})))
(defn save
"Updates the discussion with the specified id with the passed parameters."
[db id params]
(jdbc/update! db :discussions params (sql/where {:id (sanitize/parse-int id)})))
(defn delete
"Deletes the discussion with the specified id along with its child replies."
[db id]
(jdbc/delete! db :discussions (sql/where {:id (sanitize/parse-int id)}))
(jdbc/delete! db :replies (sql/where {:parent (sanitize/parse-int id)})))