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