Files
predavanja/Events-WebApi/Events.FilesAPI/Features/Certificates/DownloadCertificateController.cs
2026-05-10 23:39:55 +02:00

31 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Events.FilesAPI.Features.Certificates.Download;
using MediatR;
namespace Events.FilesAPI.Features.Certificates;
[ApiController]
[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);
}
}