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