-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaz.cs
54 lines (42 loc) · 1.21 KB
/
az.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
static class AZ {
public static IResult List(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
id, letter, due
from az where 1
""";
if(cursor != null) {
cmd.CommandText += $" and id {op} :cursor ";
cmd.Parameters.AddWithValue("cursor", cursor);
}
cmd.CommandText += $" order by id {dir} limit 10";
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,
});
}
}