-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtodo.clj
45 lines (37 loc) · 1.31 KB
/
todo.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
(ns com.example.todo.resources.todo
(:require [clojure.tools.logging :as log])
(:import [com.example.todo.representations Todo]
[com.codahale.metrics.annotation Timed]
[javax.validation Valid]
[javax.ws.rs GET POST DELETE Path Consumes Produces PathParam]))
(definterface ITodo
(get [])
(delete [])
(add [^Long id ^com.example.todo.representations.Todo todo])
(get [^Long id])
(toggle [^Long id])
(delete [^Long id]))
(deftype ^{Path "/todo"
Consumes ["application/json"]
Produces ["application/json"]}
TodoResource [state]
ITodo
(^{GET true Timed true} get [this] @state)
(^{DELETE true Timed true} delete [this]
(reset! state (atom {}))
{})
(^{Path "{id}" POST true Timed true}
add [this ^{PathParam "id"} id ^{Valid true} todo]
(swap! state assoc id todo))
(^{Path "{id}" GET true Timed true}
get [this ^{PathParam "id"} id]
(get @state id))
(^{Path "{id}/toggle" POST true Timed true}
toggle [this ^{PathParam "id"} id]
(swap! state update-in [id]
#(Todo. (not (.getComplete %)) (.getDescription %))))
(^{Path "{id}" DELETE true Timed true}
delete [this ^{PathParam "id"} id]
(swap! state dissoc id)))
(defn todo-resource []
(TodoResource. (atom {})))