-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (121 loc) · 3.53 KB
/
Program.cs
File metadata and controls
146 lines (121 loc) · 3.53 KB
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
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Rewrite;
var builder = WebApplication.CreateBuilder(args);
// register new service
builder.Services.AddSingleton<ITaskService>(new InMemoryTaskService());
var app = builder.Build();
app.UseRewriter(new RewriteOptions().AddRedirect("tasks/(.*)/(.*)", "todos/$1/$2"));
app.UseRewriter(new RewriteOptions().AddRedirect("tasks", "todos"));
app.Use( // middleware
async (context, next) =>
{
Console.WriteLine(
$"[{context.Request.Method} {context.Request.Path} {DateTime.UtcNow}] Started."
);
await next(context);
Console.WriteLine(
$"[{context.Request.Method} {context.Request.Path} {DateTime.UtcNow}] Finished."
);
}
);
var todos = new List<Todo>();
// GET with a Dependency Injection
app.MapGet(
"/todos",
Results<Ok<List<Todo>>, NotFound> (ITaskService service) =>
{
return TypedResults.Ok(service.GetTodos());
}
);
app.MapGet(
"/todos/{id}",
Results<Ok<Todo>, NotFound> (int id, ITaskService service) =>
{
var targetTodo = service.GetTodoById(id);
return targetTodo is null ? TypedResults.NotFound() : TypedResults.Ok(targetTodo);
}
);
app.MapPost(
"/todos",
(Todo task, ITaskService service) =>
{
task.Id = todos.Count + 1;
service.AddTodo(task);
return TypedResults.Created("/todos/{id}", task);
}
)
.AddEndpointFilter( // Endpoint filter
async (context, next) =>
{
var taskArgument = context.GetArgument<Todo>(0);
var errors = new Dictionary<string, string[]>();
if (taskArgument.DueDate < DateTime.UtcNow)
{
errors.Add(nameof(Todo.DueDate), ["Cannot have due date in the past."]);
}
if (taskArgument.IsCompleted)
{
errors.Add(nameof(Todo.IsCompleted), ["Cannot add completed todo."]);
}
if (errors.Count > 0)
{
return Results.ValidationProblem(errors);
}
return await next(context);
}
);
app.MapPatch(
"/todos/{id}",
Results<Ok<Todo>, NotFound> (int id, ITaskService service) =>
{
var targetTodo = service.GetTodoById(id);
if (targetTodo is null)
return TypedResults.NotFound();
targetTodo.IsCompleted = !targetTodo.IsCompleted;
return TypedResults.Ok(targetTodo);
}
);
app.MapDelete(
"/todos/{id}",
Results<NoContent, NotFound> (int id, ITaskService service) =>
{
service.DeleteTodoById(id);
return TypedResults.NoContent();
}
);
app.Run();
public record Todo(int Id, string Name, DateTime DueDate, bool IsCompleted)
{
public int Id { get; set; } = Id;
public bool IsCompleted { get; set; } = IsCompleted;
}
// Interface for dependency
interface ITaskService
{
Todo? GetTodoById(int id);
List<Todo> GetTodos();
void DeleteTodoById(int id);
Todo AddTodo(Todo task);
}
// Concrete implementation of Interface
class InMemoryTaskService : ITaskService
{
private readonly List<Todo> _todos = [];
public Todo AddTodo(Todo task)
{
_todos.Add(task);
return task;
}
public List<Todo> GetTodos()
{
return _todos;
}
public Todo? GetTodoById(int id)
{
return _todos.SingleOrDefault(task => task.Id == id);
}
public void DeleteTodoById(int id)
{
_todos.RemoveAll(task => task.Id == id);
}
}