Skip to content

Commit

Permalink
let activity creation can attach an activity image
Browse files Browse the repository at this point in the history
  • Loading branch information
humorless committed Aug 12, 2024
1 parent 56279f0 commit 2b1dab8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/co/gaiwan/compass/html/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@
([params]
[:<>
[:h2 "Create Activity"]
[:form {:method "POST" :action "/sessions"}
[:form {:method "POST" :action "/sessions"
:enctype "multipart/form-data"}
[:label {:for "title"} "Title"]
[:input {:id "title" :name "title" :type "text"
:required true :min-length 2}]
Expand Down
6 changes: 2 additions & 4 deletions src/co/gaiwan/compass/routes/profiles.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
[io.pedestal.log :as log]
[java-time.api :as time]))

(def upload-dir "uploads")

(defn wrap-authentication [handler]
(fn [req]
(log/trace ::login-check (:identity req))
Expand Down Expand Up @@ -46,7 +44,7 @@
[{:keys [params identity] :as req}]
(let [{:keys [filename tempfile] :as image} (:image params)
file-id (str (:db/id identity))
filepath (str upload-dir "/" file-id "_" filename)
filepath (str util/upload-dir "/" file-id "_" filename)
{:keys [tempids]} @(db/transact [(merge
{:user/image-path filepath}
(params->profile-data params))])]
Expand All @@ -57,7 +55,7 @@
{:flash "Successfully Saved!"})))

(defn file-handler [req]
(let [file (io/file "uploads" (get-in req [:path-params :filename]))]
(let [file (io/file util/upload-dir (get-in req [:path-params :filename]))]
(if (.exists file)
(response/file-response (.getPath file))
(response/not-found "File not found"))))
Expand Down
15 changes: 12 additions & 3 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"
(:require
[clojure.string :as str]
[clojure.java.io :as io]
[co.gaiwan.compass.db :as db]
[co.gaiwan.compass.db.queries :as q]
[co.gaiwan.compass.html.sessions :as session-html]
Expand Down Expand Up @@ -74,7 +75,7 @@
(= published? "on")
(assoc :session/published? true))))

(defn save-session
(defn POST-save-session
"Save session to Datomic
The typical params is:
Expand All @@ -87,6 +88,13 @@
:published? \"on\"}"
[{:keys [params]}]
(let [{:keys [tempids]} @(db/transact [(params->session-data params)])]
(when (:image params)
(let [{:keys [filename tempfile]} (:image params)
session-eid (get tempids "session")
file-path (str util/upload-dir "/" session-eid "_" filename)]
(io/copy tempfile (io/file file-path))
@(db/transact [{:db/id (get tempids "session")
:session/image (str util/upload-dir "/" (get tempids "session") "_" filename)}])))
(util/redirect ["/sessions" (get tempids "session")]
{:flash "Successfully created!"})))

Expand Down Expand Up @@ -114,13 +122,14 @@
capacity (:session/capacity session)
signup-cnt (:session/signup-count 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]])
(session-updated-response session-eid))
(< (or signup-cnt 0) capacity)
;; user participates the session
(do
;;TODO: add try/catch to handle :db/cas
@(db/transact [[:db/cas session-eid :session/signup-count signup-cnt ((fnil inc 0) signup-cnt)]
[:db/add session-eid :session/participants user-id]])
(session-updated-response session-eid))
Expand All @@ -146,7 +155,7 @@
[""
{:name :activity/save
:get {:handler GET-sessions}
:post {:handler save-session}}]
:post {:handler POST-save-session}}]
["/new"
{:get {:handler GET-session-new}}]
["/:id"
Expand Down
2 changes: 2 additions & 0 deletions src/co/gaiwan/compass/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
(:import
(java.time Instant ZonedDateTime ZoneId)))

(def upload-dir "uploads")

(defn datafy-instant
"Output:
```
Expand Down

0 comments on commit 2b1dab8

Please sign in to comment.