WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using System.Globalization;
|
||||
using Events.FilesAPI.Infrastructure.Files;
|
||||
using Events.FilesAPI.Infrastructure.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Events.FilesAPI.Features.Certificates.Download;
|
||||
|
||||
public sealed class CertificateFileLocator(
|
||||
IHostEnvironment hostEnvironment,
|
||||
IOptions<GeneratedFilesOptions> generatedFilesOptions)
|
||||
{
|
||||
public GeneratedFileReference? TryGet(int eventId, int personId)
|
||||
{
|
||||
string rootPath = Path.IsPathRooted(generatedFilesOptions.Value.OutputPath)
|
||||
? generatedFilesOptions.Value.OutputPath
|
||||
: Path.GetFullPath(Path.Combine(hostEnvironment.ContentRootPath, generatedFilesOptions.Value.OutputPath));
|
||||
|
||||
string certificatePath = Path.Combine(
|
||||
rootPath,
|
||||
eventId.ToString(CultureInfo.InvariantCulture),
|
||||
$"{eventId}-{personId}.pdf");
|
||||
|
||||
if (!File.Exists(certificatePath))
|
||||
return null;
|
||||
|
||||
return new GeneratedFileReference
|
||||
{
|
||||
FileName = Path.GetFileName(certificatePath),
|
||||
PhysicalPath = certificatePath
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Events.FilesAPI.Infrastructure.Files;
|
||||
using MediatR;
|
||||
|
||||
namespace Events.FilesAPI.Features.Certificates.Download;
|
||||
|
||||
public sealed record DownloadCertificateQuery(int RegistrationId) : IRequest<DownloadCertificateResult>;
|
||||
|
||||
public sealed record DownloadCertificateResult(bool RegistrationFound, GeneratedFileReference? File);
|
||||
Reference in New Issue
Block a user