Skip to content

Commit

Permalink
feat: adapters and get-all fn
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldelboni committed Aug 15, 2023
1 parent c02efb5 commit b6c8d5c
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 329 deletions.
114 changes: 75 additions & 39 deletions src/codes/clj/docs/backend/adapters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,79 @@
(date->localdatetime (ZoneId/of "UTC"))
(.format (DateTimeFormatter/ofPattern str-format))))

;(defn wire->usd-price
;{:malli/schema [:=> [:cat schemas.wire-out/CoinDeskResponse] number?]}
;[wire]
;(-> wire
;(get-in [:bpi :USD :rate_float])
;bigdec))

;(defn ^:private wire-in->db
;{:malli/schema [:=> [:cat :uuid number? pos?] schemas.db/WalletTransaction]}
;[id btc usd]
;{:wallet/id id
;:wallet/btc_amount btc
;:wallet/usd_amount_at usd})

;(defn deposit->db
;{:malli/schema [:=> [:cat :uuid pos? pos?] schemas.db/WalletTransaction]}
;[id btc usd]
;(wire-in->db id btc usd))

;(defn withdrawal->db
;{:malli/schema [:=> [:cat :uuid neg? pos?] schemas.db/WalletTransaction]}
;[id btc usd]
;(wire-in->db id btc usd))

;(defn db->wire-in
;{:malli/schema [:=> [:cat schemas.db/WalletEntry] schemas.wire-in/WalletEntry]}
;[{:wallet/keys [id btc_amount usd_amount_at created_at]}]
;{:id id
;:btc-amount (bigdec btc_amount)
;:usd-amount-at (bigdec usd_amount_at)
;:created-at created_at})

;(defn ->wallet-history
;{:malli/schema [:=> [:cat pos? [:vector schemas.db/WalletEntry]] schemas.wire-in/WalletHistory]}
;[current-usd-price wallet-entries]
;(let [total-btc (reduce #(+ (:wallet/btc_amount %2) %1) 0M wallet-entries)]
;{:entries (mapv db->wire-in wallet-entries)
;:total-btc (bigdec total-btc)
;:total-current-usd (bigdec (* current-usd-price total-btc))}))
; TODO: schema & test
(defn db->author
[{:keys [author-id login account-source avatar-url created-at]}]
{:author/author-id author-id
:author/login login
:author/account-source account-source
:author/avatar-url avatar-url
:author/created-at created-at})

; TODO: schema & test
(defn db->note
[{:keys [id definition-id body created] :as note}]
{:note/note-id id
:note/definition-id definition-id
:note/body body
:note/created-at created
:note/author (db->author note)})

; TODO: schema & test
(defn db->notes
[db-rows]
(->> db-rows
(filter #(= (:type %) "note"))
(map db->note)))

; TODO: schema & test
(defn db->example
[{:keys [id definition-id body created] :as example}
editors]
{:example/example-id id
:example/definition-id definition-id
:example/body body
:example/created-at created
:example/author (db->author example)
:example/editors editors})

; TODO: schema & test
(defn db->examples
[db-rows]
(->> db-rows
(filter #(= (:type %) "example"))
(group-by :id)
(map (fn [[_ examples]]
(let [sorted-examples (sort-by :created examples)
editors (map db->author sorted-examples)
example (last sorted-examples)]
(db->example example editors))))))

; TODO: schema & test
(defn db->see-also
[{:keys [id definition-id body created] :as see-also}]
{:see-also/see-also-id id
:see-also/definition-id definition-id
:see-also/definition-id-to body
:see-also/created-at created
:see-also/author (db->author see-also)})

; TODO: schema & test
(defn db->see-alsos
[db-rows]
(->> db-rows
(filter #(= (:type %) "see-also"))
(map db->see-also)))

; TODO: schema
(defn db->definitions [db-rows]
(->> db-rows
(group-by :definition-id)
(map (fn [[definition-id items]]
(let [notes (db->notes items)
examples (db->examples items)
see-alsos (db->see-alsos items)]
{:definition/definition-id definition-id
:definition/notes notes
:definition/examples examples
:definition/see-alsos see-alsos})))))
112 changes: 9 additions & 103 deletions src/codes/clj/docs/backend/db.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns codes.clj.docs.backend.db
(:require [codes.clj.docs.backend.schemas.db :as schemas.db]
(:require [codes.clj.docs.backend.adapters :as adapters]
[codes.clj.docs.backend.schemas.db :as schemas.db]
[codes.clj.docs.backend.schemas.types :as schemas.types]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]
Expand Down Expand Up @@ -45,35 +46,6 @@
(execute! db)
first))

; TODO: move to adapter & schema & test
(defn db->see-alsos
[db-data]
(map (fn [{:author/keys [author-id login account-source avatar-url]
:see-also/keys [see-also-id definition-id definition-id-to created-at]
:as see-also}]
#:see-also{:see-also-id see-also-id
:definition-id definition-id
:definition-id-to definition-id-to
:created-at created-at
:author {:author/author-id author-id
:author/login login
:author/account-source account-source
:author/avatar-url avatar-url
:author/created-at (:author/created-at see-also)}})
db-data))

(defn get-see-alsos
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent] [:sequential schemas.db/SeeAlso]]}
[definition-id db]
(->> (-> (sql.helpers/select :*)
(sql.helpers/from :see-also)
(sql.helpers/join :author
[:= :see-also/author-id :author/author-id])
(sql.helpers/where [:= :definition-id definition-id])
sql/format)
(execute! db)
db->see-alsos))

(defn insert-example
{:malli/schema [:=> [:cat schemas.db/NewExample schemas.types/DatabaseComponent] :any]}
[transaction db]
Expand Down Expand Up @@ -105,44 +77,6 @@
(execute! db)
first))

; TODO: move to adapter & schema & test
(defn db->examples
[db-data]
(map (fn [{:author/keys [author-id login account-source avatar-url]
:example/keys [example-id definition-id]
:example-edit/keys [body created-at]
:as example}]
#:example{:example-id example-id
:definition-id definition-id
:body body
:created-at created-at
:author {:author/author-id author-id
:author/login login
:author/account-source account-source
:author/avatar-url avatar-url
:author/created-at (:author/created-at example)}})
db-data))

(defn get-examples
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent] [:sequential schemas.db/Example]]}
[definition-id db]
(->> (-> (sql.helpers/select-distinct-on [:example/created-at :example/example-id]
:example/*
:example-edit/*
:author/*)
(sql.helpers/from :example)
(sql.helpers/join :example-edit
[:= :example/example-id :example-edit/example-id])
(sql.helpers/join :author
[:= :example-edit/author-id :author/author-id])
(sql.helpers/where [:= :example/definition-id definition-id])
(sql.helpers/order-by [:example/created-at :asc])
(sql.helpers/order-by [:example/example-id])
(sql.helpers/order-by [:example-edit/created-at :desc])
sql/format)
(execute! db)
db->examples))

(defn insert-note
{:malli/schema [:=> [:cat schemas.db/NewNote schemas.types/DatabaseComponent] :any]}
[transaction db]
Expand All @@ -164,36 +98,7 @@
(execute! db)
first))

; TODO: move to adapter & schema & test
(defn db->notes
[db-data]
(map (fn [{:author/keys [author-id login account-source avatar-url]
:note/keys [note-id definition-id body created-at updated-at]
:as note}]
#:note{:note-id note-id
:definition-id definition-id
:body body
:created-at created-at
:updated-at updated-at
:author {:author/author-id author-id
:author/login login
:author/account-source account-source
:author/avatar-url avatar-url
:author/created-at (:author/created-at note)}})
db-data))

(defn get-notes
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent] [:sequential schemas.db/Note]]}
[definition-id db]
(->> (-> (sql.helpers/select :*)
(sql.helpers/from :note)
(sql.helpers/join :author
[:= :note/author-id :author/author-id])
(sql.helpers/where [:= :definition-id definition-id])
sql/format)
(execute! db)
db->notes))

; TODO schema
(defn get-all
[definition-id db]
(->> (-> (sql.helpers/union-all
Expand All @@ -202,7 +107,7 @@
["note" :type]
:note/definition-id
:note/body
:note/created-at
[:note/created-at :created]
:author-note/*)
(sql.helpers/from :note)
(sql.helpers/join [:author :author-note]
Expand All @@ -214,7 +119,7 @@
["example" :type]
:example/definition-id
:example-edit/body
:example-edit/created-at
[:example-edit/created-at :created]
:author-example-edit/*)
(sql.helpers/from :example)
(sql.helpers/join :example-edit
Expand All @@ -227,12 +132,13 @@
[:see-also/see-also-id :id]
["see-also" :type]
:see-also/definition-id
:see-also/definition-id-to
:see-also/created-at
[:see-also/definition-id-to :body]
[:see-also/created-at :created]
:author-see-also/*)
(sql.helpers/from :see-also)
(sql.helpers/join [:author :author-see-also]
[:= :see-also/author-id :author-see-also/author-id])
(sql.helpers/where [:= :see-also/definition-id definition-id])))
sql/format)
(execute! db)))
(execute! db)
adapters/db->definitions))
Loading

0 comments on commit b6c8d5c

Please sign in to comment.