WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using Events.FilesAPI.Infrastructure.Files;
|
||||
using Events.FilesAPI.Features.RegistrationsExcel.Synchronize;
|
||||
using Events.WebAPI.Handlers.EF.Data.Postgres;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MediatR;
|
||||
|
||||
namespace Events.FilesAPI.Features.RegistrationsExcel.Download;
|
||||
|
||||
public sealed class DownloadRegistrationsExcelHandler(
|
||||
EventsContext context,
|
||||
RegistrationsExcelFileGenerator generator,
|
||||
RegistrationsExcelFileLocator fileLocator) : IRequestHandler<DownloadRegistrationsExcelQuery, DownloadRegistrationsExcelResult>
|
||||
{
|
||||
public async Task<DownloadRegistrationsExcelResult> Handle(DownloadRegistrationsExcelQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
bool exists = await context.Events
|
||||
.AsNoTracking()
|
||||
.AnyAsync(e => e.Id == request.EventId, cancellationToken);
|
||||
|
||||
if (!exists)
|
||||
return new DownloadRegistrationsExcelResult(false, null);
|
||||
|
||||
GeneratedFileReference? file = fileLocator.TryGet(request.EventId);
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
await generator.GenerateAsync(request.EventId, cancellationToken);
|
||||
file = fileLocator.TryGet(request.EventId);
|
||||
}
|
||||
|
||||
return new DownloadRegistrationsExcelResult(true, file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Events.FilesAPI.Infrastructure.Files;
|
||||
using MediatR;
|
||||
|
||||
namespace Events.FilesAPI.Features.RegistrationsExcel.Download;
|
||||
|
||||
public sealed record DownloadRegistrationsExcelQuery(int EventId) : IRequest<DownloadRegistrationsExcelResult>;
|
||||
|
||||
public sealed record DownloadRegistrationsExcelResult(bool EventFound, GeneratedFileReference? File);
|
||||
@@ -0,0 +1,27 @@
|
||||
using Events.FilesAPI.Infrastructure.Files;
|
||||
using Events.FilesAPI.Infrastructure.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Events.FilesAPI.Features.RegistrationsExcel.Download;
|
||||
|
||||
public sealed class RegistrationsExcelFileLocator(
|
||||
IHostEnvironment hostEnvironment,
|
||||
IOptions<GeneratedFilesOptions> generatedFilesOptions)
|
||||
{
|
||||
public GeneratedFileReference? TryGet(int eventId)
|
||||
{
|
||||
string rootPath = Path.IsPathRooted(generatedFilesOptions.Value.OutputPath)
|
||||
? generatedFilesOptions.Value.OutputPath
|
||||
: Path.GetFullPath(Path.Combine(hostEnvironment.ContentRootPath, generatedFilesOptions.Value.OutputPath));
|
||||
|
||||
string excelPath = Path.Combine(rootPath, $"{eventId}.xlsx");
|
||||
if (!File.Exists(excelPath))
|
||||
return null;
|
||||
|
||||
return new GeneratedFileReference
|
||||
{
|
||||
FileName = Path.GetFileName(excelPath),
|
||||
PhysicalPath = excelPath
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user