Skip to content

Commit

Permalink
Get rid of signup-count, it leads to inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Sep 3, 2024
1 parent e070412 commit ec7f833
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
23 changes: 16 additions & 7 deletions src/co/gaiwan/compass/db/queries.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
(:require
[co.gaiwan.compass.db :as db]))

#_(set! *print-namespace-maps* false)

(defn session [id]
(let [e (db/entity id)]
(-> (into {} e)
(update :session/type db/entity)
(update :session/location db/entity)
(assoc :session/signup-count (count (:session/participants e))))))

(defn all-sessions
[]
(sort-by
:session/time
(db/q
'[:find
[(pull ?e [* {:session/type [*]
:session/location [*]}]) ...]
:where
[?e :session/title]]
(db/db))))
(map session
(db/q
'[:find
[?e ...]
:where
[?e :session/title]]
(db/db)))))

(defn all-users []
(sort-by
Expand Down
1 change: 0 additions & 1 deletion src/co/gaiwan/compass/db/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
[:session/location :ref "Where does the session take place"]
[:session/image :string "Image URL, either absolute, or relative to compass root"]
[:session/capacity :long "Number of people that are able to join this session"]
[:session/signup-count :long "Number of people that are currently signing up this session"]
[:session/ticket-required? :boolean "If this session requires a ticket"]
[:session/published? :boolean "If this session is published/visible?"]
[:session/participants :ref "reference points to the user" :many]
Expand Down
12 changes: 4 additions & 8 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
:session/type (keyword "session.type" type)
:session/location (keyword "location.type" location)
:session/organized (parse-long organizer-id)
:session/signup-count 0
:session/capacity (parse-long capacity)}
(= ticket-required? "on")
(assoc :session/ticket-required? true)
Expand Down Expand Up @@ -141,21 +140,18 @@
session-eid (parse-long (get-in req [:path-params :id]))
session-seletor '[* {:session/type [*]
:session/location [*]}]
pull-session #(db/pull session-seletor session-eid)
session (pull-session)
session (q/session session-eid)
capacity (:session/capacity session)
signup-cnt (:session/signup-count session)]
signup-cnt (count (:session/participants session))]
(cond
;; user leaves the session
(session/participating? session user)
(do @(db/transact [[:db/cas session-eid :session/signup-count signup-cnt (dec signup-cnt)]
[:db/retract session-eid :session/participants user-id]])
(do @(db/transact [[:db/retract session-eid :session/participants user-id]])
(session-updated-response session-eid))
(< (or signup-cnt 0) capacity)
;; user participates the session
(do
@(db/transact [[:db/cas session-eid :session/signup-count signup-cnt ((fnil inc 0) signup-cnt)]
[:db/add session-eid :session/participants user-id]])
@(db/transact [[:db/add session-eid :session/participants user-id]])
(session-updated-response session-eid))
:else
(session-unchanged-response session-eid))))
Expand Down

0 comments on commit ec7f833

Please sign in to comment.