Skip to content

Commit 8875bb1

Browse files
Merge pull request #39 from clj-codes/feat/adds-social-dashboards-api
Adds api for latest & top author interactions
2 parents 9236b36 + 14fd876 commit 8875bb1

File tree

15 files changed

+364
-5
lines changed

15 files changed

+364
-5
lines changed

docker/docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.7"
2-
31
services:
42
db:
53
image: postgres:14

src/codes/clj/docs/backend/adapters/db/postgres.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
:author/avatar-url avatar-url
1313
:author/created-at created-at})
1414

15+
(defn db->author+interaction
16+
{:malli/schema [:=> [:cat [:sequential schemas.db/Author+InteractionsRow]]
17+
[:sequential schemas.model.social/Author+Interactions]]}
18+
[db-rows]
19+
(map (fn [{:keys [interactions] :as author}]
20+
(assoc (db->author author)
21+
:author/interactions interactions))
22+
db-rows))
23+
1524
(defn db->note
1625
{:malli/schema [:=> [:cat [:maybe schemas.db/Row]] [:maybe schemas.model.social/Note]]}
1726
[{:keys [id definition-id body created author-id] :as note}]
@@ -97,3 +106,15 @@
97106
[:maybe schemas.model.social/Social]]}
98107
[db-rows]
99108
(first (db->socials db-rows)))
109+
110+
(defn db->any-socials
111+
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
112+
[:maybe [:sequential schemas.model.social/AnySocial]]]}
113+
[db-rows]
114+
(let [notes (db->notes db-rows)
115+
examples (db->examples db-rows)
116+
see-alsos (db->see-alsos db-rows)]
117+
(concat
118+
(seq notes)
119+
(seq examples)
120+
(seq see-alsos))))

src/codes/clj/docs/backend/adapters/social.clj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
:avatar-url avatar-url
3434
:created-at created-at})
3535

36+
(defn author-interaction->model->wire
37+
{:malli/schema [:=> [:cat [:sequential schemas.model.social/Author+Interactions]]
38+
[:sequential schemas.wire.out.social/Author+Interactions]]}
39+
[author+interactions]
40+
(->> author+interactions
41+
(map (fn [{:author/keys [interactions] :as author}]
42+
(assoc (author->model->wire author)
43+
:interactions interactions)))
44+
(sort-by :interactions #(compare %2 %1))))
45+
3646
(defn editor->model->wire
3747
{:malli/schema [:=> [:cat schemas.model.social/Editor] schemas.wire.social/Editor]}
3848
[{:editor/keys [edited-at] :as author}]
@@ -129,3 +139,22 @@
129139
(enc/assoc-some
130140
(author->model->wire author)
131141
:socials (map social->model->wire socials)))
142+
143+
(defn any-social->model->wire
144+
{:malli/schema [:=> [:cat [:sequential schemas.model.social/AnySocial]]
145+
[:maybe [:sequential schemas.wire.out.social/AnySocial]]]}
146+
[any-socials]
147+
(let [notes (->> any-socials
148+
(filter #(:note/note-id %))
149+
(map note->model->wire))
150+
examples (->> any-socials
151+
(filter #(:example/example-id %))
152+
(map example->model->wire))
153+
see-alsos (->> any-socials
154+
(filter #(:see-also/see-also-id %))
155+
(map see-also->model->wire))]
156+
(->> (concat
157+
(seq notes)
158+
(seq examples)
159+
(seq see-alsos))
160+
(sort-by :created-at #(compare %2 %1)))))

src/codes/clj/docs/backend/controllers/social.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,15 @@
8686
[:maybe schemas.model.social/Social]]}
8787
[definition-id {:keys [database]}]
8888
(db.postgres/get-by-definition definition-id database))
89+
90+
(defn get-top-authors
91+
{:malli/schema [:=> [:cat :int schemas.types/Components]
92+
[:maybe [:sequential schemas.model.social/Author+Interactions]]]}
93+
[limit {:keys [database]}]
94+
(db.postgres/get-top-authors limit database))
95+
96+
(defn get-latest-interactions
97+
{:malli/schema [:=> [:cat :int schemas.types/Components]
98+
[:maybe [:sequential schemas.model.social/AnySocial]]]}
99+
[limit {:keys [database]}]
100+
(db.postgres/get-latest-interactions limit database))

src/codes/clj/docs/backend/db/postgres.clj

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,46 @@
293293
sql/format)
294294
(execute! db)
295295
adapters/db->social-definition))
296+
297+
(defn get-top-authors
298+
{:malli/schema [:=> [:cat :int schemas.types/DatabaseComponent]
299+
[:maybe [:sequential schemas.model.social/Author+Interactions]]]}
300+
[limit db]
301+
(->> (-> (sql.helpers/select
302+
:author/*
303+
[[:count :social/id] :interactions])
304+
(sql.helpers/from [(sql.helpers/union-all
305+
(-> (sql.helpers/select
306+
[:note/note-id :id]
307+
[:note/author-id :author-id])
308+
(sql.helpers/from :note))
309+
(-> (sql.helpers/select
310+
[:example-edit/example-edit-id :id]
311+
[:example-edit/author-id :author-id])
312+
(sql.helpers/from :example-edit))
313+
(-> (sql.helpers/select
314+
[:see-also/see-also-id :id]
315+
[:see-also/author-id :author-id])
316+
(sql.helpers/from :see-also))) :social])
317+
(sql.helpers/join :author
318+
[:= :social/author-id :author/author-id])
319+
(sql.helpers/group-by :author/author-id)
320+
(sql.helpers/order-by [:interactions :desc])
321+
(sql.helpers/limit limit)
322+
sql/format)
323+
(execute! db)
324+
adapters/db->author+interaction))
325+
326+
(defn get-latest-interactions
327+
{:malli/schema [:=> [:cat :int schemas.types/DatabaseComponent]
328+
[:maybe [:sequential schemas.model.social/AnySocial]]]}
329+
[limit db]
330+
(->> (-> (sql.helpers/union-all
331+
get-note-query
332+
get-example-query
333+
get-see-also-query)
334+
(sql.helpers/order-by [:created :desc])
335+
(sql.helpers/limit limit)
336+
sql/format)
337+
(execute! db)
338+
adapters/db->any-socials))

src/codes/clj/docs/backend/ports/http_in/social.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,19 @@
155155
:notes []
156156
:examples []
157157
:see-alsos []})})
158+
159+
(defn get-top-authors
160+
[{{{:keys [limit]} :query} :parameters
161+
components :components}]
162+
{:status 200
163+
:body (-> (or limit 10)
164+
(controllers.social/get-top-authors components)
165+
adapters.social/author-interaction->model->wire)})
166+
167+
(defn get-latest-interactions
168+
[{{{:keys [limit]} :query} :parameters
169+
components :components}]
170+
{:status 200
171+
:body (-> (or limit 10)
172+
(controllers.social/get-latest-interactions components)
173+
adapters.social/any-social->model->wire)})

src/codes/clj/docs/backend/routes.clj

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,24 @@
169169
:responses {200 {:body schemas.wire.out.social/Social}
170170
404 {:body :string}
171171
500 {:body :string}}
172-
:handler ports.http-in.social/get-by-definition}}]]]
172+
:handler ports.http-in.social/get-by-definition}}]]
173+
174+
["/query"
175+
["/top-authors"
176+
{:get {:summary "get top contributing author list"
177+
:parameters {:query [:map [:limit {:optional true} :int]]}
178+
:responses {200 {:body [:sequential schemas.wire.out.social/Author+Interactions]}
179+
404 {:body :string}
180+
500 {:body :string}}
181+
:handler ports.http-in.social/get-top-authors}}]
182+
183+
["/latest-interactions"
184+
{:get {:summary "get latest social interactions list"
185+
:parameters {:query [:map [:limit {:optional true} :int]]}
186+
:responses {200 {:body [:sequential schemas.wire.out.social/AnySocial]}
187+
404 {:body :string}
188+
500 {:body :string}}
189+
:handler ports.http-in.social/get-latest-interactions}}]]]
173190

174191
["/document"
175192
{:swagger {:tags ["document"]}}

src/codes/clj/docs/backend/schemas/db/postgres.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444
:account-source
4545
:avatar-url
4646
:created-at]))
47+
48+
(def Author+InteractionsRow
49+
(mu/assoc AuthorRow :interactions :int))

src/codes/clj/docs/backend/schemas/model/social.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,11 @@
111111
[:social/examples [:sequential Example]]
112112
[:social/see-alsos [:sequential SeeAlso]]])
113113

114+
(def AnySocial
115+
[:or Example Note SeeAlso])
116+
114117
(def Author+Socials
115118
(mu/assoc Author [:author/socials {:optional true}] [:sequential Social]))
119+
120+
(def Author+Interactions
121+
(mu/assoc Author :author/interactions :int))

src/codes/clj/docs/backend/schemas/wire/out/social.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@
3333
[:examples [:sequential Example]]
3434
[:see-alsos [:sequential SeeAlso]]])
3535

36+
(def AnySocial
37+
[:or Example Note SeeAlso])
38+
3639
(def Author+Socials
3740
(mu/assoc Author [:socials {:optional true}] [:sequential Social]))
41+
42+
(def Author+Interactions
43+
(mu/assoc Author :interactions :int))

0 commit comments

Comments
 (0)