Created
February 2, 2010 20:13
-
-
Save ordnungswidrig/292982 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
(def products-resource | |
(resource | |
:method-allowed? #(some #{(% :request-method)} [:get :post]) | |
:content-types-provided { "text/html" :to_html, "text/plain" :to_text } | |
:created (fn [_ req _] (str "Product " (add-product (slurp-body req)) " created.")) | |
:to_html (fn [_ req _] | |
(html [:html | |
[:head [:title "All Products"]] | |
[:body [:h1 "All Products"] | |
[:ul (map (fn [p] [:li [:a { :href (p :id)} (p :title)]]) | |
(trace "AP" (all-products)))]]])) | |
:to_text (fn [_ req _] | |
(apply str (map #(str (% :id) ": " (% :title) "\n") (all-products)))))) | |
(def product-resource | |
(resource | |
:method-allowed? #(some #{(% :request-method)} [:get :delete :put ]) | |
:content-types-provided { "text/html" :to_html, "text/plain" :to_text } | |
:exists? (fn [req] (if-let [id (trace "id" (read-string (-> req :route-params :id)))] | |
(if-let [product (trace "product" (product-by-id id))] | |
{ ::product product }))) | |
:etag (fn [req] (str "P-" (-> req ::product :id))) | |
:delete (fn [req] (remove-product-by-id (-> req :route-params :id))) | |
:entity? false | |
:to_html (fn [rmap req status] | |
(let [product (req ::product)] | |
(html [:h1 (product :id)] [:p (product :title)]))) | |
:to_text (fn [rmap req status] | |
(let [product (req ::product)] | |
(str (product :id) ": " (product :title)))))) | |
(defroutes my-app | |
(ANY "/products/" products-resource) | |
(ANY "/products/:id" product-resource) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment