Skip to content

Commit

Permalink
implement online venues API
Browse files Browse the repository at this point in the history
  • Loading branch information
lagenorhynque committed Feb 11, 2018
1 parent 484caec commit 6b96915
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![CircleCI](https://circleci.com/gh/lagenorhynque/situated-program-challenge/tree/clj-version1.svg?style=svg)](https://circleci.com/gh/lagenorhynque/situated-program-challenge/tree/clj-version1)
[![CircleCI](https://circleci.com/gh/lagenorhynque/situated-program-challenge/tree/clj-version2.svg?style=svg)](https://circleci.com/gh/lagenorhynque/situated-program-challenge/tree/clj-version2)

---

Expand Down
7 changes: 6 additions & 1 deletion rest-server/resources/rest_server/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
{:get [:meetup/fetch ^int group-id ^int meetup-id]}}
"/venues"
{:get [:venue/list ^int group-id]
[:post {venue :body-params}] [:venue/create ^int group-id venue]}}}}
[:post {venue :body-params}] [:venue/create ^int group-id venue]}
"/online-venues"
{:get [:online-venue/list ^int group-id]
[:post {venue :body-params}] [:online-venue/create ^int group-id venue]}}}}

:rest-server.handler.meetup/list {:db #ig/ref :duct.database/sql}
:rest-server.handler.meetup/create {:db #ig/ref :duct.database/sql}
Expand All @@ -40,6 +43,8 @@
:rest-server.handler.member/fetch {:db #ig/ref :duct.database/sql}
:rest-server.handler.venue/list {:db #ig/ref :duct.database/sql}
:rest-server.handler.venue/create {:db #ig/ref :duct.database/sql}
:rest-server.handler.online-venue/list {:db #ig/ref :duct.database/sql}
:rest-server.handler.online-venue/create {:db #ig/ref :duct.database/sql}
:rest-server.handler.group/list {:db #ig/ref :duct.database/sql}
:rest-server.handler.group/create {:db #ig/ref :duct.database/sql}
:rest-server.handler.group/join {:db #ig/ref :duct.database/sql}}
13 changes: 8 additions & 5 deletions rest-server/src/rest_server/boundary/db/venue.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
(ns rest-server.boundary.db.venue
(:require [duct.database.sql]
[honeysql.core :as sql]
[rest-server.boundary.db.core :as db]))
[rest-server.boundary.db.core :as db]
[rest-server.util :as util]))

(defprotocol Venues
(list-venues [db group-id])
(list-venues [db group-id venue-type])
(create-venue [db venue])
(fetch-venue [db venue-id]))

(extend-protocol Venues
duct.database.sql.Boundary
(list-venues [db group-id]
(list-venues [db group-id venue-type]
(db/select db (sql/build :select :*
:from :venues
:where [:= :group_id group-id])))
:where [:and
[:= :group_id group-id]
[:= :venue_type (util/kw->pgenum venue-type)]])))
(create-venue [db venue]
(db/insert! db :venues venue))
(db/insert! db :venues (update venue :venue-type util/kw->pgenum)))
(fetch-venue [db venue-id]
(db/select-one db (sql/build :select :*
:from :venues
Expand Down
5 changes: 4 additions & 1 deletion rest-server/src/rest_server/handler/group.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[rest-server.boundary.db.venue :as db.venue]
[rest-server.handler.meetup :as meetup]
[rest-server.handler.member :as member]
[rest-server.handler.online-venue :as online-venue]
[rest-server.handler.venue :as venue]
[rest-server.util :as util]))

Expand All @@ -17,10 +18,12 @@

(defn fetch-group-detail [db {:keys [id] :as group}]
(let [admins (db.group/fetch-group-admin-members db id)
venues (db.venue/list-venues db id)
venues (db.venue/list-venues db id :venue-type/physical)
online-venues (db.venue/list-venues db id :venue-type/online)
meetups (db.meetup/list-meetups db id)]
(assoc (group-with-admin group admins)
:venues (map venue/venue-with-address venues)
:online-venues (map online-venue/online-venue-with-id online-venues)
:meetups (map (partial meetup/fetch-meetup-detail db) meetups))))

(defmethod ig/init-key ::list [_ {:keys [db]}]
Expand Down
9 changes: 6 additions & 3 deletions rest-server/src/rest_server/handler/meetup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
[rest-server.boundary.db.meetup :as db.meetup]
[rest-server.boundary.db.venue :as db.venue]
[rest-server.handler.member :as member]
[rest-server.handler.online-venue :as online-venue]
[rest-server.handler.venue :as venue]
[rest-server.util :as util]))

(defn meetup-with-venue-and-members [{:keys [id title start-at end-at]} venue members]
(defn meetup-with-venue-and-members [{:keys [id title start-at end-at]} venue online-venue members]
{:event-id id
:title title
:start-at start-at
:end-at end-at
:venue (venue/venue-with-address venue)
:online-venue (online-venue/online-venue-with-id online-venue)
:members (map member/member-with-id members)})

(defn fetch-meetup-detail [db {:keys [id venue-id] :as meetup}]
(defn fetch-meetup-detail [db {:keys [id venue-id online-venue-id] :as meetup}]
(let [venue (db.venue/fetch-venue db venue-id)
online-venue (db.venue/fetch-venue db online-venue-id)
members (db.meetup/fetch-meetup-members db id)]
(meetup-with-venue-and-members meetup venue members)))
(meetup-with-venue-and-members meetup venue online-venue members)))

(defn get-meetup [db meetup-id]
(when-let [meetup (db.meetup/fetch-meetup db meetup-id)]
Expand Down
28 changes: 28 additions & 0 deletions rest-server/src/rest_server/handler/online_venue.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns rest-server.handler.online-venue
(:require [ataraxy.response :as response]
[integrant.core :as ig]
[rest-server.boundary.db.venue :as db.venue]))

(defn online-venue-with-id [{:keys [id name url] :as online-venue}]
(when online-venue
{:online-venue-id id
:venue-name name
:url url}))

(defmethod ig/init-key ::list [_ {:keys [db]}]
(fn [{[_ group-id] :ataraxy/result}]
[::response/ok (map online-venue-with-id
(db.venue/list-venues db
group-id
:venue-type/online))]))

(defmethod ig/init-key ::create [_ {:keys [db]}]
(fn [{[_ group-id {:keys [venue-name url]}] :ataraxy/result}]
(let [venue {:name venue-name
:group-id group-id
:url url
:venue-type :venue-type/online}
id (db.venue/create-venue db venue)]
[::response/ok (-> venue
(assoc :id id)
online-venue-with-id)])))
24 changes: 14 additions & 10 deletions rest-server/src/rest_server/handler/venue.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
[integrant.core :as ig]
[rest-server.boundary.db.venue :as db.venue]))

(defn venue-with-address [{:keys [id name postal-code prefecture city street1 street2]}]
{:venue-id id
:venue-name name
:address {:postal-code postal-code
:prefecture prefecture
:city city
:address1 street1
:address2 street2}})
(defn venue-with-address [{:keys [id name postal-code prefecture city street1 street2] :as venue}]
(when venue
{:venue-id id
:venue-name name
:address {:postal-code postal-code
:prefecture prefecture
:city city
:address1 street1
:address2 street2}}))

(defmethod ig/init-key ::list [_ {:keys [db]}]
(fn [{[_ group-id] :ataraxy/result}]
[::response/ok (map venue-with-address
(db.venue/list-venues db group-id))]))
(db.venue/list-venues db
group-id
:venue-type/physical))]))

(defmethod ig/init-key ::create [_ {:keys [db]}]
(fn [{[_ group-id {:keys [address] :as venue}] :ataraxy/result}]
Expand All @@ -25,7 +28,8 @@
:city (:city address)
:street1 (:address1 address)
:street2 (:address2 address)
:group-id group-id}
:group-id group-id
:venue-type :venue-type/physical}
id (db.venue/create-venue db venue')]
[::response/ok (-> venue'
(assoc :id id)
Expand Down
13 changes: 11 additions & 2 deletions rest-server/src/rest_server/util.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
(ns rest-server.util
(:require [camel-snake-kebab.core :refer [->kebab-case ->snake_case]]
[camel-snake-kebab.extras :refer [transform-keys]])
[camel-snake-kebab.extras :refer [transform-keys]]
[clojure.string :as str])
(:import (java.sql Timestamp)
(java.time Instant)))
(java.time Instant)
(org.postgresql.util PGobject)))

(defn transform-keys-to-kebab [m]
(transform-keys #(->kebab-case % :separator \_) m))

(defn transform-keys-to-snake [m]
(transform-keys #(->snake_case % :separator \-) m))

(defn kw->pgenum [enum-kw]
(doto (PGobject.)
(.setType (-> enum-kw
namespace
(str/replace \- \_)))
(.setValue (name enum-kw))))

(defn string->timestamp [s]
(some-> s
Instant/parse
Expand Down
4 changes: 2 additions & 2 deletions rest-server/test/rest_server/handler/group_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
:email "y.tsushima@uranohoshi.ac.jp"}]}
group-id))
rest-server.boundary.db.venue/Venues
(list-venues [_ group-id]
(list-venues [_ group-id _]
(get {1 [{:id 1
:name "Tokyo Dome"
:postal-code "112-0004"
Expand Down Expand Up @@ -301,7 +301,7 @@
:last-name "Kurosawa"
:email "d.kurosawa@uranohoshi.ac.jp"}])
rest-server.boundary.db.venue/Venues
(list-venues [_ _]
(list-venues [_ _ _]
[{:id 2
:name "Yokohama Arena"
:postal-code "222-0033"
Expand Down

0 comments on commit 6b96915

Please sign in to comment.