Skip to content

Commit 5fd7976

Browse files
committed
feat(authors): db query socials by author
1 parent 25a6688 commit 5fd7976

File tree

6 files changed

+158
-39
lines changed

6 files changed

+158
-39
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@
7777
(filter #(= (:type %) "see-also"))
7878
(map db->see-also)))
7979

80-
(defn db->social
80+
(defn db->socials
8181
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
82-
[:maybe schemas.model.social/Social]]}
82+
[:maybe [:sequential schemas.model.social/Social]]]}
8383
[db-rows]
8484
(->> db-rows
8585
(group-by :definition-id)
@@ -90,5 +90,10 @@
9090
{:social/definition-id definition-id
9191
:social/notes notes
9292
:social/examples examples
93-
:social/see-alsos see-alsos})))
94-
first))
93+
:social/see-alsos see-alsos})))))
94+
95+
(defn db->social-definition
96+
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
97+
[:maybe schemas.model.social/Social]]}
98+
[db-rows]
99+
(first (db->socials db-rows)))

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

+48-30
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,14 @@
55
[honey.sql :as sql]
66
[honey.sql.helpers :as sql.helpers]
77
[next.jdbc :as jdbc]
8-
[parenthesin.components.db.jdbc-hikari :as components.database]))
8+
[parenthesin.components.db.jdbc-hikari :as components.database]
9+
[taoensso.encore :as enc]))
910

1011
(defn ^:private execute!
1112
{:malli/schema [:=> [:cat schemas.types/DatabaseComponent :any] :any]}
1213
[db sql-params]
1314
(components.database/execute db sql-params jdbc/unqualified-snake-kebab-opts))
1415

15-
(defn upsert-author
16-
{:malli/schema [:=> [:cat schemas.model.social/NewAuthor schemas.types/DatabaseComponent]
17-
schemas.model.social/Author]}
18-
[transaction db]
19-
(->> (-> (sql.helpers/insert-into :author)
20-
(sql.helpers/values [transaction])
21-
(sql.helpers/upsert (-> (sql.helpers/on-conflict :login :account_source)
22-
(sql.helpers/do-update-set :avatar_url)))
23-
(sql.helpers/returning :*)
24-
sql/format)
25-
(execute! db)
26-
first
27-
adapters/db->author))
28-
29-
(defn get-author
30-
{:malli/schema [:=> [:cat :string schemas.model.social/account-source schemas.types/DatabaseComponent]
31-
[:maybe schemas.model.social/Author]]}
32-
[login source db]
33-
(when-let [author (->> (-> (sql.helpers/select :*)
34-
(sql.helpers/from :author)
35-
(sql.helpers/where :and
36-
[:= :login login]
37-
[:= :account_source source])
38-
sql/format)
39-
(execute! db)
40-
first)]
41-
(adapters/db->author author)))
42-
4316
(defn insert-see-also
4417
{:malli/schema [:=> [:cat schemas.model.social/NewSeeAlso schemas.types/DatabaseComponent]
4518
schemas.model.social/SeeAlso]}
@@ -259,6 +232,51 @@
259232
first
260233
adapters/db->note))
261234

235+
(defn upsert-author
236+
{:malli/schema [:=> [:cat schemas.model.social/NewAuthor schemas.types/DatabaseComponent]
237+
schemas.model.social/Author]}
238+
[transaction db]
239+
(->> (-> (sql.helpers/insert-into :author)
240+
(sql.helpers/values [transaction])
241+
(sql.helpers/upsert (-> (sql.helpers/on-conflict :login :account_source)
242+
(sql.helpers/do-update-set :avatar_url)))
243+
(sql.helpers/returning :*)
244+
sql/format)
245+
(execute! db)
246+
first
247+
adapters/db->author))
248+
249+
(defn get-author
250+
{:malli/schema [:=> [:cat :string schemas.model.social/account-source schemas.types/DatabaseComponent]
251+
[:maybe schemas.model.social/Author+Socials]]}
252+
[login source db]
253+
(when-let [author (->> (-> (sql.helpers/select :*)
254+
(sql.helpers/from :author)
255+
(sql.helpers/where :and
256+
[:= :login login]
257+
[:= :account_source source])
258+
sql/format)
259+
(execute! db)
260+
first)]
261+
(let [author-id (:author-id author)
262+
socials (->> (-> (sql.helpers/union-all
263+
(-> get-note-query
264+
(sql.helpers/where [:= :note/author-id author-id]))
265+
266+
(-> get-example-query
267+
(sql.helpers/where [:= :example-edit/author-id author-id]))
268+
269+
(-> get-see-also-query
270+
(sql.helpers/where [:= :see-also/author-id author-id])))
271+
sql/format)
272+
(execute! db)
273+
adapters/db->socials
274+
seq)]
275+
276+
(enc/assoc-some
277+
(adapters/db->author author)
278+
:author/socials socials))))
279+
262280
(defn get-by-definition
263281
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent]
264282
[:maybe schemas.model.social/Social]]}
@@ -274,4 +292,4 @@
274292
(sql.helpers/where [:= :see-also/definition-id definition-id])))
275293
sql/format)
276294
(execute! db)
277-
adapters/db->social))
295+
adapters/db->social-definition))

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

+3
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@
110110
[:social/notes [:sequential Note]]
111111
[:social/examples [:sequential Example]]
112112
[:social/see-alsos [:sequential SeeAlso]]])
113+
114+
(def Author+Socials
115+
(mu/assoc Author [:author/socials {:optional true}] [:sequential Social]))

test/integration/codes/clj/docs/backend/db/postgres_test.clj

+32-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,31 @@
2626
:cleanup util/stop-system!
2727
:fail-fast? true}
2828

29-
[database (state-flow.api/get-state :database)]
30-
31-
(util.db.postgres/upsert-author "delboni" "github")
29+
[database (state-flow.api/get-state :database)
30+
31+
; prepare db authors
32+
author-1 (util.db.postgres/upsert-author "delboni" "github")
33+
author-2 (util.db.postgres/upsert-author "not-delboni" "github")
34+
35+
; prepare socials
36+
see-also-1 (util.db.postgres/create-see-also {:see-also/author-id (:author/author-id author-1)
37+
:see-also/definition-id "clojure.core/disj"
38+
:see-also/definition-id-to "clojure.core/dissoc"})
39+
_see-also-2 (util.db.postgres/create-see-also {:see-also/author-id (:author/author-id author-2)
40+
:see-also/definition-id "clojure.core/disj"
41+
:see-also/definition-id-to "clojure.core/dissoc2"})
42+
note-1 (util.db.postgres/create-note {:note/author-id (:author/author-id author-1)
43+
:note/definition-id "clojure.core/disj"
44+
:note/body "author 1 note about this function."})
45+
_note-2 (util.db.postgres/create-note {:note/author-id (:author/author-id author-2)
46+
:note/definition-id "clojure.core/disj"
47+
:note/body "author 2 note about this function."})
48+
example-1 (util.db.postgres/create-example {:example/author-id (:author/author-id author-1)
49+
:example/definition-id "clojure.core/disj"
50+
:example/body "author 1 example about this function."})
51+
_example-2 (util.db.postgres/create-example {:example/author-id (:author/author-id author-2)
52+
:example/definition-id "clojure.core/disj"
53+
:example/body "author 2 example about this function."})]
3254

3355
(flow "upsert author with new url"
3456
(state/invoke
@@ -42,7 +64,13 @@
4264
:author/login "delboni"
4365
:author/account-source "github"
4466
:author/avatar-url "https://my.pic.com/me2.jpg"
45-
:author/created-at inst?}
67+
:author/created-at inst?
68+
:author/socials [{:social/definition-id "clojure.core/disj"
69+
:social/notes [note-1]
70+
:social/examples [(dissoc example-1
71+
:example/author-id)]
72+
:social/see-alsos [see-also-1]}]}
73+
4674
(db/get-author "delboni" "github" database))))
4775

4876
(defflow see-also-db-test

test/integration/codes/clj/docs/backend/social_test.clj

+10
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@
324324
(state-flow.server/request! {:method :get
325325
:uri (str "/api/social/example/" example-id)})))
326326

327+
(flow "should return author"
328+
(match? {:status 200
329+
:body {:author-id string?
330+
:login "delboni",
331+
:account-source "github",
332+
:avatar-url "https://my.pic/me.jpg",
333+
:created-at string?}}
334+
(state-flow.server/request! {:method :get
335+
:uri "/api/social/author/delboni/github"})))
336+
327337
(flow "delete example revision part 1"
328338
(state-flow.server/request! {:method :delete
329339
:headers {"authorization" (str "Bearer " token)}

test/unit/codes/clj/docs/backend/adapters/db/postgres_test.clj

+56-1
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,59 @@
200200
:avatar-url "https://my.pic.com/me.jpg"
201201
:created-at
202202
#inst "2020-10-23T00:00:00.000-00:00"}}]}
203-
(adapters/db->social db-rows)))))
203+
(adapters/db->social-definition db-rows)))))
204+
205+
(deftest db->author-social-test
206+
(testing "should map and get author social data for multiple definitions"
207+
(is (match? [#:social{:definition-id "clojure.core/disj"
208+
:notes [#:note{:note-id #uuid "d9564b50-98f8-4c04-a668-bd24c1241e34"
209+
:definition-id "clojure.core/disj"
210+
:body "my note about this function."
211+
:created-at #inst "2020-10-23T00:00:00.000-00:00"
212+
:author
213+
#:author{:author-id
214+
#uuid "387863e6-e32b-4d4b-8ec5-8cf4dab7e048"
215+
:login "delboni"
216+
:account-source "github"
217+
:avatar-url "https://my.pic.com/me.jpg"
218+
:created-at
219+
#inst "2020-10-23T00:00:00.000-00:00"}}]
220+
:examples []
221+
:see-alsos []}
222+
#:social{:definition-id "clojure.core/disj2"
223+
:notes [#:note{:note-id #uuid "d9564b50-98f8-4c04-a668-bd24c1241e34"
224+
:definition-id "clojure.core/disj2"
225+
:body "my note about this function 2."
226+
:created-at #inst "2020-10-23T00:00:00.000-00:00"
227+
:author
228+
#:author{:author-id
229+
#uuid "387863e6-e32b-4d4b-8ec5-8cf4dab7e048"
230+
:login "delboni"
231+
:account-source "github"
232+
:avatar-url "https://my.pic.com/me.jpg"
233+
:created-at
234+
#inst "2020-10-23T00:00:00.000-00:00"}}]
235+
:examples []
236+
:see-alsos []}]
237+
(adapters/db->socials [{:account-source "github"
238+
:avatar-url "https://my.pic.com/me.jpg"
239+
:type "note"
240+
:created #inst "2020-10-23T00:00:00.000-00:00"
241+
:login "delboni"
242+
:id #uuid "d9564b50-98f8-4c04-a668-bd24c1241e34"
243+
:author-id #uuid "387863e6-e32b-4d4b-8ec5-8cf4dab7e048"
244+
:body "my note about this function."
245+
:created-at #inst "2020-10-23T00:00:00.000-00:00"
246+
:definition-id "clojure.core/disj"}
247+
{:account-source "github"
248+
:avatar-url "https://my.pic.com/me.jpg"
249+
:type "note"
250+
:created #inst "2020-10-23T00:00:00.000-00:00"
251+
:login "delboni"
252+
:id #uuid "d9564b50-98f8-4c04-a668-bd24c1241e34"
253+
:author-id #uuid "387863e6-e32b-4d4b-8ec5-8cf4dab7e048"
254+
:body "my note about this function 2."
255+
:created-at #inst "2020-10-23T00:00:00.000-00:00"
256+
:definition-id "clojure.core/disj2"}])))))
257+
258+

0 commit comments

Comments
 (0)