using LiteCqrs; using TeleWave.Api.Common; using TeleWave.Application.Library.Collections; using TeleWave.Application.Library.Collections.AddCollectionShow; using TeleWave.Application.Library.Collections.CreateCollection; using TeleWave.Application.Library.Collections.DeleteCollection; using TeleWave.Application.Library.Collections.GetCollection; using TeleWave.Application.Library.Collections.ListCollections; using TeleWave.Application.Library.Collections.RemoveCollectionShow; using TeleWave.Application.Library.Collections.ReorderCollection; using TeleWave.Application.Library.Collections.SetCollectionPoster; using TeleWave.Application.Library.Collections.UpdateCollection; using TeleWave.Infrastructure.Identity; namespace TeleWave.Api.Endpoints; public static class CollectionEndpoints { public static IEndpointRouteBuilder MapCollectionEndpoints(this IEndpointRouteBuilder app) { var admin = app.MapGroup("/api/admin/collections") .WithTags("Admin.Collections") .RequireAuthorization(policy => policy.RequireRole(RoleNames.Admin)); admin.MapGet("", ListCollections).Produces>(); admin.MapGet("/{id:guid}", GetCollection).Produces(); admin .MapPost("", CreateCollection) .Produces(StatusCodes.Status201Created); admin.MapPut("/{id:guid}", UpdateCollection).Produces(StatusCodes.Status204NoContent); admin.MapDelete("/{id:guid}", DeleteCollection).Produces(StatusCodes.Status204NoContent); admin.MapPost("/{id:guid}/shows", AddShow).Produces(StatusCodes.Status204NoContent); admin .MapDelete("/{id:guid}/shows/{showId:guid}", RemoveShow) .Produces(StatusCodes.Status204NoContent); admin.MapPut("/{id:guid}/order", Reorder).Produces(StatusCodes.Status204NoContent); admin.MapPut("/{id:guid}/poster-image", SetPoster).Produces(StatusCodes.Status204NoContent); return app; } private static async Task ListCollections( ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new ListCollectionsQuery(), cancellationToken); return Results.Ok(result); } private static async Task GetCollection( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new GetCollectionQuery(id), cancellationToken); return result.ToHttpResult(); } private static async Task CreateCollection( CreateCollectionCommand command, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(command, cancellationToken); return result.IsSuccess ? Results.Created( $"/api/admin/collections/{result.Value}", new CreatedIdResponse(result.Value) ) : result.ToHttpResult(); } private static async Task UpdateCollection( Guid id, UpdateCollectionBody body, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send( new UpdateCollectionCommand(id, body.Name, body.Description), cancellationToken ); return result.ToHttpResult(); } private static async Task DeleteCollection( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new DeleteCollectionCommand(id), cancellationToken); return result.ToHttpResult(); } private static async Task AddShow( Guid id, CollectionShowBody body, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send( new AddCollectionShowCommand(id, body.ShowId), cancellationToken ); return result.ToHttpResult(); } private static async Task RemoveShow( Guid id, Guid showId, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send( new RemoveCollectionShowCommand(id, showId), cancellationToken ); return result.ToHttpResult(); } private static async Task Reorder( Guid id, ReorderCollectionBody body, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send( new ReorderCollectionCommand(id, body.ShowIdsInOrder), cancellationToken ); return result.ToHttpResult(); } private static async Task SetPoster( Guid id, CollectionPosterBody body, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send( new SetCollectionPosterCommand(id, body.ImageId), cancellationToken ); return result.ToHttpResult(); } } public sealed record UpdateCollectionBody(string Name, string? Description); public sealed record CollectionShowBody(Guid ShowId); public sealed record ReorderCollectionBody(IReadOnlyList ShowIdsInOrder); public sealed record CollectionPosterBody(Guid? ImageId);