-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.janet
158 lines (119 loc) · 3.61 KB
/
app.janet
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(use joy)
(use ./html)
########################################
# Route Definitions
########################################
(route :get "/" :todos/index)
(route :get "/todos" :todos/index)
(route :get "/todos/new" :todos/new)
(route :post "/todos" :todos/create)
(route :get "/todos/:id" :todos/show)
(route :get "/todos/:id/edit" :todos/edit)
(route :patch "/todos/:id" :todos/patch)
(route :delete "/todos/:id" :todos/delete)
########################################
# Todos
########################################
(def todo/body
(body :todo
(validates [:name] :required true)
(permit [:name])))
(defn todos/index [request]
(let [todos (db/from :todo :order "created_at desc")]
[:div
(link-to "New todo" :todos/new)
[:table
[:thead
[:tr
[:th "id"]
[:th "name"]
[:th "completed-at"]
[:th "created-at"]
[:th "updated-at"]
[:th]
[:th]
[:th]]]
[:tbody
(foreach [todo todos]
[:tr
[:td (todo :id)]
[:td (todo :name)]
[:td (todo :completed-at)]
[:td (todo :created-at)]
[:td (todo :updated-at)]
[:td
(link-to "Show" :todos/show todo)]
[:td
(link-to "Edit" :todos/edit todo)]
[:td
(form-to "Delete" request :todos/delete todo)]])]]]))
(defn todos/show [request]
(def {:params params} request)
(when-let [todo (db/find :todo (params :id))]
[:div
(link-to "Back home" :todos/index)
[:table
[:tr
[:th "id"]
[:th "name"]
[:th "completed-at"]
[:th "created-at"]
[:th "updated-at"]]
[:tr
[:td (todo :id)]
[:td (todo :name)]
[:td (todo :completed-at)]
[:td (todo :created-at)]
[:td (todo :updated-at)]]]]))
(defn form [request route &opt todo]
(let [{:errors errors} request]
(form-for [request route todo]
[:label {:for "name"} "name"]
[:input {:type "text" :name "name" :value (get todo :name)}]
[:div (get errors :name)]
[:button {:type "submit"} "Save"])))
(defn todos/new [request]
(form request :todos/create))
(defn todos/create [request]
(let [todo (-> (todo/body request)
(db/save))]
(if (saved? todo)
(redirect-to :todos/index)
(todos/new (put request :errors errors)))))
(defn todos/edit [request]
(let [{:params params} request]
(when-let [todo (db/find :todo (params :id))]
(form request :todos/patch todo))))
(defn todos/patch [request]
(when-let [{:params params} request
todo (db/find :todo (params :id))
todo (->> (todo/body request)
(merge todo)
(db/save))]
(if (todo :errors)
(todos/edit (put request :errors errors))
(redirect-to :todos/index))))
(defn todos/delete [request]
(def {:params params} request)
(when-let [todo (db/find :todo (params :id))]
(db/delete todo))
(redirect-to :todos/index))
########################################
# Layout
########################################
(defn layout [{:body body :request request}]
(text/html
(doctype :html5)
[:html {:lang "en"}
[:head
[:title "todos"]
[:meta {:charset "utf-8"}]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
[:meta {:name "csrf-token" :content (authenticity-token request)}]
[:link {:href "/app.css" :rel "stylesheet"}]
[:script {:src "/app.js" :defer ""}]]
[:body body]]))
########################################
# Middleware
########################################
(def app (app {:layout layout}))