using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.LoadTemplate; using FrameworkDAL.DTO.LoadTemplate; 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.LoadTemplate { public class LoadTemplateBLL : ILoadTemplateBLL { private readonly ILoadTemplateDAL _LoadTemplateDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public LoadTemplateBLL(ILoadTemplateDAL LoadTemplateDAL, AutoNumber AutoNumber, IValidation validation) { _LoadTemplateDAL = LoadTemplateDAL; _AutoNumber = AutoNumber; _Validation = validation; } public async Task GetLoadTemplate(int LoadTemplateId, LoginDTO LoginDTO) { try { return await _LoadTemplateDAL.GetLoadTemplate(LoadTemplateId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveLoadTemplate(LoadTemplateDTO LoadTemplateDTO, LoginDTO LoginDTO) { if (LoadTemplateDTO == null) throw new ArgumentNullException(nameof(LoadTemplateDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(LoadTemplateDTO.LoadTemplateTemplateName, nameof(LoadTemplateDTO.LoadTemplateTemplateName)); LoadTemplateDTO.LoadTemplateModifiedById = LoginDTO.UserId; LoadTemplateDTO.LoadTemplateModifiedOn = DateTime.UtcNow; int InitialId = LoadTemplateDTO.LoadTemplateId; int count = 1 + InitialId; if (InitialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "LOADTEMPLATE", LoginDTO); LoadTemplateDTO.LoadTemplateId = AutoNumberDTO.StartNumber; LoadTemplateDTO.LoadTemplateCreatedById = LoginDTO.UserId; LoadTemplateDTO.LoadTemplateCreatedOn = DateTime.UtcNow; await _LoadTemplateDAL.SaveLoadTemplate(LoadTemplateDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {LoadTemplateDTO.LoadTemplateId}"; } else { await _LoadTemplateDAL.UpdateLoadTemplate(LoadTemplateDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {LoadTemplateDTO.LoadTemplateId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && LoadTemplateDTO.LoadTemplateId > 0) await _AutoNumber.RollbackAutoNumber("LOADTEMPLATE", LoadTemplateDTO.LoadTemplateId, LoginDTO); throw; } } public async Task DeleteLoadTemplate(int LoadTemplateId, LoginDTO LoginDTO) { try { return await _LoadTemplateDAL.DeleteLoadTemplate(LoadTemplateId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListLoadTemplate(int FirstNumber, int MaxResult, CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _LoadTemplateDAL.GetSelectListLoadTemplate(FirstNumber, MaxResult, LoginDTO, criteriaDTO); } catch (Exception) { throw; } } } }