Line data Source code
1 4 : (ns matchscore.domain.group.repository.postgres
2 : (:require
3 : [cheshire.core :as json]
4 : [honey.sql :as sql]
5 : [matchscore.domain.group.entity :as entity]
6 : [matchscore.utils.audit :as audit]
7 : [matchscore.utils.db :as utils.db]
8 : [matchscore.utils.pagination :as qh]
9 : [next.jdbc :as jdbc]))
10 :
11 :
12 4 : (defprotocol
13 : GroupPostgresRepository
14 : (find-by-id
15 : [this id]
16 : "Find group by UUID. Returns Group map or nil.")
17 : (find-by-ids
18 : [this ids]
19 : "Find groups by multiple UUIDs. Returns seq.")
20 : (find-by-draw-id
21 : [this draw-id]
22 : "Find all groups for a draw, ordered by order. Returns seq.")
23 : (find-by-draw-ids
24 : [this draw-ids]
25 : "Find all groups for multiple draws in one batch query. Returns seq.")
26 : (search
27 : [this filters pagination]
28 : "Search groups. filters: {:drawId {...}}. sort: #{:name :order}, default: :order")
29 : (insert!
30 : [this group-data]
31 : "Insert a group. Returns [true result nil] on success, [false :conflict-id nil] when uuid already exists, or [false :database-error message] on failure.")
32 : (upsert!
33 : [this group-data]
34 : "Upsert a group. Returns [success? result-or-reason error-message].")
35 : (delete!
36 : [this id]
37 : "Delete a group by UUID. Returns true."))
38 :
39 :
40 4 : (defn- filters->specs
41 : "Converts a GraphQL filters map to a seq of [column op value] specs."
42 : [{:keys [drawId]}]
43 7 : (qh/typed-filters->specs {:draw-id (utils.db/cast-uuid-filter drawId)}))
44 :
45 :
46 4 : (defn- row->group
47 : "Converts a database row to a domain Group map."
48 : [row]
49 32 : (when row (entity/make-group (utils.db/unqualify-keys row))))
50 :
51 :
52 16 : (def ^:private valid-sort-fields #{:name :order})
53 :
54 :
55 8 : (def ^:private default-sort :order)
56 :
57 :
58 4 : (def ^:private base-query
59 28 : {:select [[:g.uuid :uuid]
60 12 : [:g.public-id :public-id]
61 12 : [:g.draw-id :draw-id]
62 12 : [:g.name :name]
63 16 : [[:raw "g.\"order\""]]
64 12 : [:g.ruleset-id :ruleset-id]
65 12 : [:g.config :config]
66 12 : [:g.created-datetime :created-datetime]
67 12 : [:g.source :source]]
68 16 : :from [[:groups :g]]})
69 :
70 :
71 4 : (defrecord
72 : PostgresGroupRepository
73 : [ds]
74 : GroupPostgresRepository
75 :
76 : (find-by-id
77 : [_ id]
78 36 : (let [query (assoc base-query :where [:= :g.uuid [:cast id :uuid]])
79 3 : result (->>
80 9 : (sql/format query)
81 9 : (jdbc/execute! ds)
82 3 : first)]
83 9 : (row->group result)))
84 :
85 : (find-by-ids
86 : [_ ids]
87 4 : (if (empty? ids)
88 0 : []
89 17 : (let [query (assoc base-query :where [:in :g.uuid (mapv #(vector :cast % :uuid) ids)])]
90 1 : (->>
91 3 : (sql/format query)
92 3 : (jdbc/execute! ds)
93 2 : (mapv row->group)))))
94 :
95 : (find-by-draw-id
96 : [_ draw-id]
97 8 : (let [query (->>
98 4 : base-query
99 4 : (merge
100 40 : {:where [:= :g.draw-id [:cast draw-id :uuid]]
101 16 : :order-by [[:g/order :asc]]}))]
102 4 : (->>
103 12 : (sql/format query)
104 12 : (jdbc/execute! ds)
105 8 : (mapv row->group))))
106 :
107 : (find-by-draw-ids
108 : [_ draw-ids]
109 4 : (if (empty? draw-ids)
110 0 : []
111 2 : (let [query (->>
112 1 : base-query
113 1 : (merge
114 15 : {:where [:in :g.draw-id (mapv #(vector :cast % :uuid) draw-ids)]
115 4 : :order-by [[:g/order :asc]]}))]
116 1 : (->>
117 3 : (sql/format query)
118 3 : (jdbc/execute! ds)
119 2 : (mapv row->group)))))
120 :
121 : (search
122 : [_ filters pagination]
123 4 : (let [specs (filters->specs filters)
124 2 : query (qh/build-search-query
125 1 : base-query
126 1 : specs
127 1 : valid-sort-fields
128 1 : default-sort
129 1 : pagination
130 6 : {:sort-field-casts {:order :integer}
131 5 : :sort-col-exprs {:order [:raw "\"order\""]}})
132 1 : rows (->>
133 3 : (sql/format query)
134 3 : (jdbc/execute! ds)
135 2 : (mapv row->group))
136 6 : opts (when-not (:cursor pagination)
137 5 : (let [cnt-query (qh/build-count-query base-query specs)
138 3 : total (->>
139 3 : (sql/format cnt-query)
140 3 : (jdbc/execute-one! ds)
141 1 : utils.db/unqualify-keys
142 : :total)]
143 3 : {:total-count total}))]
144 7 : (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
145 :
146 : (insert!
147 : [_ group-data]
148 3 : (try
149 6 : (let [{:keys [uuid public-id draw-id name order ruleset-id config source]} group-data
150 24 : query {:insert-into :groups
151 42 : :values [{:uuid [:cast uuid :uuid]
152 3 : :public_id public-id
153 12 : :draw_id [:cast draw-id :uuid]
154 3 : :name name
155 15 : :order (or order 1)
156 6 : :ruleset_id (when ruleset-id [:cast ruleset-id :uuid])
157 30 : :config [:cast (json/generate-string (or config {})) :jsonb]
158 15 : :source (or source "matchscore")}]
159 6 : :on-conflict [:uuid]
160 : :do-nothing true
161 6 : :returning [:*]}
162 6 : result (->>
163 6 : (sql/format
164 9 : (audit/stamp query)
165 15 : {:quoted true
166 : :quoted-snake true})
167 9 : (jdbc/execute! ds)
168 3 : first
169 3 : row->group)]
170 18 : (if result [true result nil] [false :conflict-id nil]))
171 0 : (catch Exception e [false :database-error (.getMessage e)])))
172 :
173 : (upsert!
174 : [_ group-data]
175 2 : (try
176 4 : (let [{:keys [uuid public-id draw-id name order ruleset-id config source]} group-data
177 14 : query {:insert-into :groups
178 28 : :values [{:uuid [:cast uuid :uuid]
179 2 : :public_id public-id
180 8 : :draw_id [:cast draw-id :uuid]
181 2 : :name name
182 10 : :order (or order 1)
183 4 : :ruleset_id (when ruleset-id [:cast ruleset-id :uuid])
184 21 : :config [:cast (json/generate-string (or config {})) :jsonb]
185 10 : :source (or source "matchscore")}]
186 4 : :on-conflict [:uuid]
187 14 : :do-update-set {:fields [:name :order :ruleset_id :config]}
188 4 : :returning [:*]}
189 4 : result (->>
190 4 : (sql/format
191 6 : (audit/stamp query)
192 10 : {:quoted true
193 : :quoted-snake true})
194 6 : (jdbc/execute! ds)
195 2 : first
196 2 : row->group)]
197 8 : [true result nil])
198 0 : (catch Exception e [false :database-error (.getMessage e)])))
199 :
200 1 : (delete!
201 : [_ id]
202 5 : (let [query {:delete-from :groups
203 7 : :where [:= :uuid [:cast id :uuid]]}]
204 1 : (->>
205 3 : (sql/format query)
206 2 : (jdbc/execute! ds)))
207 : true))
208 :
209 :
210 4 : (defn make-repository
211 : "Creates a PostgresGroupRepository from a ds."
212 : [ds]
213 12 : (->PostgresGroupRepository ds))
|