-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (141 loc) · 3.36 KB
/
Program.cs
File metadata and controls
169 lines (141 loc) · 3.36 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(configure =>
{
configure.DocumentName = "TodoApi";
configure.Title = "Todo API v1";
configure.Version = "v1";
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseOpenApi();
app.UseSwaggerUi();
}
var todos = new Dictionary<Guid, Todo>();
app.MapPost(
"/todos",
(CreateRequest request) =>
{
var todo = Todo.Create(request.Title);
todos[todo.Id] = todo;
return Results.Created($"/todos/{todo.Id}", Response.From(todo));
}
);
app.MapPut(
"/todos/{id:guid}",
(Guid id, UpdateRequest request) =>
{
if (!todos.TryGetValue(id, out var todo))
{
return Results.NotFound("Todo not found.");
}
var updated = todo.Update(request.Title, request.IsCompleted);
todos[id] = updated;
return Results.Ok(Response.From(updated));
}
);
app.MapGet(
"/todos/{id}",
(Guid? id) =>
{
if (!todos.TryGetValue(id.Value, out var todo))
{
return Results.NotFound("Todo not found.");
}
return Results.Ok(Response.From(todo));
}
);
app.MapGet(
"/todos",
() =>
{
return Results.Ok(todos.Values.Select(Response.From));
}
);
app.MapDelete(
"/todos/{id:guid}",
(Guid id) =>
{
if (!todos.Remove(id))
{
return Results.NotFound("Todo not found.");
}
return Results.NoContent();
}
);
app.MapGet("/", () => "Hello World!");
app.Run();
public class Todo
{
public Guid Id { get; init; }
public string Title { get; private set; }
public bool IsCompleted { get; private set; }
public DateTime CreatedAt { get; init; }
public DateTime? UpdatedAt { get; private set; }
public DateTime? CompletedAt { get; private set; }
public static Todo Create(string title)
{
return new Todo
{
Id = Guid.NewGuid(),
Title = title,
IsCompleted = false,
CreatedAt = DateTime.UtcNow,
};
}
public Todo Update(string title, bool isCompleted)
{
Title = title;
UpdatedAt = DateTime.UtcNow;
if (isCompleted && !IsCompleted)
{
IsCompleted = isCompleted;
CompletedAt = DateTime.UtcNow;
}
else if (!isCompleted && IsCompleted)
{
IsCompleted = isCompleted;
CompletedAt = null;
}
return this;
}
public void Complete()
{
if (IsCompleted)
{
return;
}
IsCompleted = true;
CompletedAt = DateTime.UtcNow;
UpdatedAt = DateTime.UtcNow;
}
}
public record CreateRequest
{
public required string Title { get; init; }
}
public record UpdateRequest
{
public required string Title { get; init; }
public bool IsCompleted { get; init; }
}
public record Response
{
public Guid Id { get; init; }
public required string Title { get; init; }
public bool IsCompleted { get; init; }
public DateTime? CreatedAt { get; init; }
public DateTime? UpdatedAt { get; init; }
public DateTime? CompletedAt { get; init; }
public static Response From(Todo todo) =>
new()
{
Id = todo.Id,
Title = todo.Title,
IsCompleted = todo.IsCompleted,
CreatedAt = todo.CreatedAt,
UpdatedAt = todo.UpdatedAt,
CompletedAt = todo.CompletedAt,
};
}