Skip to content

Instantly share code, notes, and snippets.

@siscia
Last active August 29, 2015 14:01
Show Gist options
  • Save siscia/173c76cf168d67386f7e to your computer and use it in GitHub Desktop.
Save siscia/173c76cf168d67386f7e to your computer and use it in GitHub Desktop.
(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