From ec7f83339f63d630e1f82244a9ef69bdb9e2762b Mon Sep 17 00:00:00 2001 From: Arne Brasseur Date: Tue, 3 Sep 2024 10:44:32 +0200 Subject: [PATCH] Get rid of signup-count, it leads to inconsistencies --- src/co/gaiwan/compass/db/queries.clj | 23 ++++++++++++++++------- src/co/gaiwan/compass/db/schema.clj | 1 - src/co/gaiwan/compass/routes/sessions.clj | 12 ++++-------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/co/gaiwan/compass/db/queries.clj b/src/co/gaiwan/compass/db/queries.clj index 5c48e8c..27c7b0d 100644 --- a/src/co/gaiwan/compass/db/queries.clj +++ b/src/co/gaiwan/compass/db/queries.clj @@ -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 diff --git a/src/co/gaiwan/compass/db/schema.clj b/src/co/gaiwan/compass/db/schema.clj index e04a41c..b195614 100644 --- a/src/co/gaiwan/compass/db/schema.clj +++ b/src/co/gaiwan/compass/db/schema.clj @@ -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] diff --git a/src/co/gaiwan/compass/routes/sessions.clj b/src/co/gaiwan/compass/routes/sessions.clj index 8f0a0ca..03c9053 100644 --- a/src/co/gaiwan/compass/routes/sessions.clj +++ b/src/co/gaiwan/compass/routes/sessions.clj @@ -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) @@ -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))))