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

34 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Events.Auth;
using Events.FilesAPI.Features.Certificates.Download;
using MediatR;
using Microsoft.AspNetCore.Authorization;
namespace Events.FilesAPI.Features.Certificates;
[ApiController]
[Authorize(Policy = nameof(Policies.ReadData))]
[Route("Registrations")]
public class DownloadCertificateController : ControllerBase
{
private const string PdfContentType = "application/pdf";
[HttpGet("{id}/Certificate")]
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DownloadCertificate(
int id,
[FromServices] IMediator mediator,
CancellationToken cancellationToken)
{
DownloadCertificateResult result = await mediator.Send(new DownloadCertificateQuery(id), cancellationToken);
if (!result.RegistrationFound)
return Problem(statusCode: StatusCodes.Status404NotFound, detail: $"Invalid id = {id}");
if (result.File == null)
return Problem(statusCode: StatusCodes.Status404NotFound, detail: "Certificate could not be generated.");
return PhysicalFile(result.File.PhysicalPath, PdfContentType, result.File.FileName);
}
}