using System.ComponentModel.DataAnnotations; using FrameworkDAL.CustomCode.DataAlert; using FrameworkDAL.DTO.DataAlert; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; namespace FrameworkBLL.DataAlert { public class DataAlertBLL : IDataAlertBLL { private readonly IDataAlertDAL _DataAlertDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public DataAlertBLL(IDataAlertDAL DataAlertDAL, AutoNumber AutoNumber, IValidation Validation) { _DataAlertDAL = DataAlertDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task> GetDataAlert(int dataAlertId, LoginDTO loginDTO) { try { return await _DataAlertDAL.GetDataAlert(dataAlertId, loginDTO); } catch (Exception) { throw; } } public async Task GetSelectListDataAlert(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _DataAlertDAL.GetSelectListDataAlert(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveDataAlert(DataAlertDTO DataAlertDTO, LoginDTO LoginDTO) { if (DataAlertDTO == null) throw new ArgumentNullException(nameof(DataAlertDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(DataAlertDTO.DataAlertName, nameof(DataAlertDTO.DataAlertName)); DataAlertDTO.DataAlertModifiedById = LoginDTO.UserId; DataAlertDTO.DataAlertModifiedOn = DateTime.UtcNow; int count = 1; if (DataAlertDTO.DataAlertId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "DATAALERT", LoginDTO); DataAlertDTO.DataAlertId = autoNumberDTO.StartNumber; DataAlertDTO.DataAlertCreatedById = LoginDTO.UserId; DataAlertDTO.DataAlertCreatedOn = DateTime.UtcNow; await _DataAlertDAL.SaveDataAlert(DataAlertDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {DataAlertDTO.DataAlertId}"; } else { await _DataAlertDAL.UpdateDataAlert(DataAlertDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {DataAlertDTO.DataAlertId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && DataAlertDTO.DataAlertId > 0) await _AutoNumber.RollbackAutoNumber("DATAALERT", DataAlertDTO.DataAlertId, LoginDTO); throw; } } public async Task DeleteDataAlert(int DataAlertId, LoginDTO LoginDTO) { try { return await _DataAlertDAL.DeleteDataAlert(DataAlertId, LoginDTO); } catch (Exception) { throw; } } } }