using System.ComponentModel.DataAnnotations; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Validation; using MMDAL.CustomCode.NDTReport; using MMDAL.DTO.NDTReport; namespace MMBLL.NDTReport { public class NDTReportBLL : INDTReportBLL { private readonly INDTReportDAL _NDTReportDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public NDTReportBLL(INDTReportDAL NDTReportDAL, AutoNumber AutoNumber, IValidation Validation) { _NDTReportDAL = NDTReportDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetNDTReport(int NDTReportId, LoginDTO LoginDTO) { try { return await _NDTReportDAL.GetNDTReport(NDTReportId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListNDTReport(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _NDTReportDAL.GetSelectListNDTReport(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task SaveNDTReport(NDTReportDTO dto, LoginDTO login) { if (dto == null) throw new ArgumentNullException(nameof(dto)); try { // --------------------------- // VALIDATION // --------------------------- await _Validation.NotEmpty(dto.NDTReportReportNo, nameof(dto.NDTReportReportNo)); await _Validation.NotEmpty(dto.NDTReportNdtMethodId.ToString(), nameof(dto.NDTReportNdtMethodId)); await _Validation.NotEmpty(dto.NDTReportCustomerId.ToString(), nameof(dto.NDTReportCustomerId)); // --------------------------- // SYSTEM FIELDS // --------------------------- dto.NDTReportModifiedById = login.UserId; dto.NDTReportModifiedOn = DateTime.UtcNow; // ONLY SET CREATED ON INSERT if (dto.NDTReportId == 0) { dto.NDTReportCreatedById = login.UserId; dto.NDTReportCreatedOn = DateTime.UtcNow; } // --------------------------- // CALL DAL (IMPORTANT) // --------------------------- int reportId = await _NDTReportDAL.SaveNDTReport(dto, login); // --------------------------- // UPDATE DTO (IMPORTANT) // --------------------------- dto.NDTReportId = reportId; return "Details Saved Successfully."; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } } public async Task DeleteNDTReport(int NDTReportId, LoginDTO LoginDTO) { try { // Delete children first to maintain referential integrity await _NDTReportDAL.DeleteNDTConditionParam(NDTReportId, LoginDTO); await _NDTReportDAL.DeleteNDTItemResult(NDTReportId, LoginDTO); return await _NDTReportDAL.DeleteNDTReport(NDTReportId, LoginDTO); } catch (Exception) { throw; } } } }