-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo.cs
283 lines (232 loc) · 8.16 KB
/
todo.cs
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
278
279
280
281
282
283
readonly record struct TodoO (
long? id,
string? task,
long? due,
long[]? categories,
bool? done
);
static class Todo {
public static IResult Create(
Auth auth, SqliteConnection conn, TodoO todo) {
if(String.IsNullOrEmpty(todo.task?.Trim())) {
return Results.BadRequest(new {error = "need a task"});
}
using var tran = conn.BeginTransaction();
try {
using var cmd = conn.CreateCommand();
cmd.CommandText =
"""
insert into todo
(task, done, due_unix_timestamp, userid)
values
(:task, :done, :due, :userid)
""";
cmd.Parameters.AddWithValue("task", todo.task);
cmd.Parameters.AddWithValue("done", todo.done ?? false);
cmd.Parameters.AddWithValue("due",
todo.due ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds());
cmd.Parameters.AddWithValue("userid", auth.GetCurrentUser());
cmd.ExecuteNonQuery();
if(todo.categories is not (null or {Length: 0})) {
using var last_insert = conn.CreateCommand();
last_insert.CommandText = "select last_insert_rowid()";
long? todoid = (long?) last_insert.ExecuteScalar();
if(todoid is not null) {
using var category_todo = conn.CreateCommand();
category_todo.CommandText =
Enumerable.Range(0, todo.categories.Length)
.Aggregate(
"insert or ignore into category_todo"
+ " (categoryid, todoid) values ",
(a, v) => a + $"(:categoryid_{v}, :todoid),")
.TrimEnd(',');
category_todo.Parameters.AddRange(
todo.categories.Select((e, i) => new SqliteParameter {
ParameterName = $"categoryid_{i}",
SqliteType = SqliteType.Integer,
Value = e
}));
category_todo.Parameters.Add(
new SqliteParameter {
ParameterName = "todoid",
SqliteType = SqliteType.Integer,
Value = todoid
});
category_todo.ExecuteNonQuery();
}
}
tran.Commit();
} catch(SqliteException ex) {
tran.Rollback();
return Results.BadRequest(new {error = ex.Message});
}
return Results.Ok();
}
public static IResult Update(
Auth auth, SqliteConnection conn, TodoO todo) {
if(todo.id is null) {
return Results.BadRequest(new {error = "need an id"});
}
if(todo with {id = null} == new TodoO()) {
return Results.BadRequest(new {error = "nothing to update"});
}
using var tran = conn.BeginTransaction();
try {
using var cmd = conn.CreateCommand();
cmd.CommandText = "update or ignore todo set ";
if(todo.task is not null) {
cmd.CommandText += " task = :task ,";
cmd.Parameters.AddWithValue("task", todo.task);
}
if(todo.due is not null) {
cmd.CommandText += " due_unix_timestamp = :due ,";
cmd.Parameters.AddWithValue("due", todo.due);
}
if(todo.done is not null) {
cmd.CommandText += " done = :done ,";
cmd.Parameters.AddWithValue("done", todo.done);
}
cmd.CommandText = cmd.CommandText.TrimEnd(',');
cmd.CommandText += " where id = :id and userid = :userid";
cmd.Parameters.AddWithValue("id", todo.id);
cmd.Parameters.AddWithValue("userid", auth.GetCurrentUser());
if(cmd.ExecuteNonQuery() == 0) {
return Results.BadRequest(new {error = "cannot update"});
}
if(todo.categories is not null) {
using var del = conn.CreateCommand();
del.CommandText = "delete from category_todo where todoid = :todoid";
del.Parameters.AddWithValue("todoid", todo.id);
del.ExecuteNonQuery();
if(todo.categories.Length != 0) {
using var category_todo = conn.CreateCommand();
category_todo.CommandText =
Enumerable.Range(0, todo.categories.Length)
.Aggregate(
"insert or ignore into category_todo"
+ " (categoryid, todoid) values ",
(a, v) => a + $"(:categoryid_{v}, :todoid),")
.TrimEnd(',');
category_todo.Parameters.AddRange(
todo.categories.Select((e, i) => new SqliteParameter {
ParameterName = $"categoryid_{i}",
SqliteType = SqliteType.Integer,
Value = e
}));
category_todo.Parameters.Add(
new SqliteParameter {
ParameterName = "todoid",
SqliteType = SqliteType.Integer,
Value = todo.id
});
category_todo.ExecuteNonQuery();
}
}
tran.Commit();
} catch(SqliteException ex) {
tran.Rollback();
return Results.BadRequest(new {error = ex.Message});
}
return Results.Ok("update");
}
public static IResult List(
Auth auth, SqliteConnection conn, JsonElement? o) {
long? cursor_init = o?._long("cursor_init");
long? cursor_prev = o?._long("cursor_prev");
long? cursor_next = o?._long("cursor_next");
long? cursor = cursor_next ?? cursor_prev;
bool forward = cursor_prev == null;
var (op, dir) = forward ? (">" , "asc") : ("<" , "desc");
using var cmd = conn.CreateCommand();
cmd.CommandText =
"""
select
t.id,
t.task,
t.done,
t.due_unix_timestamp,
iif((c.id is null), null,
json_group_array(
json_object('id', c.id, 'name', c.name, 'color', c.color)
)) as categories
from todo t
left join category_todo ct on t.id = ct.todoid
left join category c on c.id = ct.categoryid
where 1
""";
if(o?._long("id") is long id) {
cmd.CommandText += " and t.id=:id ";
cmd.Parameters.AddWithValue("id", id);
}
if(o?._str("task") is string task) {
cmd.CommandText += " and t.task like :task ";
cmd.Parameters.AddWithValue("task", $"%{task}%");
}
if(o?._bool("done") is bool done) {
cmd.CommandText += " and t.done = :done ";
cmd.Parameters.AddWithValue("done", done);
}
if(o?._long("due_from") is long due_from) {
cmd.CommandText += " and t.due_unix_timestamp >= :due_from ";
cmd.Parameters.AddWithValue("due_from", due_from);
}
if(o?._long("due_to") is long due_to) {
cmd.CommandText += " and t.due_unix_timestamp <= :due_to ";
cmd.Parameters.AddWithValue("due_to", due_to);
}
if(o?._arr("categories")?
.Select(e => (long?) (
(e.ValueKind is JsonValueKind.Number
&& e.TryGetInt64(out var i)) ? i : null))
.Where(e => e != null) is var arr
&& arr is not null
&& String.Join(',', arr) is var categories
&& categories.Length != 0
) {
cmd.CommandText +=
$" and ct.categoryid in ({String.Join(',', categories)}) ";
}
if(cursor != null) {
cmd.CommandText += $" and t.id {op} :cursor ";
cmd.Parameters.AddWithValue("cursor", cursor);
}
cmd.CommandText +=
$"and t.userid = :userid group by t.id order by t.id {dir} limit 10";
cmd.Parameters.AddWithValue("userid", auth.GetCurrentUser());
var data = cmd.ExecuteReader().ToDictArray(!forward);
cursor_prev = cursor_next = null;
if(data.Length != 0) {
cursor_prev = (long?) data[0]["id"];
if(cursor == null) {
cursor_init = cursor_prev;
}
if(cursor_init == cursor_prev) {
cursor_prev = null;
}
if(data.Length == 10) {
cursor_next = (long?) data[^1]["id"];
}
}
return Results.Ok(new {
data,
cursor_init,
cursor_prev,
cursor_next,
});
}
public static IResult Delete(
Auth auth, SqliteConnection conn, JsonElement o) {
long? id = o._long("id");
if(id is null) {
return Results.BadRequest(new {error = "need an id"});
}
using var cmd = conn.CreateCommand();
cmd.CommandText = "delete from todo where id=:id and userid=:userid";
cmd.Parameters.AddWithValue("id", id);
cmd.Parameters.AddWithValue("userid", auth.GetCurrentUser());
if(cmd.ExecuteNonQuery() == 0) {
return Results.BadRequest(new {error = "cannot delete"});
}
return Results.Ok();
}
}