-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
164 lines (123 loc) · 4.11 KB
/
Program.cs
File metadata and controls
164 lines (123 loc) · 4.11 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
using Microsoft.EntityFrameworkCore;
using NSwag.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(config =>
{
config.DocumentName = "ApiMinimal";
config.Title = "ApiMinimal v1";
config.Version = "v1";
});
Console.WriteLine("Está funcionando!!");
var app = builder.Build();
// just to test
app.UseOpenApi();
app.UseSwaggerUi(config =>
{
config.DocumentTitle = "MinimalAPI";
config.Path = "/swagger";
config.DocumentPath = "/swagger/{documentName}/swagger.json";
config.DocExpansion = "list";
});
// to prodution uncomment this block of code
// if (app.Environment.IsDevelopment())
// {
// app.UseOpenApi();
// app.UseSwaggerUi(config =>
// {
// config.DocumentTitle = "MinimalAPI";
// config.Path = "/swagger";
// config.DocumentPath = "/swagger/{documentName}/swagger.json";
// config.DocExpansion = "list";
// });
// }
RouteGroupBuilder todoItems = app.MapGroup("/todoitems");
todoItems.MapGet("/", GetAllTodos);
todoItems.MapGet("/complete", GetCompleteTodos);
todoItems.MapGet("/{id}", GetTodo);
todoItems.MapPost("/", CreateTodo);
todoItems.MapPut("/{id}", UpdateTodo);
todoItems.MapDelete("/{id}", DeleteTodo);
app.Run();
static async Task<IResult> GetAllTodos(TodoDb db)
{
return TypedResults.Ok(await db.Todos.Select(x => new TodoItemDTO(x)).ToArrayAsync());
}
static async Task<IResult> GetCompleteTodos(TodoDb db)
{
return TypedResults.Ok(await db.Todos.Where(t => t.IsComplete).Select(x => new TodoItemDTO(x)).ToListAsync());
}
static async Task<IResult> GetTodo(int id, TodoDb db)
{
return await db.Todos.FindAsync(id)
is Todo todo
? TypedResults.Ok(new TodoItemDTO(todo))
: TypedResults.NotFound();
}
static async Task<IResult> CreateTodo(TodoItemDTO todoItemDTO, TodoDb db)
{
var todoItem = new Todo
{
IsComplete = todoItemDTO.IsComplete,
Name = todoItemDTO.Name
};
db.Todos.Add(todoItem);
await db.SaveChangesAsync();
todoItemDTO = new TodoItemDTO(todoItem);
return TypedResults.Created($"/todoitems/{todoItem.Id}", todoItemDTO);
}
static async Task<IResult> UpdateTodo(int id, TodoItemDTO todoItemDTO, TodoDb db)
{
var todo = await db.Todos.FindAsync(id);
if (todo is null) return TypedResults.NotFound();
todo.Name = todoItemDTO.Name;
todo.IsComplete = todoItemDTO.IsComplete;
await db.SaveChangesAsync();
return TypedResults.NoContent();
}
static async Task<IResult> DeleteTodo(int id, TodoDb db)
{
if (await db.Todos.FindAsync(id) is Todo todo)
{
db.Todos.Remove(todo);
await db.SaveChangesAsync();
return TypedResults.NoContent();
}
return TypedResults.NotFound();
}
// todoItems.MapGet("/", async (TodoDb db) =>
// await db.Todos.ToListAsync());
// todoItems.MapGet("/complete", async (TodoDb db) =>
// await db.Todos.Where(t => t.IsComplete).ToListAsync());
// todoItems.MapGet("/{id}", async (int id, TodoDb db) =>
// await db.Todos.FindAsync(id)
// is Todo todo
// ? Results.Ok(todo)
// : Results.NotFound()
// );
// todoItems.MapPost("/", async (Todo todo, TodoDb db) =>
// {
// db.Todos.Add(todo);
// await db.SaveChangesAsync();
// return Results.Created($"/todoitems/{todo.Id}", todo);
// });
// todoItems.MapPut("/{id}", async (int id, Todo inputTodo, TodoDb db) =>
// {
// var todo = await db.Todos.FindAsync(id);
// if (todo is null) return Results.NotFound();
// todo.Name = inputTodo.Name;
// todo.IsComplete = inputTodo.IsComplete;
// await db.SaveChangesAsync();
// return Results.NoContent();
// });
// todoItems.MapDelete("/{id}", async (int id, TodoDb db) =>
// {
// if (await db.Todos.FindAsync(id) is Todo todo){
// db.Todos.Remove(todo);
// await db.SaveChangesAsync();
// return Results.NoContent();
// }
// return Results.NotFound();
// });