From 60713c20e2ec4a47579cb4bc1e485c7ff8dc7d0b Mon Sep 17 00:00:00 2001 From: Tony Bark <35226681+tonytins@users.noreply.github.com> Date: Sat, 29 Nov 2025 13:15:58 -0500 Subject: [PATCH] Refactor todo API endpoints and constants Introduced constants for API name, version, and todo route. Refactored todo endpoints to use appropriate HTTP methods (GET, POST, PUT, DELETE) and updated route paths for clarity and RESTful design. Removed duplicate and incorrect endpoint mappings. --- Program.cs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Program.cs b/Program.cs index acfe535..341b174 100644 --- a/Program.cs +++ b/Program.cs @@ -1,13 +1,18 @@ var builder = WebApplication.CreateBuilder(args); +const string API_NAME = "BarkAPI"; +const string VERSION = "v1"; +const string TODO = "todo"; + builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("TodoDb")); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOpenApiDocument(cfg => { - cfg.DocumentName = "BarkAPI"; - cfg.Title = "BarkAPI v1"; - cfg.Version = "v1"; + cfg.DocumentName = API_NAME; + cfg.Title = $"{API_NAME} {VERSION}"; + cfg.Version = VERSION; }); + var app = builder.Build(); if (app.Environment.IsDevelopment()) @@ -15,25 +20,25 @@ if (app.Environment.IsDevelopment()) app.UseOpenApi(); app.UseSwaggerUi(cfg => { - cfg.DocumentTitle = "BarkAPI"; + cfg.DocumentTitle = API_NAME; cfg.Path = "/swagger"; cfg.DocumentPath = "/swagger/{documentName}/swagger.json}"; cfg.DocExpansion = "list"; }); } -app.MapGet("/", () => "Bark API"); +app.MapGet("/", () => "Bark Online"); -app.MapGet("/todo", async (TodoDb db) +app.MapGet($"{TODO}", async (TodoDb db) => await db.Todos.ToListAsync()); -app.MapGet("/todo", 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) => await db.Todos.FindAsync(id) is Todo todo ? Results.Ok() : Results.NotFound()); -app.MapGet("/todo", async (Todo todo, TodoDb db) +app.MapPost($"{TODO}", async (Todo todo, TodoDb db) => { db.Todos.Add(todo); @@ -42,7 +47,7 @@ app.MapGet("/todo", async (Todo todo, TodoDb db) return Results.Created($"/todo/{todo.Id}", todo); }); -app.MapGet("/todo/{id}", async (int id, Todo input, TodoDb db) => +app.MapPut("/todo/{id}", async (int id, Todo input, TodoDb db) => { var todo = await db.Todos.FindAsync(id); @@ -56,7 +61,7 @@ app.MapGet("/todo/{id}", async (int id, Todo input, TodoDb db) => return Results.NoContent(); }); -app.MapGet("/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) { @@ -68,12 +73,4 @@ app.MapGet("/todo/{id}", async (int id, TodoDb db) => return Results.NotFound(); }); -app.MapPost("/todo", async (Todo todo, TodoDb db) => -{ - db.Todos.Add(todo); - await db.SaveChangesAsync(); - - return Results.Created($"/todo/{todo.Id}", todo); -}); - app.Run();