mirror of
https://git.tonybark.com/tonytins/bark.api.git
synced 2026-02-10 16:24:47 -05:00
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:
parent
725d4bdeaf
commit
60713c20e2
1 changed files with 15 additions and 18 deletions
33
Program.cs
33
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<TodoDb>(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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue