using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using GB5Shared.ScanIdentifier; using WiBLL.ScanIdentifier; using static GB5Shared.GB5Constant.Constant; namespace WiSL.EndPoints.ScanIdentifier; // Print facility's batch action — renders one PDF (one page per selected row, sized to the // chosen label's real dimensions) and marks every row printed. Mirrors GenerateScanCode.cs's // BaseEndpoint + HttpContext.Response.SendStreamAsync + return null! pattern for streaming a // generated file back, rather than a JSON envelope. public class PrintBatch : BaseEndpoint> { private readonly IWiScanIdentifierBLL _bll; public PrintBatch(IWiScanIdentifierBLL bll) => _bll = bll; public override void Configure() { Post("/ScanIdentifier/PrintBatch"); AllowAnonymous(); } public record Parameters( [property: FromHeader] string Login, [property: FromBody] PrintBatchRequestDTO Request ); protected override string? GetCacheKey(Parameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync(Parameters req, LoginDTO login, CancellationToken ct) { try { var pdfBytes = await _bll.PrintBatchAsync(req.Request, login, ct).ConfigureAwait(false); using var ms = new MemoryStream(pdfBytes); await HttpContext.Response.SendStreamAsync(ms, "scan-labels.pdf", ms.Length, "application/pdf", cancellation: ct); return null!; } catch (InvalidOperationException ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 400); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, login, ex.Message, 500); } } }