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