-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
57 lines (42 loc) · 1.53 KB
/
Program.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
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<CalimaDB>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
app.MapGet("/", async (CalimaDB db) =>
await db.Calimas.ToListAsync());
app.MapGet("/calimas", async (CalimaDB db) =>
await db.Calimas.ToListAsync());
app.MapGet("/calimas/complete", async (CalimaDB db) =>
await db.Calimas.Where(t => t.IsComplete).ToListAsync());
app.MapGet("/calima/{id}", async (int id, CalimaDB db) =>
await db.Calimas.FindAsync(id)
is Calima calima
? Results.Ok(calima)
: Results.NotFound());
app.MapPost("/calima", async (Calima calima, CalimaDB db) =>
{
db.Calimas.Add(calima);
await db.SaveChangesAsync();
return Results.Created($"/calima/{calima.Id}", calima);
});
app.MapPut("/calima/{id}", async (int id, Calima calimaInput, CalimaDB db) =>
{
var calima = await db.Calimas.FindAsync(id);
if (calima is null) return Results.NotFound();
calima.Name = calimaInput.Name;
calima.IsComplete = calimaInput.IsComplete;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/calima/{id}", async (int id, CalimaDB db) =>
{
if (await db.Calimas.FindAsync(id) is Calima calima)
{
db.Calimas.Remove(calima);
await db.SaveChangesAsync();
return Results.NoContent();
}
return Results.NotFound();
});
app.Run();