Add GET /item/:id endpoint
This new endpoint was so easy to add, it would have been a shame not to do it. Besides, it makes sense since the reponse from `POST /items` provided a `Location` header for the newly created item.
This commit is contained in:
@@ -64,6 +64,14 @@
|
||||
{:status 200
|
||||
:body "Ahoy mate, the ship be sailin' alright"})
|
||||
|
||||
(defn get-item-handler
|
||||
[request]
|
||||
(let [id (get-in request [:path-params :id])
|
||||
item (db-get-item id)]
|
||||
(cond
|
||||
(nil? item) {:status 404 :body {:error "Not Found"}}
|
||||
:else {:status 200 :body item})))
|
||||
|
||||
(defn get-items-handler
|
||||
[_]
|
||||
{:status 200
|
||||
@@ -100,10 +108,11 @@
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
[["/ahoy" {:get ahoy-handler}]
|
||||
["/items" {:get get-items-handler
|
||||
:post {:handler create-item-handler
|
||||
:parameters {:body Item}}}]]
|
||||
[["/ahoy" {:get ahoy-handler}]
|
||||
["/item/:id" {:get get-item-handler}]
|
||||
["/items" {:get get-items-handler
|
||||
:post {:handler create-item-handler
|
||||
:parameters {:body Item}}}]]
|
||||
;; Middlewares:
|
||||
;; - wrap-content-type-json: ensure POST routes are sent JSON payload
|
||||
;; - muuntaja middleware to automatically decode JSON
|
||||
|
||||
Reference in New Issue
Block a user