using System.ComponentModel.DataAnnotations; using FrameworkDAL.CustomCode.Taxonomy; using FrameworkDAL.DTO.Taxonomy; 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 static GB5Shared.GB5Constant.Constant; namespace FrameworkBLL.Taxonomy { public class CourseBLL : ICourseBLL { private readonly ICourseDAL _CourseDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public CourseBLL(ICourseDAL courseDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _CourseDAL = courseDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetCourse(int courseId, LoginDTO loginDTO) { try { return await _CourseDAL.GetCourse(courseId, loginDTO); } catch (Exception) { throw; } } public async Task GetSelectListCourse(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _CourseDAL.GetSelectListCourse(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveCourse(CourseDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(dto.CourseCode, nameof(dto.CourseCode)); await _Validation.NotEmpty(dto.CourseName, nameof(dto.CourseName)); dto.TenantId = dto.IsShared == 1 ? -1 : loginDTO.ClientId; dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; string message; if (dto.CourseId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "COURSE", loginDTO); dto.CourseId = autoNumberDTO.StartNumber; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; await _CourseDAL.SaveCourse(dto, loginDTO); message = $"{SuccessResponse.SaveSuccessMessage} {dto.CourseId}"; } else { await _CourseDAL.UpdateCourse(dto, loginDTO); message = $"{SuccessResponse.UpdateSuccessMessage} {dto.CourseId}"; } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.CourseId, EntityConstant.OBJECTCOURSE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.CourseId > 0) await _AutoNumber.RollbackAutoNumber("COURSE", dto.CourseId, loginDTO); throw; } } public async Task DeleteCourse(int courseId, LoginDTO loginDTO) { try { var result = await _CourseDAL.DeleteCourse(courseId, loginDTO); var cacheKey = new CacheKeyGeneration().KeyGeneration(courseId, EntityConstant.OBJECTCOURSE, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return result; } catch (Exception) { throw; } } } }