using Novelly.Api.Common; namespace Novelly.Api.Characters; public static class CharacterEndpoints { public static IEndpointRouteBuilder MapCharacterEndpoints(this IEndpointRouteBuilder app) { var projectScoped = app.MapGroup("/api/projects/{projectId:guid}/characters").WithTags("Characters").AddEndpointFilter(); projectScoped.MapGet("/", async (Guid projectId, CharacterService service, CancellationToken ct) => Results.Ok(await service.ListAsync(projectId, ct))) .WithSummary("List a project's character dossiers."); projectScoped.MapPost("/", async ( Guid projectId, CreateCharacterRequest request, CharacterService service, CancellationToken ct) => { var created = await service.CreateAsync(projectId, request, ct); return Results.Created($"/api/characters/{created.Id}", created); }) .WithSummary("Add a character dossier."); var characters = app.MapGroup("/api/characters").WithTags("Characters").AddEndpointFilter(); characters.MapGet("/{id:guid}", async (Guid id, CharacterService service, CancellationToken ct) => Results.Ok(await service.GetAsync(id, ct))) .WithSummary("Read a character dossier."); characters.MapPatch("/{id:guid}", async ( Guid id, UpdateCharacterRequest request, CharacterService service, CancellationToken ct) => Results.Ok(await service.UpdateAsync(id, request, ct))) .WithSummary("Update a character dossier."); characters.MapDelete("/{id:guid}", async (Guid id, CharacterService service, CancellationToken ct) => { await service.DeleteAsync(id, ct); return Results.NoContent(); }) .WithSummary("Delete a character."); characters.MapPost("/{id:guid}/relationships", async ( Guid id, CreateRelationshipRequest request, CharacterService service, CancellationToken ct) => Results.Ok(await service.AddRelationshipAsync(id, request, ct))) .WithSummary("Relate this character to another in the same project."); characters.MapDelete("/relationships/{relationshipId:guid}", async ( Guid relationshipId, CharacterService service, CancellationToken ct) => { await service.RemoveRelationshipAsync(relationshipId, ct); return Results.NoContent(); }) .WithSummary("Remove a relationship."); characters.MapGet("/{id:guid}/arc", async ( Guid id, CharacterArcService service, CancellationToken ct) => Results.Ok(await service.ListAsync(id, ct))) .WithSummary("Read a character's arc: its stages, in order."); characters.MapPost("/{id:guid}/arc", async ( Guid id, CreateArcStageRequest request, CharacterArcService service, CancellationToken ct) => { var created = await service.CreateAsync(id, request, ct); return Results.Created($"/api/arc-stages/{created.Id}", created); }) .WithSummary("Add a stage to a character's arc."); characters.MapPost("/{id:guid}/arc/reorder", async ( Guid id, ReorderArcStagesRequest request, CharacterArcService service, CancellationToken ct) => Results.Ok(await service.ReorderAsync(id, request, ct))) .WithSummary("Renumber a character's arc to match the order given."); var arcStages = app.MapGroup("/api/arc-stages").WithTags("Characters").AddEndpointFilter(); arcStages.MapGet("/{id:guid}", async (Guid id, CharacterArcService service, CancellationToken ct) => Results.Ok(await service.GetAsync(id, ct))) .WithSummary("Read one arc stage."); arcStages.MapPatch("/{id:guid}", async ( Guid id, UpdateArcStageRequest request, CharacterArcService service, CancellationToken ct) => Results.Ok(await service.UpdateAsync(id, request, ct))) .WithSummary("Update an arc stage."); arcStages.MapDelete("/{id:guid}", async (Guid id, CharacterArcService service, CancellationToken ct) => { await service.DeleteAsync(id, ct); return Results.NoContent(); }) .WithSummary("Delete an arc stage."); return app; } }