using System.ComponentModel.DataAnnotations; 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; using MMDAL.CustomCode.GateEntry; using MMDAL.DTO.GateEntry; namespace MMBLL.GateEntry { public class GateEntryBLL : IGateEntryBLL { private readonly IGateEntryDAL _GateEntryDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public GateEntryBLL(IGateEntryDAL GateEntryDAL, AutoNumber AutoNumber, IValidation Validation) { _GateEntryDAL = GateEntryDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetGateEntry(int GateEntryId, LoginDTO LoginDTO) { try { return await _GateEntryDAL.GetGateEntry(GateEntryId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveGateEntry(GateEntryDTO GateEntryDTO, LoginDTO LoginDTO) { if (GateEntryDTO == null) throw new ArgumentNullException(nameof(GateEntryDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(GateEntryDTO.GateEntryDate.ToString(), nameof(GateEntryDTO.GateEntryDate)); GateEntryDTO.OUId = LoginDTO.WorkOUId; GateEntryDTO.GateEntryModifiedById = LoginDTO.UserId; GateEntryDTO.GateEntryModifiedOn = DateTime.UtcNow; int count = 1; if (GateEntryDTO.GateEntryId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "GATEENTRY", LoginDTO); GateEntryDTO.GateEntryId = autoNumberDTO.StartNumber; GateEntryDTO.GateEntryCreatedById = LoginDTO.UserId; GateEntryDTO.GateEntryCreatedOn = DateTime.UtcNow; await _GateEntryDAL.SaveGateEntry(GateEntryDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {GateEntryDTO.GateEntryId}"; } else { await _GateEntryDAL.UpdateGateEntry(GateEntryDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {GateEntryDTO.GateEntryId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && GateEntryDTO.GateEntryId > 0) await _AutoNumber.RollbackAutoNumber("GATEENTRY", GateEntryDTO.GateEntryId, LoginDTO); throw; } } public async Task DeleteGateEntry(int GateEntryId, LoginDTO LoginDTO) { try { return await _GateEntryDAL.DeleteGateEntry(GateEntryId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListGateEntry(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _GateEntryDAL.GetSelectListGateEntry(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }