Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
snippetlist/src/clj/net/thegeez/snippetlist/users.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (43 sloc)
1.94 KB
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
(ns net.thegeez.snippetlist.users | |
(:require [clojure.java.jdbc :as jdbc] | |
[io.pedestal.interceptor :as interceptor] | |
[net.thegeez.w3a.context :as context] | |
[net.thegeez.w3a.link :as link])) | |
(defn user-resource [context data] | |
(let [{:keys [id]} data] | |
(-> data | |
(dissoc :id) | |
(assoc-in [:links :self] (link/link context :users/show :params {:id id})) | |
(update-in [:snippets] (fn [snippets] | |
(map #(link/link context :snippets/show :params {:id (:id %)}) snippets)))))) | |
(defn get-users [context] | |
(->> (jdbc/query (:database context) ["SELECT id, username, created_at, updated_at FROM users"]) | |
(map (fn [user] | |
(assoc user :snippets | |
(jdbc/query (:database context) ["SELECT id FROM snippets WHERE owner=?" (:id user)])))) | |
(map (partial user-resource context)))) | |
(def index | |
(interceptor/interceptor | |
{:enter (fn [context] | |
(merge context | |
{:response | |
{:status 200 | |
:data {:users (get-users context)}}}))})) | |
(defn get-user [db id] | |
(when-let [user (first (jdbc/query db ["SELECT id, username, created_at, updated_at FROM users WHERE id = ?" id]))] | |
(assoc user :snippets (jdbc/query db ["SELECT id FROM snippets WHERE owner = ?" (:id user)])))) | |
(def with-user | |
(interceptor/interceptor | |
{:enter (fn [context] | |
(let [id (get-in context [:request :path-params :id])] | |
(if-let [user (get-user (:database context) id)] | |
(assoc context :user (user-resource context user)) | |
(context/terminate context 404))))})) | |
(def show | |
(interceptor/interceptor | |
{:enter (fn [context] | |
(merge context | |
{:response | |
{:status 200 | |
:data {:user (get-in context [:user]) | |
:links {:home (link/link context :home)}}}}))})) |