using System.ComponentModel.DataAnnotations; using FrameworkDAL.CustomCode.EventSource; using FrameworkDAL.DTO.Event; 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.EventSource { public class EventSourceBLL : IEventSourceBLL { private readonly IEventSourceDAL _EventSourceDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public EventSourceBLL(IEventSourceDAL EventSourceDAL, AutoNumber AutoNumber, IValidation Validation) { _EventSourceDAL = EventSourceDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetSelectListEventSource(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _EventSourceDAL.GetSelectListEventSource(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception ex) { throw new Exception("Error in GetSelectListEventSource", ex); } } public async Task GetEventSource(int EventSourceId, LoginDTO LoginDTO) { try { return await _EventSourceDAL.GetEventSource(EventSourceId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveEventSource(EventSourceDTO EventSourceDTO, LoginDTO LoginDTO) { if (EventSourceDTO == null) throw new ArgumentNullException(nameof(EventSourceDTO)); AutoNumberDTO? AutoNumberDTO = null; try { await _Validation.NotEmpty(EventSourceDTO.EventSourceCode, nameof(EventSourceDTO.EventSourceCode)); await _Validation.NotEmpty(EventSourceDTO.EventSourceName, nameof(EventSourceDTO.EventSourceName)); EventSourceDTO.EventSourceModifiedById = LoginDTO.UserId; EventSourceDTO.EventSourceModifiedOn = DateTime.UtcNow; EventSourceDTO.EventSourceModifiedByName = LoginDTO.UserName; int initialId = EventSourceDTO.EventSourceId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO autoNumberDTO = await _AutoNumber.GetNumberAsync(count, "EVENTSOURCE", LoginDTO); EventSourceDTO.EventSourceId = AutoNumberDTO.StartNumber; EventSourceDTO.EventSourceCreatedById = LoginDTO.UserId; EventSourceDTO.EventSourceCreatedOn = DateTime.UtcNow; EventSourceDTO.EventSourceCreatedByName = LoginDTO.UserName; await _EventSourceDAL.SaveEventSource(EventSourceDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {EventSourceDTO.EventSourceId}"; } else { await _EventSourceDAL.UpdateEventSource(EventSourceDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {EventSourceDTO.EventSourceId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (AutoNumberDTO != null && EventSourceDTO.EventSourceId > 0) await _AutoNumber.RollbackAutoNumber("EVENTSOURCE", EventSourceDTO.EventSourceId, LoginDTO); throw; } } public async Task DeleteEventSource(int EventSourceId, LoginDTO LoginDTO) { try { return await _EventSourceDAL.DeleteEventSource(EventSourceId, LoginDTO); } catch (Exception) { throw; } } } }