-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodoApiWebApiEndpoint.cs
153 lines (117 loc) · 6.55 KB
/
TodoApiWebApiEndpoint.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
using Microsoft.Data.Sqlite;
namespace Futurum.WebApiEndpoint.Micro.Sample.Todo;
[WebApiEndpoint("api/todos", "todos")]
public partial class TodoApiWebApiEndpoint
{
protected override void Build(IEndpointRouteBuilder builder)
{
builder.MapGet("/", GetAllHandler)
.WithName("GetAllTodos");
builder.MapGet("/complete", GetCompleteHandler)
.WithName("GetCompleteTodos");
builder.MapGet("/incomplete", GetIncompleteHandler)
.WithName("GetIncompleteTodos");
builder.MapGet("/{id:int}", GetByIdHandler)
.WithName("GetTodoById");
builder.MapGet("/find", FindHandler)
.WithName("FindTodo");
builder.MapPost("/", CreateHandler)
.WithName("CreateTodo");
builder.MapPut("/{id}", UpdateHandler)
.WithName("UpdateTodo");
builder.MapPut("/{id}/mark-complete", MarkCompleteHandler)
.WithName("MarkComplete");
builder.MapPut("/{id}/mark-incomplete", MarkIncompleteHandler)
.WithName("MarkIncomplete");
builder.MapDelete("/{id}", DeleteHandler)
.WithName("DeleteTodo");
builder.MapDelete("/delete-all", DeleteAllHandler)
.WithName("DeleteAll");
}
private static Results<Ok<IAsyncEnumerable<Todo>>, BadRequest<ProblemDetails>> GetAllHandler(HttpContext context, SqliteConnection db)
{
return RunToOk(Execute, context, "Failed to get todos");
IAsyncEnumerable<Todo> Execute() =>
db.QueryAsync<Todo>("SELECT * FROM Todos");
}
private static Results<Ok<IAsyncEnumerable<Todo>>, BadRequest<ProblemDetails>> GetCompleteHandler(HttpContext context, SqliteConnection db)
{
return RunToOk(Execute, context, "Failed to get complete todos");
IAsyncEnumerable<Todo> Execute() =>
db.QueryAsync<Todo>("SELECT * FROM Todos WHERE IsComplete = true");
}
private static Results<Ok<IAsyncEnumerable<Todo>>, BadRequest<ProblemDetails>> GetIncompleteHandler(HttpContext context, SqliteConnection db)
{
return RunToOk(Execute, context, "Failed to get incomplete todos");
IAsyncEnumerable<Todo> Execute() =>
db.QueryAsync<Todo>("SELECT * FROM Todos WHERE IsComplete = false");
}
private static Task<Results<Ok<Todo>, NotFound, BadRequest<ProblemDetails>>> GetByIdHandler(HttpContext context, SqliteConnection db, int id)
{
return RunAsync(Execute, context, $"Failed to find todo with id '{id}'");
async Task<Results<Ok<Todo>, NotFound>> Execute() =>
await db.QuerySingleAsync<Todo>("SELECT * FROM Todos WHERE Id = @id", id.AsDbParameter()) is Todo todo
? TypedResults.Ok(todo)
: TypedResults.NotFound();
}
private static Task<Results<Ok<Todo>, NoContent, BadRequest<ProblemDetails>>> FindHandler(HttpContext context, SqliteConnection db, string title, bool? isComplete)
{
return RunAsync(Execute, context, $"Failed to find todo with title '{title}'");
async Task<Results<Ok<Todo>, NoContent>> Execute() =>
await db.QuerySingleAsync<Todo>("SELECT * FROM Todos WHERE Title = @title COLLATE NOCASE AND (@isComplete is NULL OR IsComplete = @isComplete)",
title.AsDbParameter(),
isComplete.AsDbParameter()) is Todo todo
? TypedResults.Ok(todo)
: TypedResults.NoContent();
}
private static Task<Results<Created<Todo>, BadRequest<ProblemDetails>>> CreateHandler(HttpContext context, SqliteConnection db, Todo todo)
{
return RunAsync(Execute, context, "Failed to create todo");
async Task<Created<Todo>> Execute()
{
var createdTodo = await db.QuerySingleAsync<Todo>("INSERT INTO Todos(Title, IsComplete) Values(@Title, @IsComplete) RETURNING *",
todo.Title.AsDbParameter(),
todo.IsComplete.AsDbParameter());
return TypedResults.Created($"/todos/{createdTodo?.Id}", createdTodo);
}
}
private static Task<Results<NoContent, NotFound, BadRequest<ProblemDetails>>> UpdateHandler(HttpContext context, SqliteConnection db, int id, Todo inputTodo)
{
inputTodo.Id = id;
return RunAsync(Execute, context, "Failed to update todo");
async Task<Results<NoContent, NotFound>> Execute() =>
await db.ExecuteAsync("UPDATE Todos SET Title = @Title, IsComplete = @IsComplete WHERE Id = @Id", inputTodo.Title.AsDbParameter(), inputTodo.IsComplete.AsDbParameter()) == 1
? TypedResults.NoContent()
: TypedResults.NotFound();
}
private static Task<Results<NoContent, NotFound, BadRequest<ProblemDetails>>> MarkCompleteHandler(HttpContext context, SqliteConnection db, int id)
{
return RunAsync(Execute, context, $"Failed to mark todo with id '{id}' as incomplete");
async Task<Results<NoContent, NotFound>> Execute() =>
await db.ExecuteAsync("UPDATE Todos SET IsComplete = true WHERE Id = @id", id.AsDbParameter()) == 1
? TypedResults.NoContent()
: TypedResults.NotFound();
}
private static Task<Results<NoContent, NotFound, BadRequest<ProblemDetails>>> MarkIncompleteHandler(HttpContext context, SqliteConnection db, int id)
{
return RunAsync(Execute, context, $"Failed to mark todo with id '{id}' as incomplete");
async Task<Results<NoContent, NotFound>> Execute() =>
await db.ExecuteAsync("UPDATE Todos SET IsComplete = false WHERE Id = @id", id.AsDbParameter()) == 1
? TypedResults.NoContent()
: TypedResults.NotFound();
}
private static Task<Results<NoContent, NotFound, BadRequest<ProblemDetails>>> DeleteHandler(HttpContext context, SqliteConnection db, int id)
{
return RunAsync(Execute, context, $"Failed to delete todo with id '{id}'");
async Task<Results<NoContent, NotFound>> Execute() =>
await db.ExecuteAsync("DELETE FROM Todos WHERE Id = @id", id.AsDbParameter()) == 1
? TypedResults.NoContent()
: TypedResults.NotFound();
}
private static Task<Results<Ok<int>, BadRequest<ProblemDetails>>> DeleteAllHandler(HttpContext context, SqliteConnection db)
{
return RunToOkAsync(Execute, context, "Failed to delete all todos");
Task<int> Execute() =>
db.ExecuteAsync("DELETE FROM Todos");
}
}