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.
This commit is contained in:
Tony Bark 2025-11-29 13:15:58 -05:00
parent 725d4bdeaf
commit 60713c20e2

View file

@ -1,13 +1,18 @@
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
const string API_NAME = "BarkAPI";
const string VERSION = "v1";
const string TODO = "todo";
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoDb")); builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoDb"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(cfg => builder.Services.AddOpenApiDocument(cfg =>
{ {
cfg.DocumentName = "BarkAPI"; cfg.DocumentName = API_NAME;
cfg.Title = "BarkAPI v1"; cfg.Title = $"{API_NAME} {VERSION}";
cfg.Version = "v1"; cfg.Version = VERSION;
}); });
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
@ -15,25 +20,25 @@ if (app.Environment.IsDevelopment())
app.UseOpenApi(); app.UseOpenApi();
app.UseSwaggerUi(cfg => app.UseSwaggerUi(cfg =>
{ {
cfg.DocumentTitle = "BarkAPI"; cfg.DocumentTitle = API_NAME;
cfg.Path = "/swagger"; cfg.Path = "/swagger";
cfg.DocumentPath = "/swagger/{documentName}/swagger.json}"; cfg.DocumentPath = "/swagger/{documentName}/swagger.json}";
cfg.DocExpansion = "list"; 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()); => 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()); => 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) =>
await db.Todos.FindAsync(id) is Todo todo ? Results.Ok() : Results.NotFound()); 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); db.Todos.Add(todo);
@ -42,7 +47,7 @@ app.MapGet("/todo", async (Todo todo, TodoDb db)
return Results.Created($"/todo/{todo.Id}", todo); 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); 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(); 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) 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(); 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(); app.Run();