using CRMBLL.Action; using CRMDAL.DTO.Action; using FastEndpoints; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using static GB5Shared.GB5Constant.Constant; namespace CRMSL.EndPoints.Action { public class SaveAction : BaseEndpoint> { private readonly IActionBLL _ActionBLL; public SaveAction(IActionBLL ActionBLL) { _ActionBLL = ActionBLL; } public override void Configure() { Post("/Action/SaveAction"); AllowAnonymous(); } public record SaveActionParameters( [property: FromHeader] string Login, [property: FromBody] TActionDTO TActionDTO ); protected override string? GetCacheKey(SaveActionParameters req, LoginDTO login) => null; protected override async Task> ExecuteAsync( SaveActionParameters req, LoginDTO LoginDTO, CancellationToken ct) { try { if (req.TActionDTO == null) throw new ArgumentNullException(nameof(req.TActionDTO), "Action data is required."); if (req.TActionDTO.BIZTransactionTypeId == -1 || req.TActionDTO.BIZTransactionTypeId == 0) throw new ArgumentException("BIZTransactionTypeId is required."); if (req.TActionDTO.TActionTransactionDate == default) throw new ArgumentException("Transaction date is required."); bool isNew = req.TActionDTO.TActionId == 0; if (isNew && (req.TActionDTO.ActionDetailArray == null || req.TActionDTO.ActionDetailArray.Count == 0)) throw new ArgumentException("At least one action detail is required."); var result = await _ActionBLL.SaveAction(req.TActionDTO, LoginDTO); string message = isNew ? $"Details Saved Successfully With Number: {result}" : $"Details Updated Successfully With Number: {result}"; return await GB5Shared.ResponseStandard.Response .CreateSuccessResponse(message, CacheKeyLevel.NOT_REQUIRED, LoginDTO); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response .CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO, ex.Message, 500); } } } }