using FrameworkDAL.CustomCode.WebForm; using FrameworkDAL.DTO.Form; 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.WebForm { public class WebFormBLL : IWebFormBLL { private readonly IWebFormDAL _WebFormDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public WebFormBLL(IWebFormDAL WebFormDAL, AutoNumber AutoNumber, IValidation Validation) { _WebFormDAL = WebFormDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetWebForm(int WebFormId, LoginDTO LoginDTO) { try { return await _WebFormDAL.GetWebForm(WebFormId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveWebForm(WebFormDTO WebFormDTO, LoginDTO LoginDTO) { if (WebFormDTO == null) throw new ArgumentNullException(nameof(WebFormDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(WebFormDTO.WebFormName, nameof(WebFormDTO.WebFormName)); WebFormDTO.WebFormModifiedById = LoginDTO.UserId; WebFormDTO.WebFormModifiedOn = DateTime.UtcNow; int count = 1; if (WebFormDTO.WebFormId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "WebForm", LoginDTO); WebFormDTO.WebFormId = autoNumberDTO.StartNumber; WebFormDTO.WebFormCreatedById = LoginDTO.UserId; WebFormDTO.WebFormCreatedOn = DateTime.UtcNow; await _WebFormDAL.SaveWebForm(WebFormDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {WebFormDTO.WebFormId}"; } else { await _WebFormDAL.UpdateWebForm(WebFormDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {WebFormDTO.WebFormId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && WebFormDTO.WebFormId > 0) await _AutoNumber.RollbackAutoNumber("WebForm", WebFormDTO.WebFormId, LoginDTO); throw; } } public async Task GetSelectListWebForm(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _WebFormDAL.GetSelectListWebForm(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task DeleteWebForm(int WebFormId, LoginDTO LoginDTO) { try { return await _WebFormDAL.DeleteWebForm(WebFormId, LoginDTO); } catch (Exception) { throw; } } } }