using Novelly.Api.Common; using Novelly.Api.Common.Validation; namespace Novelly.Api.Chapters; public static class ChapterEndpoints { public static IEndpointRouteBuilder MapChapterEndpoints(this IEndpointRouteBuilder app) { var projectScoped = app.MapGroup("/api/projects/{projectId:guid}/chapters").WithTags("Chapters") .AddEndpointFilter() .AddEndpointFilter(); projectScoped.MapGet("/", async (Guid projectId, ChapterService service, CancellationToken ct) => Results.Ok((await service.ListAsync(projectId, ct)).Select(c => c.ToSummaryResponse()))) .WithSummary("List a project's chapters in manuscript order."); projectScoped.MapPost("/", async ( Guid projectId, CreateChapterRequest request, ChapterService service, CancellationToken ct) => { var chapter = await service.CreateAsync(projectId, request, ct); if (chapter is null) { return Results.NotFound(); } var created = chapter.ToResponse(); return Results.Created($"/api/chapters/{created.Id}", created); }) .WithSummary("Add a chapter."); var chapters = app.MapGroup("/api/chapters").WithTags("Chapters") .AddEndpointFilter() .AddEndpointFilter(); chapters.MapGet("/{id:guid}", async (Guid id, ChapterService service, CancellationToken ct) => (await service.GetAsync(id, ct))?.ToResponse().ToApiResult()) .WithSummary("Read a chapter with its beats and prose."); chapters.MapPatch("/{id:guid}", async ( Guid id, UpdateChapterRequest request, ChapterService service, CancellationToken ct) => (await service.UpdateAsync(id, request, ct))?.ToResponse().ToApiResult()) .WithSummary("Update a chapter."); chapters.MapDelete("/{id:guid}", async (Guid id, ChapterService service, CancellationToken ct) => await service.DeleteAsync(id, ct) ? Results.NoContent() : Results.NotFound()) .WithSummary("Delete a chapter."); return app; } }