-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.clj
72 lines (64 loc) · 2.34 KB
/
api.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns webapp.api
(:require [webapp.html :as html]
[webapp.animals :as animals]
[webapp.books :as books]
[liberator.core :refer (resource)]
[compojure.core :refer (defroutes ANY)]
[compojure.route :refer (resources not-found)]
[ring.middleware.params :refer (wrap-params)]
[ring.middleware.edn :refer (wrap-edn-params)]
[ring.util.response :refer (redirect)]
[clj-json.core :as json]))
(defn handle-exception
[ctx]
(let [e (:exception ctx)]
(.printStackTrace e)
{:status 500 :message (.getMessage e)}))
(defroutes routes
(ANY "/animals"
[name species]
(resource
:available-media-types ["application/edn" "application/json" "text/html"]
:allowed-methods [:get :post]
:handle-ok (fn [ctx]
(let [found (animals/read)]
(condp = (-> ctx :representation :media-type)
"application/edn" found
"application/json" (json/generate-string found)
"text/html" (html/generate-string found))))
:post! (fn [ctx] {::id (animals/create! {:name name :species species})})
:post-redirect? (fn [ctx] {:location (str "/animals/" (::id ctx))})
:handle-exception handle-exception))
(ANY "/animals/:id"
[id name species]
(let [id (Integer/parseInt id)]
(resource
:available-media-types ["application/edn"]
:allowed-methods [:get :put :delete]
:handle-ok (fn [ctx]
(animals/read id))
:put! (fn [ctx]
(animals/update!
id
{:name name :species species}))
:new? false
:respond-with-entity? true
:delete! (fn [ctx] (animals/delete! id))
:handle-exception handle-exception)))
(ANY "/books"
[]
(resource
:available-media-types ["application/edn"]
:allowed-methods [:get]
:handle-ok (fn [ctx] (books/read))
:handle-exception handle-exception))
(ANY "/"
[]
(redirect "/index.html"))
(resources "/" {:root "public"})
(resources "/" {:root "/META-INF/resources"})
(not-found "404"))
(def handler
(-> routes
wrap-params
wrap-edn-params))