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.LMSBridge; using TMSDAL.DTO.LMSBridge; using static GB5Shared.GB5Constant.Constant; namespace TMSBLL.LMSBridge { public class LMSBridgeBLL : ILMSBridgeBLL { private readonly ILMSBridgeDAL _LMSBridgeDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public LMSBridgeBLL(ILMSBridgeDAL lmsBridgeDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _LMSBridgeDAL = lmsBridgeDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetLMSBridge(int lmsBridgeId, LoginDTO loginDTO) { try { var result = await _LMSBridgeDAL.GetLMSBridge(lmsBridgeId, loginDTO); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task SaveLMSBridge(LMSBridgeDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(dto.LMSBridgeCode, nameof(dto.LMSBridgeCode)); await _Validation.NotEmpty(dto.LMSBridgeName, nameof(dto.LMSBridgeName)); dto.TenantId = loginDTO.ClientId; dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; string message; if (dto.LMSBridgeId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "LMSBRIDGE", loginDTO); dto.LMSBridgeId = autoNumberDTO.StartNumber; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; await _LMSBridgeDAL.SaveLMSBridge(dto, loginDTO); message = string.Format(SuccessResponse.SaveSuccessWithCodeAndName, dto.LMSBridgeCode, dto.LMSBridgeName); } else { await _LMSBridgeDAL.UpdateLMSBridge(dto, loginDTO); message = string.Format(SuccessResponse.UpdateSuccessWithCodeAndName, dto.LMSBridgeCode, dto.LMSBridgeName); } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.LMSBridgeId, EntityConstant.OBJECTLMSBRIDGE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.LMSBridgeId > 0) await _AutoNumber.RollbackAutoNumber("LMSBRIDGE", dto.LMSBridgeId, loginDTO); throw; } } public async Task DeleteLMSBridge(int lmsBridgeId, LoginDTO loginDTO) { try { int result = await _LMSBridgeDAL.DeleteLMSBridge(lmsBridgeId, loginDTO); var cacheKey = new CacheKeyGeneration().KeyGeneration(lmsBridgeId, EntityConstant.OBJECTLMSBRIDGE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return result > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception) { throw; } } public async Task GetSelectListLMSBridge(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _LMSBridgeDAL.GetSelectListLMSBridge(loginDTO); } catch (Exception) { throw; } } public async Task GetLMSBridgeList(LoginDTO loginDTO) { try { return await _LMSBridgeDAL.GetLMSBridgeList(loginDTO); } catch (Exception) { throw; } } } }