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:
Tony Bark 2025-12-06 21:13:37 -05:00
parent 60713c20e2
commit 45dff673f8

View file

@ -29,10 +29,10 @@ if (app.Environment.IsDevelopment())
app.MapGet("/", () => "Bark Online");
app.MapGet($"{TODO}", async (TodoDb db)
app.MapGet($"/{TODO}", async (TodoDb db)
=> 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());
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);
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) =>
@ -63,14 +63,11 @@ app.MapPut("/todo/{id}", async (int id, Todo input, 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);
await db.SaveChangesAsync();
return Results.NoContent();
}
return Results.NotFound();
});
app.Run();