using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkBLL.Project; using FrameworkDAL.CustomCode.Project; using FrameworkDAL.DTO.Project; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.ResponseStandard; using GB5Shared.GenerateAutoNumber; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.Validation; using GB5Shared.Resource.Response; using System.ComponentModel.DataAnnotations; namespace FrameworkBLL.Project { public class ProjectBLL : IProjectBLL { private readonly IProjectDAL _ProjectDAL; private readonly AutoNumber _autoNumber; private readonly IValidation _Validation; public ProjectBLL(IProjectDAL ProjectDAL, AutoNumber autoNumber, IValidation Validation) { _ProjectDAL = ProjectDAL; _autoNumber = autoNumber; _Validation = Validation; } public async Task GetProject(int ProjectId, LoginDTO loginDTO) { try { return await _ProjectDAL.GetProject(ProjectId, loginDTO); } catch (Exception) { throw; } } public async Task SaveProject(ProjectDTO ProjectDTO, LoginDTO LoginDTO) { if (ProjectDTO == null) throw new ArgumentNullException(nameof(ProjectDTO)); AutoNumberDTO autoNumberDTO = null; try { await _Validation.NotEmpty(ProjectDTO.ProjectCode, nameof(ProjectDTO.ProjectCode)); await _Validation.NotEmpty(ProjectDTO.ProjectName, nameof(ProjectDTO.ProjectName)); ProjectDTO.ProjectModifiedById = LoginDTO.UserId; ProjectDTO.ProjectModifiedOn = DateTime.UtcNow; if (ProjectDTO.ProjectId == 0) { autoNumberDTO = await _autoNumber.GetAutoNumber(1, "PROJECT", LoginDTO); ProjectDTO.ProjectId = autoNumberDTO.StartNumber; ProjectDTO.ProjectCreatedById = LoginDTO.UserId; ProjectDTO.ProjectCreatedOn = DateTime.UtcNow; await _ProjectDAL.SaveProject(ProjectDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {ProjectDTO.ProjectId}"; } else { await _ProjectDAL.UpdateProject(ProjectDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {ProjectDTO.ProjectId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception ex) { if (autoNumberDTO != null && ProjectDTO.ProjectId > 0) await _autoNumber.RollbackAutoNumber("PROJECT", ProjectDTO.ProjectId, LoginDTO); throw new Exception($"Failed: {ex.Message}", ex); } } public async Task GetSelectListProject(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _ProjectDAL.GetSelectListProject(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteProject(int ProjectId, LoginDTO LoginDTO) { try { return await _ProjectDAL.DeleteProject(ProjectId, LoginDTO); } catch (Exception) { throw; } } } }