using LiteCqrs; using TeleWave.Api.Common; using TeleWave.Application.Library; using TeleWave.Application.Library.AddEpisode; using TeleWave.Application.Library.CreateShow; using TeleWave.Application.Library.DeleteShow; using TeleWave.Application.Library.GetShow; using TeleWave.Application.Library.ListShows; using TeleWave.Application.Library.RemoveEpisode; using TeleWave.Infrastructure.Identity; namespace TeleWave.Api.Endpoints; public static class ShowEndpoints { public static IEndpointRouteBuilder MapShowEndpoints(this IEndpointRouteBuilder app) { var admin = app.MapGroup("/api/admin/shows") .WithTags("Admin.Shows") .RequireAuthorization(policy => policy.RequireRole(RoleNames.Admin)); admin.MapPost("", CreateShow).Produces(StatusCodes.Status201Created); admin.MapGet("", ListShows).Produces>(); admin.MapGet("/{id:guid}", GetShow).Produces(); admin.MapDelete("/{id:guid}", DeleteShow).Produces(StatusCodes.Status204NoContent); admin .MapPost("/{id:guid}/episodes", AddEpisode) .Produces(StatusCodes.Status201Created); admin .MapDelete("/{id:guid}/episodes/{episodeId:guid}", RemoveEpisode) .Produces(StatusCodes.Status204NoContent); return app; } private static async Task CreateShow( CreateShowCommand command, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(command, cancellationToken); return result.IsSuccess ? Results.Created($"/api/admin/shows/{result.Value}", new CreatedIdResponse(result.Value)) : result.ToHttpResult(); } private static async Task ListShows(ISender sender, CancellationToken cancellationToken) { var result = await sender.Send(new ListShowsQuery(), cancellationToken); return Results.Ok(result); } private static async Task GetShow( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new GetShowQuery(id), cancellationToken); return result.ToHttpResult(); } private static async Task DeleteShow( Guid id, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new DeleteShowCommand(id), cancellationToken); return result.ToHttpResult(); } private static async Task AddEpisode( Guid id, AddEpisodeBody body, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new AddEpisodeCommand(id, body.MediaAssetId), cancellationToken); return result.IsSuccess ? Results.Created($"/api/admin/shows/{id}", new CreatedIdResponse(result.Value)) : result.ToHttpResult(); } private static async Task RemoveEpisode( Guid id, Guid episodeId, ISender sender, CancellationToken cancellationToken ) { var result = await sender.Send(new RemoveEpisodeCommand(id, episodeId), cancellationToken); return result.ToHttpResult(); } } public sealed record AddEpisodeBody(Guid MediaAssetId);