using DMSBLL.ESSDocument;
using FastEndpoints;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using GB5Shared.FastEndPoint;
using static GB5Shared.GB5Constant.Constant;
namespace DMSSL.EndPoints.ESSDocument
{
///
/// Receives a multipart/form-data file upload from an employee.
/// Employee identity is always resolved from the Login header — never from form data.
/// Max upload size enforced at Kestrel level (configure in Program.cs if needed).
///
public class UploadDocument : BaseEndpoint>
{
public readonly IESSDocumentBLL _ESSDocumentBLL;
public UploadDocument(IESSDocumentBLL essDocumentBLL)
{
_ESSDocumentBLL = essDocumentBLL;
}
public record UploadDocumentParameters(
[property: FromHeader] string Login,
[property: QueryParam] int DocumentTypeId,
[property: QueryParam] int? PeriodYear,
[property: QueryParam] int? PeriodMonth,
[property: QueryParam] string? DisplayName
);
public override void Configure()
{
Post("/ESSDocument/UploadDocument");
AllowAnonymous();
AllowFileUploads();
}
protected override async Task> ExecuteAsync(
UploadDocumentParameters req, LoginDTO loginDTO, CancellationToken ct)
{
try
{
var file = HttpContext.Request.Form.Files.FirstOrDefault();
if (file is null || file.Length == 0)
throw new ArgumentException("No file provided or file is empty.");
await using var stream = file.OpenReadStream();
var result = await _ESSDocumentBLL.UploadDocument(
stream,
file.FileName,
file.ContentType,
file.Length,
req.DocumentTypeId,
req.PeriodYear,
req.PeriodMonth,
req.DisplayName,
loginDTO,
ct);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, loginDTO);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, loginDTO, ex.Message, 500);
}
}
}
}