-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory.cs
125 lines (99 loc) · 3.31 KB
/
category.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
static class Category {
public static async Task<IResult> Create(
SqlConnection conn, JsonElement o
) {
if(o.ValueKind is not JsonValueKind.Array) {
return Results.BadRequest(new {error = "not an array"});
}
await conn.OpenAsync();
using var cmd = conn.CreateCommand();
cmd.CommandText = "insert into category(name, color) values ";
int _i = 0;
foreach(var ob in o.EnumerateArray()) {
string? name = ob._str("name");
string? color = ob._str("color");
if((name, color) is (null, null)) {
continue;
}
int i = _i++;
cmd.CommandText += $"(@name_{i}, @color_{i}),";
cmd.Parameters.AddWithValue($"name_{i}", name);
cmd.Parameters.AddWithValue($"color_{i}", color);
}
if(_i == 0) {
return Results.BadRequest(new {error = "array is empty"});
}
cmd.CommandText = cmd.CommandText.TrimEnd(',');
if(await cmd.ExecuteNonQueryAsync() == 0) {
return Results.BadRequest(new {error = "cannot create"});
}
return Results.Ok();
}
public static async Task<IResult> List(
SqlConnection conn, JsonElement? o
) {
await conn.OpenAsync();
using var cmd = conn.CreateCommand();
cmd.CommandText = "select id, name, color from category where 1=1 ";
if(o?._long("id") is long id) {
cmd.CommandText += " and id=@id ";
cmd.Parameters.AddWithValue("id", id);
}
if(o?._str("name") is string name) {
cmd.CommandText += " and name like @name ";
cmd.Parameters.AddWithValue("name", $"%{name}%");
}
if(o?._str("color") is string color) {
cmd.CommandText += " and color = @color ";
cmd.Parameters.AddWithValue("color", color);
}
return Results.Ok((await cmd.ExecuteReaderAsync()).ToDictArray());
}
public static async Task<IResult> Update(
SqlConnection conn, JsonElement o
) {
long? id = o._long("id");
if(id is null) {
return Results.BadRequest(new {error = "need an id"});
}
string? name = o._str("name");
string? color = o._str("color");
if((name, color) is (null, null)) {
return Results.BadRequest(new {error = "no field to update"});
}
await conn.OpenAsync();
using var cmd = conn.CreateCommand();
cmd.CommandText = "update category set ";
if(name is not null) {
cmd.CommandText += " name = @name ,";
cmd.Parameters.AddWithValue("name", name);
}
if(color is not null) {
cmd.CommandText += " color = @color ,";
cmd.Parameters.AddWithValue("color", color);
}
cmd.CommandText = cmd.CommandText.TrimEnd(',');
cmd.CommandText += " where id = @id";
cmd.Parameters.AddWithValue("id", id);
if(await cmd.ExecuteNonQueryAsync() == 0) {
return Results.BadRequest(new {error = "could not update"});
}
return Results.Ok();
}
public static async Task<IResult> Delete(
SqlConnection conn, JsonElement o
) {
long? id = o._long("id");
if(id is null) {
return Results.BadRequest(new {error = "need an id"});
}
await conn.OpenAsync();
using var cmd = conn.CreateCommand();
cmd.CommandText = "delete from category where id = @id";
cmd.Parameters.AddWithValue("id", id);
if(await cmd.ExecuteNonQueryAsync() == 0) {
return Results.BadRequest(new {error = "cannot delete"});
}
return Results.Ok();
}
}