using FrameworkDAL.CustomCode.Action; using FrameworkDAL.DTO.MAction; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.Resource.Response; using System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using GB5Shared.Validation; using GB5Shared.GenerateAutoNumber; namespace FrameworkBLL.Action { public class ActionBLL : IActionBLL { private readonly IActionDAL _ActionDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ActionBLL(IActionDAL ActionDAL, AutoNumber AutoNumber, IValidation Validation) { _ActionDAL = ActionDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetAction(int MActionId, LoginDTO loginDTO) { try { return await _ActionDAL.GetAction(MActionId, loginDTO); } catch (Exception) { throw; } } public async Task SaveAction(MActionDTO MActionDTO, LoginDTO LoginDTO) { if (MActionDTO == null) throw new ArgumentNullException(nameof(MActionDTO)); AutoNumberDTO? autoNumberDTO = null; try { MActionDTO.MActionModifiedById = LoginDTO.UserId; MActionDTO.MActionModifiedOn = DateTime.UtcNow; int count = 1; if (MActionDTO.MActionId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "MACTION", LoginDTO); MActionDTO.MActionId = autoNumberDTO.StartNumber; MActionDTO.MActionCreatedById = LoginDTO.UserId; MActionDTO.MActionCreatedOn = DateTime.UtcNow; await _ActionDAL.SaveAction(MActionDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {MActionDTO.MActionId}"; } else { await _ActionDAL.UpdateAction(MActionDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {MActionDTO.MActionId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && MActionDTO.MActionId > 0) await _AutoNumber.RollbackAutoNumber("MACTION", MActionDTO.MActionId, LoginDTO); throw; } } public async Task DeleteAction(int MActionId, LoginDTO LoginDTO) { try { return await _ActionDAL.DeleteAction(MActionId, LoginDTO); } catch (Exception) { throw; } } } }