using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.Parameter; using FrameworkDAL.DTO.Parameter; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.SuccessORError; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; namespace FrameworkBLL.Parameter { public class ParameterBLL : IParameterBLL { private readonly IParameterDAL _ParameterDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ParameterBLL(IParameterDAL IParameterDAL, AutoNumber AutoNumber, IValidation Validation) { _ParameterDAL = IParameterDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetParameter (int ParameterId, LoginDTO LoginDTO) { try { return await _ParameterDAL.GetParameter(ParameterId, LoginDTO); } catch(Exception) { throw; } } public async Task SaveParameter(ParameterDTO ParameterDTO, LoginDTO LoginDTO) { if (ParameterDTO == null) throw new ArgumentNullException(nameof(ParameterDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(ParameterDTO.ParameterCode, nameof(ParameterDTO.ParameterCode)); await _Validation.NotEmpty(ParameterDTO.ParameterName, nameof(ParameterDTO.ParameterName)); ParameterDTO.ParameterModifiedById = LoginDTO.UserId; ParameterDTO.ParameterModifiedOn = DateTime.UtcNow; int initialId = ParameterDTO.ParameterId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "PARAMETER", LoginDTO); ParameterDTO.ParameterId = AutoNumberDTO.StartNumber; ParameterDTO.ParameterCreatedById = LoginDTO.UserId; ParameterDTO.ParameterCreatedOn = DateTime.UtcNow; await _ParameterDAL.SaveParameter(ParameterDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {ParameterDTO.ParameterId}"; } else { await _ParameterDAL.UpdateParameter(ParameterDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {ParameterDTO.ParameterId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && ParameterDTO.ParameterId > 0) await _AutoNumber.RollbackAutoNumber("PARAMETER", ParameterDTO.ParameterId, LoginDTO); throw; } } public async Task DeleteParameter(int ParameterId, LoginDTO LoginDTO) { try { return await _ParameterDAL.DeleteParameter(ParameterId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListParameter(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _ParameterDAL.GetSelectListParameter(criteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }