-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.clj
115 lines (103 loc) · 5.36 KB
/
articles.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(ns kaleidoscope.clj.api.articles
(:require [kaleidoscope.clj.persistence.rdbms :as rdbms]
[kaleidoscope.clj.utils.core :as utils]
[next.jdbc :as next]
[taoensso.timbre :as log]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Articles
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def get-articles
(rdbms/make-finder :articles))
(defn create-article!
[db {:keys [article-url article-title] :as article}]
(log/infof "Creating Article: %s" article)
(let [now (utils/now)
result (rdbms/insert! db
:articles (cond-> (assoc article
:created-at now
:modified-at now)
(nil? article-title) (assoc :article-title article-url))
:ex-subtype :UnableToCreateArticle)]
(log/infof "Created Article: %s" result)
result))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Branches
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def get-branches
(rdbms/make-finder :full-branches))
(defn create-branch!
[db {:keys [article-id author branch-name] :as article-branch}]
(log/infof "Creating branch: %s" article-branch)
(next/with-transaction [tx db]
(let [now (utils/now)
[{article-id :id :as article}] (if article-id
(get-articles tx {:id article-id})
(create-article! tx (select-keys article-branch [:author :article-url :article-tags :hostname :article-title])))
[{branch-id :id :as branch}] (rdbms/insert! tx
:article-branches {:branch-name branch-name
:article-id article-id
:created-at now
:modified-at now}
:ex-subtype :UnableToCreateArticleBranch)
result (get-branches tx {:branch-id branch-id})]
(log/infof "Created Article Branch: %s" result)
result)))
(defn publish-branch!
([db branch-id]
(publish-branch! db branch-id (utils/now)))
([db branch-id now]
(log/infof "Publishing Branch: %s" branch-id)
(let [result (rdbms/update! db :article-branches
{:published-at now}
[:= :id branch-id]
:ex-subtype :UnableToPublishBranch)
result (get-branches db {:branch-id branch-id})]
(log/infof "Published Branch: %s" result)
result)))
(defn unpublish-branch!
[db branch-id]
(log/infof "Unpublishing Branch: %s" branch-id)
(let [result (rdbms/update! db :article-branches
{:published-at nil}
[:= :id branch-id]
:ex-subtype :UnableToUnPublishBranch)
result (get-branches db {:branch-id branch-id})]
(log/infof "Unpublished Branch: %s" result)
result))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Versions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def get-versions
(rdbms/make-finder :full-versions))
(defn create-version!
[db {:keys [branch-id] :as article-branch} {:keys [created-at] :as article-version}]
(let [branch-id (or branch-id (get-in (get-branches db article-branch)
[0 :branch-id]))
now (or created-at (utils/now))
[{version-id :id :as version}] (rdbms/insert! db
:article-versions (assoc article-version
:branch-id branch-id
:created-at now
:modified-at now)
:ex-subtype :UnableToCreateArticleBranch)
result (get-versions db {:version-id version-id})]
(log/infof "Created Article version: %s" result)
result))
(defn published?
[branch]
(:published-at branch))
(defn new-version!
[db article-branch article-version]
(let [existing-branch (first (get-branches db (select-keys article-branch [:article-url :hostname])))]
(if (published? existing-branch)
(throw (ex-info "Cannot change a published branch" existing-branch))
(next/with-transaction [tx db]
(when-not existing-branch
(create-branch! tx article-branch))
(let [[branch] (get-branches tx (select-keys article-branch [:article-url :hostname]))]
(create-version! tx branch article-version))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Published articles
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def get-published-articles
(rdbms/make-finder :published-articles))