diff --git a/repl-sessions/db.clj b/repl-sessions/db.clj index 7b409b2..10d8854 100644 --- a/repl-sessions/db.clj +++ b/repl-sessions/db.clj @@ -18,20 +18,30 @@ (def req {:identity "ccc" :path-params {:id "17592186045455"}}) -(def session (merge {:session/capacity 14} - (db/entity (parse-long (get-in req [:path-params :id]))))) + +(def session-eid 17592186045455) + +@(db/transact [[:db/add session-eid :session/capacity 14] + [:db/add session-eid :session/signup 0]]) + +(def session (db/entity (parse-long (get-in req [:path-params :id])))) (let [user-id-str (:identity req) session-eid (parse-long (get-in req [:path-params :id])) ;; session (db/entity session-eid) capacity (:session/capacity session) - curr-participants (:session/participants session)] + curr-participants (:session/participants session) + signup (:session/signup session) + new-signup (inc signup)] ;;TODO ;; Write some code to handle the case that :db/cas throws exception at race condition (prn :session-eid session-eid) (prn :capacity capacity) (prn :curr-ps curr-participants) - (prn :check (< (count curr-participants) capacity)) - (if (< (count curr-participants) capacity) - @(db/transact [[:db/add session-eid :session/participants user-id-str]]) + (prn :check (< signup capacity)) + (prn :debug-tx [[:db/cas session-eid :session/signup signup new-signup] + [:db/add session-eid :session/participants user-id-str]]) + (if (< signup capacity) + @(db/transact [[:db/cas session-eid :session/signup signup new-signup] + [:db/add session-eid :session/participants user-id-str]]) {:html/body "No enough capacity for this session"})) diff --git a/src/co/gaiwan/compass/routes/sessions.clj b/src/co/gaiwan/compass/routes/sessions.clj index 0dad023..5ca98fb 100644 --- a/src/co/gaiwan/compass/routes/sessions.clj +++ b/src/co/gaiwan/compass/routes/sessions.clj @@ -74,10 +74,13 @@ session-eid (parse-long (get-in req [:path-params :id])) session (db/entity session-eid) capacity (:session/capacity session) - current-participants (:session/participants session)] - (if (< (count current-participants) capacity) + signup (:session/signup session) + new-signup (inc signup)] + (if (< signup capacity) (do - @(db/transact [[:db/add session-eid :session/participants user-id-str]]) + ;;TODO: add try/catch to handle :db/cas + @(db/transact [[:db/cas session-eid :session/signup signup new-signup] + [:db/add session-eid :session/participants user-id-str]]) {:html/body "success"}) {:html/body "No enough capacity for this session"})) {:html/body (pr-str (db/entity (parse-long (get-in req [:path-params :id]))))})))