From 45dff673f8bd8b4e190d1d986b949bc978886d73 Mon Sep 17 00:00:00 2001 From: Tony Bark <35226681+tonytins@users.noreply.github.com> Date: Sat, 6 Dec 2025 21:13:37 -0500 Subject: [PATCH] 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. --- Program.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Program.cs b/Program.cs index 341b174..7a6c72e 100644 --- a/Program.cs +++ b/Program.cs @@ -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) - { - db.Todos.Remove(todo); - await db.SaveChangesAsync(); - return Results.NoContent(); - } - - return Results.NotFound(); + if (await db.Todos.FindAsync(id) is not Todo todo) return Results.NotFound(); + db.Todos.Remove(todo); + await db.SaveChangesAsync(); + return Results.NoContent(); + }); app.Run();