Skip to content

Commit

Permalink
add fetch one reservation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
macielti committed Nov 16, 2024
1 parent df60d05 commit 4876418
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/rango_graalvm/controllers/reservation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
(pool/with-connection [database-conn postgresql]
(database.reservation/by-menu menu-id database-conn)))

(s/defn fetch-reservation :- models.reservation/Reservation
[reservation-id :- s/Uuid
postgresql]
(pool/with-connection [database-conn postgresql]
(database.reservation/lookup reservation-id database-conn)))

(s/defn fetch-student-reservation-by-menu :- models.reservation/Reservation
[student-code :- s/Str
menu-id :- s/Uuid
Expand Down
9 changes: 9 additions & 0 deletions src/rango_graalvm/db/postgresql/reservation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
{:params [menu-id]})
(->> (map adapters.reservation/postgresql->internal))))

(s/defn lookup :- (s/maybe models.reservation/Reservation)
[reservation-id :- s/Uuid
database-conn]
(some-> (pg/execute database-conn
"SELECT * FROM reservations WHERE id = $1"
{:params [reservation-id]})
first
adapters.reservation/postgresql->internal))

(s/defn lookup-by-student-and-menu :- (s/maybe models.reservation/Reservation)
[student-id :- s/Uuid
menu-id :- s/Uuid
Expand Down
5 changes: 5 additions & 0 deletions src/rango_graalvm/diplomat/http_server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
(common-traceability/http-with-correlation-id diplomat.http-server.reservation/retract-reservation!)]
:route-name :retract-reservation]

["/api/reservations/:reservation-id"
:get [interceptors.reservation/reservation-resource-existence-interceptor-check
(common-traceability/http-with-correlation-id diplomat.http-server.reservation/fetch-reservation)]
:route-name :fetch-one-reservation]

["/api/reservations/menus/:menu-id"
:get [interceptors.menu/menu-resource-existence-interceptor-check
(common-traceability/http-with-correlation-id diplomat.http-server.reservation/fetch-reservations-by-menu)]
Expand Down
7 changes: 7 additions & 0 deletions src/rango_graalvm/diplomat/http_server/reservation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
:body {:reservations (->> (controllers.reservation/fetch-by-menu (UUID/fromString menu-id) postgresql)
(map adapters.reservation/internal->wire))}})

(s/defn fetch-reservation
[{{:keys [reservation-id]} :path-params
{:keys [postgresql]} :components}]
{:status 200
:body {:reservation (->> (controllers.reservation/fetch-reservation (UUID/fromString reservation-id) postgresql)
adapters.reservation/internal->wire)}})

(s/defn retract-reservation!
[{{:keys [reservation-id]} :path-params
{:keys [postgresql]} :components}]
Expand Down
8 changes: 8 additions & 0 deletions test/integration/aux/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@
:get (str "/api/reservation-by-student-and-menu?student-code=" student-code "&menu-id=" menu-id))]
{:status status
:body (json/decode body true)}))

(defn fetch-one-reservation
[reservation-id
service-fn]
(let [{:keys [body status]} (test/response-for service-fn
:get (str "/api/reservations/" reservation-id))]
{:status status
:body (json/decode body true)}))
36 changes: 35 additions & 1 deletion test/integration/reservation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,44 @@
(testing "Reservation was created"
(is (clj-uuid/uuid-string? reservation-id)))

(testing "Retract reservation"
(testing "Fetch reservation"
(is (match? {:status 200
:body {:reservation {:id clj-uuid/uuid-string?
:menu-id menu-id}}}
(http/fetch-student-reservation-by-menu student-access-code menu-id service-fn))))

(ig/halt! system)))


(s/deftest fetch-one-reservation-test
(let [system (ig/init components/config-test)
service-fn (-> system ::component.service/service :io.pedestal.http/service-fn)
token (-> (http/authenticate-admin {:customer {:username "admin" :password "da3bf409"}} service-fn)
:body :token)
{student-access-code :code} (-> {:student fixtures.student/wire-in-student}
(http/create-student token service-fn)
:body :student)
{menu-id :id} (-> (http/create-menu {:menu fixtures.menu/wire-in-menu} token service-fn) :body :menu)
{reservation-id :id} (-> (http/create-reservation {:reservation {:student-code student-access-code
:menu-id menu-id}} service-fn)
:body :reservation)]

(testing "Admin is authenticated"
(is (string? token)))

(testing "Student was created"
(is (string? student-access-code)))

(testing "Menu was created"
(is (clj-uuid/uuid-string? menu-id)))

(testing "Reservation was created"
(is (clj-uuid/uuid-string? reservation-id)))

(testing "Fetch one reservation"
(is (match? {:status 200
:body {:reservation {:id clj-uuid/uuid-string?
:menu-id menu-id}}}
(http/fetch-one-reservation reservation-id service-fn))))

(ig/halt! system)))

0 comments on commit 4876418

Please sign in to comment.