using AccountsBLL.Voucher; using AccountsDAL.DTO.Voucher; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using System.Linq; using static GB5Shared.GB5Constant.Constant; namespace AccountsSL.EndPoints.Voucher { public class SaveBatchVoucher : BaseEndpoint> { private readonly IVoucherBLL _VoucherBLL; public SaveBatchVoucher(IVoucherBLL voucherBLL) { _VoucherBLL = voucherBLL; } public override void Configure() { Post("/Voucher/SaveBatchVoucher"); AllowAnonymous(); } public record SaveBatchVoucherParameters( [property: FromHeader] string Login, [property: FromBody] List Vouchers ); protected override async Task> ExecuteAsync(SaveBatchVoucherParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { var Result = await _VoucherBLL.SaveBatchVoucherAsync(req.Vouchers, LoginDTO!); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( string.Join("; ", Result), CacheKeyLevel.NOT_REQUIRED, LoginDTO); } catch (VoucherEndValidationException ex) { string message = string.Join("; ", ex.ValidationRows.Select(r => r.ValidationMessage)); return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, message, 422); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, ex.Message, 500); } } } }