Skip to content

Commit

Permalink
PUT event resource
Browse files Browse the repository at this point in the history
  • Loading branch information
anthontaylor committed Feb 23, 2017
1 parent 3a6512c commit e3bfd42
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
16 changes: 15 additions & 1 deletion src/handler/events.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
[event]
(db/insert-event event))

(defn update-event
[event]
(db/update-event event))

(def endpoints
(context "/events" []
:tags ["events"]
Expand All @@ -36,4 +40,14 @@
:summary "get's an event id"
(if-let [event (get-event id)]
(ok event)
(not-found)))))
(not-found)))

(PUT "/" request
:body [event s/ReturnEvent]
:responses { 201 {:schema s/ReturnEvent
:description "Resource Updated"}
400 {:description "Bad Request"}}
:summary "Updates an Event"
(if-let [{id :id :as returned-event} (update-event event)]
(created (str "api/event/" id) returned-event)
(bad-request "Error")))))
54 changes: 41 additions & 13 deletions src/sogeti_server/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[honeysql.core :as sql]
[honeysql.helpers :refer :all]
[environ.core :refer [env]]
[clojure.tools.trace :refer [trace]]
[clj-time.core :as t]))

(def db {:classname "com.mysql.jdbc.Driver"
Expand All @@ -20,12 +21,7 @@
(where [:= :ID user_id])
sql/format))

(defn get-event-query
[event_id]
(-> (select :*)
(from :EVENTS)
(where [:= :ID event_id])
sql/format))


(defn now
[]
Expand All @@ -46,13 +42,6 @@
(j/query db)
first))

(defn get-event
[event_id]
(->>
event_id
get-event-query
(j/query db)
first))

(defn insert-user
[user]
Expand All @@ -61,12 +50,51 @@
(j/insert! db :USER))
(get-user (:id user)))

(defn get-event-query
[event_id]
(-> (select :*)
(from :EVENTS)
(where [:= :ID event_id])
sql/format))

(defn get-event
[event_id]
(->>
event_id
get-event-query
(j/query db)
first))

(defn insert-event
[event]
(->> event
(j/insert! db :EVENTS))
(get-event (:id event)))

(defn find-event
[user_id]
(->>
(get-event-query user_id)
(j/query db)
first))

(defn update-event-query
[{id :id :as event}]
(-> (update :EVENTS)
(sset (dissoc event :id))
(where [:= :ID id])
sql/format))


(defn update-event
([event]
(update-event db event))
([conn {uid :id :as event}]
(->> event
update-event-query
(j/execute! conn))
(get-event (:id event))))

(defn truncate
[]
(j/with-db-transaction [conn db]
Expand Down
10 changes: 10 additions & 0 deletions src/sogeti_server/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@
:start_time s/Str
:end_time s/Str
:reserved s/Bool})

(s/defschema ReturnEvent
{:id Long
:title s/Str
:description s/Str
:start_time s/Str
:end_time s/Str
:reserved s/Bool})


0 comments on commit e3bfd42

Please sign in to comment.