using System.ComponentModel.DataAnnotations; using FrameworkDAL.CustomCode.Taxonomy; using FrameworkDAL.DTO.Taxonomy; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; 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 TopicLinkBLL : ITopicLinkBLL { private readonly ITopicLinkDAL _TopicLinkDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; public TopicLinkBLL(ITopicLinkDAL topicLinkDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate) { _TopicLinkDAL = topicLinkDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; } public async Task GetTopicLinksByEntity(int entityType, int entityId, LoginDTO loginDTO) { try { return await _TopicLinkDAL.GetTopicLinksByEntity(entityType, entityId, loginDTO); } catch (Exception) { throw; } } public async Task GetTopicLinksByTopic(int topicId, LoginDTO loginDTO) { try { return await _TopicLinkDAL.GetTopicLinksByTopic(topicId, loginDTO); } catch (Exception) { throw; } } public async Task SaveTopicLink(TopicLinkDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.Range(1, int.MaxValue, dto.TopicId, nameof(dto.TopicId)); await _Validation.Range(1, int.MaxValue, dto.EntityType, nameof(dto.EntityType)); await _Validation.Range(1, int.MaxValue, dto.EntityId, nameof(dto.EntityId)); dto.TenantId = loginDTO.ClientId; if (dto.TopicLinkId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "TOPICLINK", loginDTO); dto.TopicLinkId = autoNumberDTO.StartNumber; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; await _TopicLinkDAL.SaveTopicLink(dto, loginDTO); } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.TopicLinkId, EntityConstant.OBJECTTOPICLINK, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return $"{SuccessResponse.SaveSuccessMessage} {dto.TopicLinkId}"; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.TopicLinkId > 0) await _AutoNumber.RollbackAutoNumber("TOPICLINK", dto.TopicLinkId, loginDTO); throw; } } public async Task DeleteTopicLink(int topicLinkId, LoginDTO loginDTO) { try { var result = await _TopicLinkDAL.DeleteTopicLink(topicLinkId, loginDTO); var cacheKey = new CacheKeyGeneration().KeyGeneration(topicLinkId, EntityConstant.OBJECTTOPICLINK, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return result; } catch (Exception) { throw; } } } }