From 78948fd017adee3154f3fcfa8b22f44c18223b94 Mon Sep 17 00:00:00 2001 From: Benjamin Sigonneau Date: Sat, 31 Jan 2026 02:18:26 +0100 Subject: [PATCH] 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. --- src/yohoho/core.clj | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/yohoho/core.clj b/src/yohoho/core.clj index 8e573bd..1b50c89 100644 --- a/src/yohoho/core.clj +++ b/src/yohoho/core.clj @@ -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