|
5 | 5 | [honey.sql :as sql]
|
6 | 6 | [honey.sql.helpers :as sql.helpers]
|
7 | 7 | [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])) |
9 | 10 |
|
10 | 11 | (defn ^:private execute!
|
11 | 12 | {:malli/schema [:=> [:cat schemas.types/DatabaseComponent :any] :any]}
|
12 | 13 | [db sql-params]
|
13 | 14 | (components.database/execute db sql-params jdbc/unqualified-snake-kebab-opts))
|
14 | 15 |
|
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 |
| - |
43 | 16 | (defn insert-see-also
|
44 | 17 | {:malli/schema [:=> [:cat schemas.model.social/NewSeeAlso schemas.types/DatabaseComponent]
|
45 | 18 | schemas.model.social/SeeAlso]}
|
|
259 | 232 | first
|
260 | 233 | adapters/db->note))
|
261 | 234 |
|
| 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 | + |
262 | 280 | (defn get-by-definition
|
263 | 281 | {:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent]
|
264 | 282 | [:maybe schemas.model.social/Social]]}
|
|
274 | 292 | (sql.helpers/where [:= :see-also/definition-id definition-id])))
|
275 | 293 | sql/format)
|
276 | 294 | (execute! db)
|
277 |
| - adapters/db->social)) |
| 295 | + adapters/db->social-definition)) |
0 commit comments