Line data Source code
1 4 : (ns matchscore.domain.draw.command
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.draw.entity :as entity]
6 : [matchscore.domain.draw.repository.postgres :as draw-postgres-repo]
7 : [matchscore.utils.errors :as utils.errors]))
8 :
9 :
10 4 : (defn- insert-or-fail
11 : [repo draw-data]
12 10 : (let [[ok? result error-message] (draw-postgres-repo/insert! repo draw-data)]
13 10 : (cond ok? (either/right result)
14 :
15 0 : (= result :conflict-id)
16 0 : (either/left
17 0 : (utils.errors/build-error
18 : :check
19 0 : {:code :error.draw/conflict-id
20 : :message "Draw with such id already exists"
21 0 : :context {:input-data draw-data}}))
22 :
23 : :else
24 0 : (either/left
25 0 : (utils.errors/build-error
26 : :database
27 0 : {:code :error.draw/insert-failed
28 0 : :message error-message})))))
29 :
30 :
31 4 : (defn- find-by-id-or-fail
32 : [repo draw-id]
33 9 : (if-let [existing (draw-postgres-repo/find-by-id repo draw-id)]
34 3 : (either/right existing)
35 2 : (either/left
36 3 : (utils.errors/build-error
37 : :check
38 5 : {:code :error.draw/not-found
39 : :message "Draw not found."}))))
40 :
41 :
42 4 : (defn- check-belongs-to-stage
43 : [existing stage-id]
44 6 : (if (= (:stage-id existing) stage-id)
45 3 : (either/right existing)
46 0 : (either/left
47 0 : (utils.errors/build-error
48 : :check
49 0 : {:code :error.draw/not-found
50 : :message "Draw not found."}))))
51 :
52 :
53 4 : (defn- upsert-or-fail
54 : [repo draw-data]
55 5 : (let [[ok? result error-message] (draw-postgres-repo/upsert! repo draw-data)]
56 2 : (if ok?
57 3 : (either/right result)
58 0 : (either/left
59 0 : (utils.errors/build-error
60 : :database
61 0 : {:code :error.draw/upsert-failed
62 0 : :message error-message})))))
63 :
64 :
65 4 : (defn- save-if-changed
66 : [repo existing draw-data]
67 5 : (if (entity/has-changes? existing draw-data)
68 3 : (c/fmap
69 6 : #(hash-map :code :success.draw/ok :data %)
70 7 : (upsert-or-fail repo (entity/merge-updates existing draw-data)))
71 2 : (either/right
72 4 : {:code :success.draw/no-changes
73 1 : :data existing})))
74 :
75 :
76 4 : (defn create-draw!
77 : "Creates a new draw. Validates data and checks for ID conflicts.
78 : Returns Either<Error, Draw>."
79 : [{:keys [repos]} draw-data]
80 16 : (let [repo (:draw-postgres-repo repos)] (insert-or-fail repo draw-data)))
81 :
82 :
83 4 : (defn update-draw!
84 : "Updates an existing draw. Returns Either<Error, {:code kw :data Draw}>.
85 : Validates that the draw belongs to the given stage."
86 : [{:keys [repos]} draw-data]
87 4 : (let [repo (:draw-postgres-repo repos)]
88 27 : (c/mlet [existing (find-by-id-or-fail repo (:uuid draw-data))
89 7 : existing (check-belongs-to-stage existing (:stage-id draw-data))
90 5 : result (save-if-changed repo existing draw-data)]
91 3 : (c/return result))))
92 :
93 :
94 4 : (defn delete-draw!
95 : "Deletes a draw by id. Returns Either<Error, true>.
96 : Validates that the draw belongs to the given stage."
97 : [{:keys [repos]} id stage-id]
98 4 : (let [repo (:draw-postgres-repo repos)]
99 18 : (c/mlet [existing (find-by-id-or-fail repo id)
100 4 : _ (check-belongs-to-stage existing stage-id)]
101 6 : (either/right (draw-postgres-repo/delete! repo id)))))
|