using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.EventChannel; using FrameworkDAL.DTO.EventChannel; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.Validation; using System.ComponentModel.DataAnnotations; namespace FrameworkBLL.EventChannel { public class EventChannelBLL : IEventChannelBLL { private readonly IEventChannelDAL _EventChannelDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public EventChannelBLL(IEventChannelDAL EventChannelDAL, AutoNumber AutoNumber,IValidation Validation) { _EventChannelDAL = EventChannelDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetEventChannel(int EventChannelId, LoginDTO LoginDTO) { try { return await _EventChannelDAL.GetEventChannel(EventChannelId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveEventChannel(EventChannelDTO EventChannelDTO, LoginDTO LoginDTO) { if (EventChannelDTO == null) throw new ArgumentNullException(nameof(EventChannelDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(EventChannelDTO.EventChannelCode, nameof(EventChannelDTO.EventChannelCode)); await _Validation.NotEmpty(EventChannelDTO.EventChannelName, nameof(EventChannelDTO.EventChannelName)); EventChannelDTO.EventChannelModifiedById = LoginDTO.UserId; EventChannelDTO.EventChannelModifiedOn = DateTime.UtcNow; int InitialId = EventChannelDTO.EventChannelId; int count = 1 + InitialId; if (InitialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "EVENTCHANNEL", LoginDTO); EventChannelDTO.EventChannelId = AutoNumberDTO.StartNumber; EventChannelDTO.EventChannelCreatedById = LoginDTO.UserId; EventChannelDTO.EventChannelCreatedOn = DateTime.UtcNow; await _EventChannelDAL.SaveEventChannel(EventChannelDTO, LoginDTO); return $"Details saved successfully with EventChannel Id {EventChannelDTO.EventChannelId}."; } else { await _EventChannelDAL.UpdateEventChannel(EventChannelDTO, LoginDTO); return $"Details updated successfully with EventChannel Id {EventChannelDTO.EventChannelId}."; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && EventChannelDTO.EventChannelId > 0) await _AutoNumber.RollbackAutoNumber("EVENTCHANNEL", EventChannelDTO.EventChannelId, LoginDTO); throw; } } public async Task GetSelectListEventChannel(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _EventChannelDAL.GetSelectListEventChannel(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception ex) { throw new Exception("Error in GetSelectListEventChannel", ex); } } public async Task DeleteEventChannel(int EventChannelId, LoginDTO LoginDTO) { try { return await _EventChannelDAL.DeleteEventChannel(EventChannelId, LoginDTO); } catch (Exception) { throw; } } } }