using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using GB5Shared.Criteria.DTO; using GB5Shared.Criteria.Query; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GB5Exception; using GB5Shared.QueryExecutor; using Microsoft.Extensions.Caching.Memory; namespace GB5Shared.Criteria { public class CriteriaDAL : ICriteriaDAL { private readonly IMemoryCache _cache; private readonly IQueryExecutor _queryExecutor; public CriteriaDAL(IQueryExecutor queryExecutor, IMemoryCache cache) { _queryExecutor = queryExecutor; _cache = cache; } public async Task> FilteredObjectAsync(string objectName, LoginDTO login) { try { var allEntities = await GetFromCacheAsync(login); var result = allEntities .Where(e => e.EntityCode.Equals(objectName, StringComparison.OrdinalIgnoreCase)) .ToList(); if (!result.Any()) { throw new NotFoundException( $"Entity entry not found for object name >> {objectName}"); } return result; } catch (Exception) { throw; } } private async Task> GetFromCacheAsync(LoginDTO login) { try { string cacheKey = $"GenBLLObjectContent_{login.DatabaseName}"; if (_cache.TryGetValue(cacheKey, out List? cachedData)) { return cachedData!; } var data = await GetEntityForGenericAsync(login); var cacheOptions = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30), SlidingExpiration = TimeSpan.FromMinutes(10), Priority = CacheItemPriority.High }; _cache.Set(cacheKey, data, cacheOptions); return data; } catch (Exception) { throw; } } private async Task> GetEntityForGenericAsync(LoginDTO login) { try { return (await _queryExecutor .QueryAsync(login, CriteriaEntityQB.GET_ENTITY_FOR_GENERIC, null!)) .ToList(); } catch (Exception) { throw; } } public void ClearCache(string databaseName) { string cacheKey = $"GenBLLObjectContent_{databaseName}"; _cache.Remove(cacheKey); } public async Task AddTenantIdFiler(CriteriaDTO SupplieCriteria, byte IsMultitenant, Type ObjectType, LoginDTO LoginDTO) { try { if (IsMultitenant == 1) { return SupplieCriteria; } var resultCriteria = new CriteriaDTO(); var AttributesCriteriaDTOs = new List { new AttributesCriteriaDTO { FieldName = "BaseTenantId", OperationType = CriteriaDTO.OperationType.In, FieldValue = LoginDTO.ClientId + ",-1" } }; if (SupplieCriteria == null) { var SectionCriteriaDTO = new SectionCriteriaDTO { OperationType = CriteriaDTO.SectionOperationType.None, AttributesCriteriaList = AttributesCriteriaDTOs }; resultCriteria.SectionCriteriaList = new List { SectionCriteriaDTO }; } else { var SectionCriteriaDTO = new SectionCriteriaDTO { AttributesCriteriaList = AttributesCriteriaDTOs, OperationType = CriteriaDTO.SectionOperationType.None }; SupplieCriteria.SectionCriteriaList[SupplieCriteria.SectionCriteriaList.Count - 1].OperationType = CriteriaDTO.SectionOperationType.And; SupplieCriteria.SectionCriteriaList.Add(SectionCriteriaDTO); resultCriteria = SupplieCriteria; } return resultCriteria; } catch (Exception) { throw; } } } }