Last active
August 29, 2015 14:01
-
-
Save siscia/173c76cf168d67386f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defresource get-user | |
:allowed-methods #{:get} | |
:available-media-types ["application/json"] | |
:malformed? (fn [ctx] | |
(let [q-params (get-in ctx [:request :query-params])] | |
(if-let [email (get q-params "email")] | |
[false {:query | |
{:email email}}] | |
(if-let [username (get q-params "username")] | |
[false {:query | |
{:username username}}] | |
[true {:malformed {:message "You need to provide or the email or the username"}}])))) | |
:handle-malformed (fn [ctx] | |
(generate-string (:malformed ctx))) | |
:authorized? ((:fn authorization) :user) | |
:handle-unauthorized (:handle authorization) | |
:exists? (fn [ctx] | |
(if-let [user (user/fetch (-> ctx :user :database) | |
(-> ctx :query))] | |
[true {:result user}] | |
false)) | |
:handle-not-found (fn [ctx] | |
(generate-string | |
{:message (format "User not found") | |
:query (:query ctx)})) | |
:handle-ok (fn [ctx] | |
(generate-string (-> ctx :result | |
make-entity-json-friendly) {:pretty-print true}))) | |
(fact | |
(:authorized? get-user) => ((:fn authorization) :user)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment