using FrameworkDAL.CustomCode.State; using FrameworkDAL.DTO.State; 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; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FrameworkBLL.State { public class StateBLL : IStateBLL { private readonly IStateDAL _StateDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public StateBLL(IStateDAL IStateDAL, AutoNumber AutoNumber, IValidation Validation) { _StateDAL = IStateDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetSelectListState(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _StateDAL.GetSelectListState(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task GetState(int StateId, LoginDTO LoginDTO) { try { return await _StateDAL.GetState(StateId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveState(StateDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { // Validations await _Validation.NotEmpty(dto.StateCode, nameof(dto.StateCode)); await _Validation.NotEmpty(dto.StateName, nameof(dto.StateName)); dto.StateModifiedById = loginDTO.UserId; dto.StateModifiedOn = DateTime.UtcNow; if (dto.StateId == 0) { // Auto Number Generate autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "STATE", loginDTO); dto.StateId = autoNumberDTO.StartNumber; dto.StateCreatedById = loginDTO.UserId; dto.StateCreatedOn = DateTime.UtcNow; await _StateDAL.SaveState(dto, loginDTO); return $"{SuccessResponse.SaveSuccessMessage} {dto.StateId}"; } else { await _StateDAL.UpdateState(dto, loginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {dto.StateId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception ex) { if (autoNumberDTO != null && dto.StateId > 0) await _AutoNumber.RollbackAutoNumber("STATE", dto.StateId, loginDTO); throw new Exception($"Failed: {ex.Message}", ex); } } public async Task DeleteState(int StateId, LoginDTO LoginDTO) { try { return await _StateDAL.DeleteState(StateId, LoginDTO); } catch (Exception) { throw; } } } }