Skip to content

Commit

Permalink
clinic: add route handlers for front-end resources and wrap-params mi…
Browse files Browse the repository at this point in the history
…ddleware
  • Loading branch information
ashutoshgngwr committed Oct 13, 2023
1 parent 1712c64 commit 1a61833
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
3 changes: 1 addition & 2 deletions clinic/shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
:output-dir "resources/public/js"
:asset-path "/js"
:modules {:app {:entries [clinic.core]}}
:devtools {:after-load clinic.core/mount-root}}}
:dev-http {8000 "resources/public"}}
:devtools {:after-load clinic.core/mount-root}}}}
16 changes: 12 additions & 4 deletions clinic/src/clj/clinic/routes/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
(:require [clinic.config :as config]
[clinic.routes.patient :as patient]
[clojure.tools.logging :as log]
[compojure.core :refer [context defroutes]]
[compojure.route :refer [not-found]]
[compojure.core :refer [context defroutes GET]]
[compojure.route :refer [not-found resources]]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
[ring.middleware.params :refer [wrap-params]]
[ring.util.response :as r]))

(defroutes ^:private routes
(context "/api/v1" _
(context "/patients" _ patient/handler))
(not-found (r/status 404)))
(context "/patients" _ patient/handler)
(not-found (r/status 404)))

;; UI
(resources "/")
(GET "/*" _ (r/resource-response "index.html" {:root "public"})))

(defn- wrap-exception-handler [next-handler]
(fn exception-handler [request]
Expand All @@ -23,6 +29,8 @@
"The default API route handler."
(-> routes
(config/wrap)
(wrap-keyword-params)
(wrap-params)
(wrap-json-body {:keywords? true})
(wrap-json-response)
(wrap-exception-handler)))
25 changes: 15 additions & 10 deletions clinic/src/clj/clinic/routes/patient.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
[ring.util.response :as r]))

(defn- create-patient! [{{fhir-server-url :fhir-server-base-url} :config
params :body}]
(try
(-> (svc/create! fhir-server-url params)
(r/response)
(r/status 201))
(catch Exception e
(case (:type (ex-data e))
:invalid-params (r/status 400)
:mrn-conflict (r/status 503)
(throw e)))))
content-type :content-type
:as request}]
(let [params (case content-type
"application/x-www-form-urlencoded" (request :params)
"application/json" (request :body)
nil)]
(try
(-> (svc/create! fhir-server-url params)
(r/response)
(r/status 201))
(catch Exception e
(case (:type (ex-data e))
:invalid-params (r/status 400)
:mrn-conflict (r/status 503)
(throw e))))))

(defroutes handler
(POST "/" _ create-patient!))

0 comments on commit 1a61833

Please sign in to comment.