using Microsoft.AspNetCore.Mvc; using Events.FilesAPI.Features.RegistrationsExcel.Download; using MediatR; namespace Events.FilesAPI.Features.RegistrationsExcel; [ApiController] [Route("Events")] public class DownloadRegistrationsExcelController : ControllerBase { private const string XlsxContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; [HttpGet("{id}/RegistrationsExcel")] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task DownloadRegistrationsExcel( int id, [FromServices] IMediator mediator, CancellationToken cancellationToken) { DownloadRegistrationsExcelResult result = await mediator.Send(new DownloadRegistrationsExcelQuery(id), cancellationToken); if (!result.EventFound) return Problem(statusCode: StatusCodes.Status404NotFound, detail: $"Invalid id = {id}"); if (result.File == null) return Problem(statusCode: StatusCodes.Status404NotFound, detail: "Registrations Excel could not be generated."); return PhysicalFile(result.File.PhysicalPath, XlsxContentType, result.File.FileName); } }