mirror of
https://git.tonybark.com/tonytins/bark.api.git
synced 2026-02-10 08:14:47 -05:00
Fix todo route paths and simplify delete logic
Added leading slashes to todo route paths. Updated the Created result path in the POST endpoint. Simplified the DELETE endpoint logic by inverting the not-found check and reducing nesting.
This commit is contained in:
parent
60713c20e2
commit
45dff673f8
1 changed files with 8 additions and 11 deletions
19
Program.cs
19
Program.cs
|
|
@ -29,10 +29,10 @@ if (app.Environment.IsDevelopment())
|
||||||
|
|
||||||
app.MapGet("/", () => "Bark Online");
|
app.MapGet("/", () => "Bark Online");
|
||||||
|
|
||||||
app.MapGet($"{TODO}", async (TodoDb db)
|
app.MapGet($"/{TODO}", async (TodoDb db)
|
||||||
=> await db.Todos.ToListAsync());
|
=> await db.Todos.ToListAsync());
|
||||||
|
|
||||||
app.MapGet($"{TODO}/complete", async (TodoDb db)
|
app.MapGet($"/{TODO}/complete", async (TodoDb db)
|
||||||
=> await db.Todos.Where(t => t.IsCompleted).ToListAsync());
|
=> await db.Todos.Where(t => t.IsCompleted).ToListAsync());
|
||||||
|
|
||||||
app.MapGet("/todo/{id}", async (int id, TodoDb db) =>
|
app.MapGet("/todo/{id}", async (int id, TodoDb db) =>
|
||||||
|
|
@ -44,7 +44,7 @@ app.MapPost($"{TODO}", async (Todo todo, TodoDb db)
|
||||||
db.Todos.Add(todo);
|
db.Todos.Add(todo);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
return Results.Created($"/todo/{todo.Id}", todo);
|
return Results.Created($"/{TODO}/{todo.Id}", todo);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.MapPut("/todo/{id}", async (int id, Todo input, TodoDb db) =>
|
app.MapPut("/todo/{id}", async (int id, Todo input, TodoDb db) =>
|
||||||
|
|
@ -63,14 +63,11 @@ app.MapPut("/todo/{id}", async (int id, Todo input, TodoDb db) =>
|
||||||
|
|
||||||
app.MapDelete("/todo/{id}", async (int id, TodoDb db) =>
|
app.MapDelete("/todo/{id}", async (int id, TodoDb db) =>
|
||||||
{
|
{
|
||||||
if (await db.Todos.FindAsync(id) is Todo todo)
|
if (await db.Todos.FindAsync(id) is not Todo todo) return Results.NotFound();
|
||||||
{
|
db.Todos.Remove(todo);
|
||||||
db.Todos.Remove(todo);
|
await db.SaveChangesAsync();
|
||||||
await db.SaveChangesAsync();
|
return Results.NoContent();
|
||||||
return Results.NoContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.NotFound();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue