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