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