using System.ComponentModel.DataAnnotations; using FrameworkBLL.Country; using FrameworkDAL.CustomCode.Country; using FrameworkDAL.DTO.Country; 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; namespace FrameworkBLL.Country { public class CountryBLL : ICountryBLL { private readonly ICountryDAL _CountryDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public CountryBLL(ICountryDAL CountryDAL, AutoNumber AutoNumber, IValidation Validation) { _CountryDAL = CountryDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetCountry(int countryId, LoginDTO LoginDTO) { try { return await _CountryDAL.GetCountry(countryId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveCountry(CountryDTO CountryDTO, LoginDTO LoginDTO) { if (CountryDTO == null) throw new ArgumentNullException(nameof(CountryDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(CountryDTO.CountryCode, nameof(CountryDTO.CountryCode)); await _Validation.NotEmpty(CountryDTO.CountryName, nameof(CountryDTO.CountryName)); CountryDTO.CountryModifiedById = LoginDTO.UserId; CountryDTO.CountryModifiedOn = DateTime.UtcNow; int count = 1; if (CountryDTO.CountryId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "COUNTRY", LoginDTO); CountryDTO.CountryId = autoNumberDTO.StartNumber; CountryDTO.CountryCreatedById = LoginDTO.UserId; CountryDTO.CountryCreatedOn = DateTime.UtcNow; await _CountryDAL.SaveCountry(CountryDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {CountryDTO.CountryId}"; } else { await _CountryDAL.UpdateCountry(CountryDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {CountryDTO.CountryId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && CountryDTO.CountryId > 0) await _AutoNumber.RollbackAutoNumber("COUNTRY", CountryDTO.CountryId, LoginDTO); throw; } } public async Task GetSelectListCountry(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _CountryDAL.GetSelectListCountry(criteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task DeleteCountry(int CountryId, LoginDTO LoginDTO) { try { return await _CountryDAL.DeleteCountry(CountryId, LoginDTO); } catch (Exception) { throw; } } } }