using FrameworkDAL.CustomCode.FormulaField; using FrameworkDAL.DTO.Formula; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FrameworkBLL.FormulaField { public class FormulaFieldBLL : IFormulaFieldBLL { private readonly IFormulaFieldDAL _FormulaFieldDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public FormulaFieldBLL(IFormulaFieldDAL FormulaFieldDAL, AutoNumber AutoNumber, IValidation Validation) { _FormulaFieldDAL = FormulaFieldDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetFormulaField(int FormulaField, LoginDTO LoginDTO) { try { return await _FormulaFieldDAL.GetFormulaField(FormulaField, LoginDTO); } catch (Exception) { throw; } } public async Task SaveFormulaField(FormulaFieldDTO FormulaFieldDTO, LoginDTO LoginDTO) { if (FormulaFieldDTO == null) throw new ArgumentNullException(nameof(FormulaFieldDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(FormulaFieldDTO.FormulaFieldCode, nameof(FormulaFieldDTO.FormulaFieldCode)); await _Validation.NotEmpty(FormulaFieldDTO.FormulaFieldName, nameof(FormulaFieldDTO.FormulaFieldName)); FormulaFieldDTO.FormulaFieldModifiedById = LoginDTO.UserId; FormulaFieldDTO.FormulaFieldModifiedOn = DateTime.UtcNow; int count = 1; if (FormulaFieldDTO.FormulaFieldId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "FormulaField", LoginDTO); FormulaFieldDTO.FormulaFieldId = autoNumberDTO.StartNumber; FormulaFieldDTO.FormulaFieldCreatedById = LoginDTO.UserId; FormulaFieldDTO.FormulaFieldCreatedOn = DateTime.UtcNow; await _FormulaFieldDAL.SaveFormulaField(FormulaFieldDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {FormulaFieldDTO.FormulaFieldId}"; } else { await _FormulaFieldDAL.UpdateFormulaField(FormulaFieldDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {FormulaFieldDTO.FormulaFieldId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && FormulaFieldDTO.FormulaFieldId > 0) await _AutoNumber.RollbackAutoNumber("FormulaField", FormulaFieldDTO.FormulaFieldId, LoginDTO); throw; } } public async Task DeleteFormulaField(int FormulaField, LoginDTO LoginDTO) { try { return await _FormulaFieldDAL.DeleteFormulaField(FormulaField, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListFormulaField(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _FormulaFieldDAL.GetSelectListFormulaField(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListFormulaFieldSection(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _FormulaFieldDAL.GetSelectListFormulaFieldSection(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }