Files
predavanja/Events-WebApi/Events.FilesAPI/Features/RegistrationsExcel/DownloadRegistrationsExcelController.cs
2026-05-12 02:20:00 +02:00

34 lines
1.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Events.Auth;
using Events.FilesAPI.Features.RegistrationsExcel.Download;
using MediatR;
using Microsoft.AspNetCore.Authorization;
namespace Events.FilesAPI.Features.RegistrationsExcel;
[ApiController]
[Authorize(Policy = nameof(Policies.ReadData))]
[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<IActionResult> 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);
}
}