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 System.ComponentModel.DataAnnotations; using TMSDAL.CustomCode.TrainerProfile; using TMSDAL.DTO.TrainerProfile; using static GB5Shared.GB5Constant.Constant; namespace TMSBLL.TrainerProfile { public class TrainerProfileBLL : ITrainerProfileBLL { private readonly ITrainerProfileDAL _TrainerProfileDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public TrainerProfileBLL(ITrainerProfileDAL trainerProfileDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _TrainerProfileDAL = trainerProfileDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetTrainerProfile(int trainerId, LoginDTO loginDTO) { try { return await _TrainerProfileDAL.GetTrainerProfile(trainerId, loginDTO); } catch (Exception) { throw; } } public async Task SaveTrainerProfile(TrainerProfileDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(dto.TrainerCode, nameof(dto.TrainerCode)); await _Validation.NotEmpty(dto.TrainerFullName, nameof(dto.TrainerFullName)); if (dto.TrainerType == 0) // Internal { if (dto.EmployeeId == null || dto.EmployeeId == 0) throw new ValidationException("EmployeeId is required for Internal trainers."); dto.ExternalVendorId = -1; } else if (dto.TrainerType == 1) // External { if (dto.ExternalVendorId == null || dto.ExternalVendorId == 0) throw new ValidationException("ExternalVendorId is required for External trainers."); dto.EmployeeId = -1; } else { throw new ValidationException("TrainerType must be 0 (Internal) or 1 (External)."); } dto.TenantId = loginDTO.ClientId; dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; string message; if (dto.TrainerId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "TRAINERPROFILE", loginDTO); dto.TrainerId = autoNumberDTO.StartNumber; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; await _TrainerProfileDAL.SaveTrainerProfile(dto, loginDTO); message = string.Format(SuccessResponse.SaveSuccessWithCodeAndName, dto.TrainerCode, dto.TrainerFullName); } else { await _TrainerProfileDAL.UpdateTrainerProfile(dto, loginDTO); message = string.Format(SuccessResponse.UpdateSuccessWithCodeAndName, dto.TrainerCode, dto.TrainerFullName); } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.TrainerId, EntityConstant.OBJECTTRAINERPROFILE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.TrainerId > 0) await _AutoNumber.RollbackAutoNumber("TRAINERPROFILE", dto.TrainerId, loginDTO); throw; } } public async Task GetSelectList(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _TrainerProfileDAL.GetSelectList(criteriaDTO, login, ct); public async Task GetTrainerIdByEmployeeId(int employeeId, LoginDTO login, CancellationToken ct) => await _TrainerProfileDAL.GetTrainerIdByEmployeeId(employeeId, login, ct); public async Task DeleteTrainerProfile(int trainerId, LoginDTO loginDTO) { try { var result = await _TrainerProfileDAL.DeleteTrainerProfile(trainerId, loginDTO); var cacheKey = new CacheKeyGeneration().KeyGeneration(trainerId, EntityConstant.OBJECTTRAINERPROFILE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return result; } catch (Exception) { throw; } } public async Task GetSelectListTrainerProfile(LoginDTO loginDTO) { try { return await _TrainerProfileDAL.GetSelectListTrainerProfile(loginDTO); } catch (Exception) { throw; } } } }