-
Notifications
You must be signed in to change notification settings - Fork 1
/
Todo.re
277 lines (250 loc) · 6.83 KB
/
Todo.re
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
open ReWeb;
module D = Decoders_yojson.Safe.Decode;
module Entity = {
[@deriving to_yojson]
type t = {
id: int,
title: string,
completed: bool,
};
module Unregistered = {
type t = {
title: string,
completed: bool,
};
let make = (title, completed) => {
title,
completed: completed |> TodoBackend.Util.get_with_default(false),
};
let of_yojson = {
open D;
let (<$>) = D.map;
make
<$> field("title", string)
<*> field_opt("completed", bool)
|> D.decode_value;
};
};
module Partial = {
type t = {
title: option(string),
completed: option(bool),
};
let make_partial = (title, completed) => {title, completed};
let of_yojson = {
open D;
let (<$>) = D.map;
make_partial
<$> field_opt("title", string)
<*> field_opt("completed", bool)
|> D.decode_value;
};
};
let update_with_partial = (partial: Partial.t, todo: t): Unregistered.t => {
title: partial.title |> TodoBackend.Util.get_with_default(todo.title),
completed:
partial.completed |> TodoBackend.Util.get_with_default(todo.completed),
};
};
module Repository = {
open Entity;
open Database;
let or_error = m => {
switch%lwt (m) {
| Ok(a) => Ok(a) |> Lwt.return
| Error(e) => Error(Database_error(Caqti_error.show(e))) |> Lwt.return
};
};
let pool = Database.pool;
let create_table = [%rapper
execute(
{sql|
CREATE TABLE IF NOT EXISTS todos (
id serial,
title VARCHAR(128) NOT NULL,
completed BOOLEAN DEFAULT TRUE,
PRIMARY KEY (id)
);
|sql},
)
];
Caqti_lwt.Pool.use(create_table(), pool);
let get_all = () => {
let get_all_query = [%rapper
get_many(
{sql| SELECT @int{id}, @string{title}, @bool{completed} FROM todos ORDER BY id|sql},
record_out,
)
];
Caqti_lwt.Pool.use(get_all_query(), pool) |> or_error;
};
let get_one_by_id = id => {
let get_one_query = [%rapper
get_opt(
{sql| SELECT @int{id}, @string{title}, @bool{completed} FROM todos WHERE id = %int{id} |sql},
record_out,
)
];
Caqti_lwt.Pool.use(get_one_query(~id), pool) |> or_error;
};
let create_one = (unregistered: Unregistered.t) => {
let create_one_query = [%rapper
execute(
{sql| INSERT INTO todos(title, completed)
VALUES (%string{title}, %bool{completed})|sql},
)
];
let {title, completed}: Unregistered.t = unregistered;
Caqti_lwt.Pool.use(create_one_query(~title, ~completed), pool)
|> or_error;
};
let update_one = (id, unregistered) => {
let update_one_query = [%rapper
execute(
{sql| UPDATE todos SET (title, completed) = (%string{title}, %bool{completed}) WHERE id = %int{id} |sql},
)
];
let {title, completed}: Unregistered.t = unregistered;
Caqti_lwt.Pool.use(update_one_query(~id, ~title, ~completed), pool)
|> or_error;
};
};
open Lwt.Infix;
open Entity;
let index = _req =>
Repository.get_all()
>>= (
fun
| Ok(todos) =>
`List(todos |> List.map(to_yojson))
|> Response.of_json(~status=`OK)
|> Lwt.return
| Error(Database_error(e)) => {
print_endline(e);
Response.of_text(
~status=`Internal_server_error,
"Internal server error",
)
|> Lwt.return;
}
);
let show = (id, _req) =>
switch (int_of_string_opt(id)) {
| None =>
Response.of_text(~status=`Bad_request, "id must be an integer")
|> Lwt.return
| Some(id) =>
Repository.get_one_by_id(id)
>>= (
fun
| Ok(maybe_todo) =>
switch (maybe_todo) {
| None =>
Response.of_text(
~status=`Not_found,
"Could not find todo with id = " ++ string_of_int(id),
)
|> Lwt.return
| Some(todo) =>
todo |> to_yojson |> Response.of_json(~status=`OK) |> Lwt.return
}
| Error(Database_error(e)) => {
print_endline("\n" ++ e);
Response.of_text(
~status=`Internal_server_error,
"Internal server error",
)
|> Lwt.return;
}
)
};
let create =
Filter.body_json @@
(
req => {
let json_body: Yojson.Safe.t = Request.context(req);
switch (Unregistered.of_yojson(json_body)) {
| Error(e) =>
Response.of_text(
~status=`Bad_request,
"Error " ++ D.string_of_error(e),
)
|> Lwt.return
| Ok(unregistered) =>
Repository.create_one(unregistered)
>>= (
fun
| Ok () => Response.of_status(`OK) |> Lwt.return
| Error(Database_error(e)) => {
print_endline("\n" ++ e);
Response.of_text(
~status=`Internal_server_error,
"Internal server error",
)
|> Lwt.return;
}
)
};
}
);
let update = (_meth, id) =>
Filter.body_json @@
(
req => {
switch (int_of_string_opt(id)) {
| None =>
Response.of_text(
~status=`Bad_request,
"id must be an integer, instead got: " ++ id,
)
|> Lwt.return
| Some(id) =>
Repository.get_one_by_id(id)
>>= (
fun
| Error(Database_error(e)) => {
print_endline("\n" ++ e);
Response.of_text(
~status=`Internal_server_error,
"Internal server error",
)
|> Lwt.return;
}
| Ok(None) =>
Response.of_text(
~status=`Not_found,
"There is no todo with id: " ++ string_of_int(id),
)
|> Lwt.return
| Ok(Some(persisted)) => {
let json_body: Yojson.Safe.t = Request.context(req);
switch (json_body |> Partial.of_yojson) {
| Error(e) =>
Response.of_text(
~status=`Bad_request,
"Error " ++ D.string_of_error(e),
)
|> Lwt.return
| Ok(partial) =>
let updated_todo = persisted |> update_with_partial(partial);
Repository.update_one(id, updated_todo)
>>= (
fun
| Ok () => Response.of_status(`OK) |> Lwt.return
| Error(Database_error(e)) => {
print_endline("\n" ++ e);
Response.of_text(
~status=`Internal_server_error,
"Internal server error",
)
|> Lwt.return;
}
);
};
}
)
};
}
);
let resource = route =>
Server.resource(~index, ~show, ~create, ~update, route);