using GB5Shared.DaprCache; 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 Newtonsoft.Json; using System.ComponentModel.DataAnnotations; using TMSDAL.CustomCode.TrainingSession; using TMSDAL.DTO.TrainingProgramme; using static GB5Shared.GB5Constant.Constant; namespace TMSBLL.TrainingSession { public class TrainingProgrammeSessionBLL : ITrainingProgrammeSessionBLL { private readonly ITrainingProgrammeSessionDAL _DAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public TrainingProgrammeSessionBLL(ITrainingProgrammeSessionDAL dal, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _DAL = dal; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetProgrammeSessions(int programmeSessionId, LoginDTO loginDTO) { try { var result = await _DAL.GetProgrammeSessions(programmeSessionId, loginDTO); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task GetProgrammeSessionsList(LoginDTO loginDTO) { try { return await _DAL.GetProgrammeSessionsList(loginDTO); } catch (Exception) { throw; } } public async Task GetSelectListProgrammeSessions(CriteriaDTO CriteriaDTO, LoginDTO loginDTO) { try { return await _DAL.GetSelectListProgrammeSessions(CriteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveProgrammeSessions(ProgrammeSessionDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(dto.SessionCode, nameof(dto.SessionCode)); await _Validation.NotEmpty(dto.SessionTitle, nameof(dto.SessionTitle)); dto.TenantId = loginDTO.ClientId; string message; if (dto.ProgrammeSessionId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "PROGRAMMESESSION", loginDTO); dto.ProgrammeSessionId = autoNumberDTO.StartNumber; await _DAL.SaveProgrammeSessions(dto, loginDTO); message = string.Format(SuccessResponse.SaveSuccessWithCodeAndName, dto.SessionCode, dto.SessionTitle); } else { await _DAL.UpdateProgrammeSessions(dto, loginDTO); message = string.Format(SuccessResponse.UpdateSuccessWithCodeAndName, dto.SessionCode, dto.SessionTitle); } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.ProgrammeSessionId, EntityConstant.OBJECTPROGRAMMESESSION, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.ProgrammeSessionId > 0) await _AutoNumber.RollbackAutoNumber("PROGRAMMESESSION", dto.ProgrammeSessionId, loginDTO); throw; } } public async Task DeleteProgrammeSessions(int programmeSessionId, LoginDTO loginDTO) { try { var result = await _DAL.DeleteProgrammeSessions(programmeSessionId, loginDTO); var cacheKey = new CacheKeyGeneration().KeyGeneration(programmeSessionId, EntityConstant.OBJECTPROGRAMMESESSION, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return result; } catch (Exception) { throw; } } } }