36 lines
1.3 KiB
C#
36 lines
1.3 KiB
C#
using Events.FilesAPI.Infrastructure.Files;
|
|
using Events.FilesAPI.Features.Certificates.Synchronize;
|
|
using Events.WebAPI.Handlers.EF.Data.Postgres;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Events.FilesAPI.Features.Certificates.Download;
|
|
|
|
public sealed class DownloadCertificateHandler(
|
|
EventsContext context,
|
|
CertificateFileGenerator generator,
|
|
CertificateFileLocator fileLocator) : IRequestHandler<DownloadCertificateQuery, DownloadCertificateResult>
|
|
{
|
|
public async Task<DownloadCertificateResult> Handle(DownloadCertificateQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var registration = await context.Registrations
|
|
.AsNoTracking()
|
|
.Where(r => r.Id == request.RegistrationId)
|
|
.Select(r => new { r.EventId, r.PersonId })
|
|
.SingleOrDefaultAsync(cancellationToken);
|
|
|
|
if (registration == null)
|
|
return new DownloadCertificateResult(false, null);
|
|
|
|
GeneratedFileReference? file = fileLocator.TryGet(registration.EventId, registration.PersonId);
|
|
|
|
if (file == null)
|
|
{
|
|
await generator.GenerateAsync(registration.EventId, registration.PersonId, cancellationToken);
|
|
file = fileLocator.TryGet(registration.EventId, registration.PersonId);
|
|
}
|
|
|
|
return new DownloadCertificateResult(true, file);
|
|
}
|
|
}
|