public virtual async Task <PricelistSearchResult> SearchPricelistsAsync(PricelistSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), nameof(SearchPricelistsAsync), criteria.GetCacheKey()); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry => { cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken()); var result = AbstractTypeFactory <PricelistSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var query = BuildQuery(repository, criteria); var sortInfos = BuildSortExpression(criteria); result.TotalCount = await query.CountAsync(); if (criteria.Take > 0) { var pricelistIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id) .Select(x => x.Id) .Skip(criteria.Skip).Take(criteria.Take) .AsNoTracking() .ToArrayAsync(); var unorderedResults = await _pricingService.GetPricelistsByIdAsync(pricelistIds); result.Results = unorderedResults.OrderBy(x => Array.IndexOf(pricelistIds, x.Id)).ToList(); } } return result; })); }
public virtual async Task <PricelistAssignmentSearchResult> SearchPricelistAssignmentsAsync(PricelistAssignmentsSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), nameof(SearchPricelistAssignmentsAsync), criteria.GetCacheKey()); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry => { cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken()); cacheEntry.AddExpirationToken(PricingCacheRegion.CreateChangeToken()); var retVal = AbstractTypeFactory <PricelistAssignmentSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var query = repository.PricelistAssignments; if (!criteria.PriceListIds.IsNullOrEmpty()) { query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId)); } if (!string.IsNullOrEmpty(criteria.Keyword)) { query.Where(x => x.Name.Contains(criteria.Keyword) || x.Description.Contains(criteria.Keyword)); } var sortInfos = criteria.SortInfos; if (sortInfos.IsNullOrEmpty()) { sortInfos = new[] { new SortInfo { SortColumn = ReflectionUtility.GetPropertyName <PricelistAssignment>(x => x.Priority) } }; } query = query.OrderBySortInfos(sortInfos); retVal.TotalCount = await query.CountAsync(); if (criteria.Take > 0) { query = query.Skip(criteria.Skip).Take(criteria.Take); var pricelistAssignmentsIds = await query.Select(x => x.Id).ToListAsync(); retVal.Results = (await _pricingService.GetPricelistAssignmentsByIdAsync(pricelistAssignmentsIds.ToArray())) .OrderBy(x => pricelistAssignmentsIds.IndexOf(x.Id)) .ToList(); } } return retVal; })); }
public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey()); return(_platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry => { cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken()); cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken()); var result = AbstractTypeFactory <PriceSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var query = await BuildQueryAsync(repository, criteria); var sortInfos = BuildSortExpression(criteria); //Try to replace sorting columns names TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos); if (criteria.GroupByProducts) { var groupedQuery = query.Select(x => x.ProductId).OrderBy(x => x).Distinct(); result.TotalCount = await groupedQuery.CountAsync(); if (criteria.Take > 0) { query = query.Where(x => groupedQuery.Contains(x.ProductId)); } } else { result.TotalCount = await query.CountAsync(); } if (criteria.Take > 0) { var priceIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id) .Skip(criteria.Skip).Take(criteria.Take) .Select(x => x.Id) .AsNoTracking() .ToArrayAsync(); var unorderedResults = await _pricingService.GetPricesByIdAsync(priceIds); result.Results = unorderedResults.OrderBy(x => Array.IndexOf(priceIds, x.Id)).ToList(); } } return result; })); }
private void ResetCache() { //Clear cache (Smart cache implementation) PricingCacheRegion.ExpireRegion(); PricingSearchCacheRegion.ExpireRegion(); }
public virtual Task <PriceSearchResult> SearchPricesAsync(PricesSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), nameof(SearchPricesAsync), criteria.GetCacheKey()); return(_platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry => { cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken()); cacheEntry.AddExpirationToken(PricingSearchCacheRegion.CreateChangeToken()); var retVal = AbstractTypeFactory <PriceSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var query = repository.Prices; if (!criteria.PriceListIds.IsNullOrEmpty()) { query = query.Where(x => criteria.PriceListIds.Contains(x.PricelistId)); } if (!criteria.ProductIds.IsNullOrEmpty()) { query = query.Where(x => criteria.ProductIds.Contains(x.ProductId)); } if (criteria.ModifiedSince.HasValue) { query = query.Where(x => x.ModifiedDate >= criteria.ModifiedSince); } var sortInfos = criteria.SortInfos.ToArray(); if (sortInfos.IsNullOrEmpty()) { sortInfos = new[] { new SortInfo { SortColumn = ReflectionUtility.GetPropertyName <Price>(x => x.List) } }; } //Try to replace sorting columns names TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos); query = query.OrderBySortInfos(sortInfos); // TODO: add checks for criteria.Take being greater than 0 if (criteria.GroupByProducts) { var groupedQuery = query.GroupBy(x => x.ProductId).OrderBy(x => 1); retVal.TotalCount = await groupedQuery.CountAsync(); query = groupedQuery.Skip(criteria.Skip).Take(criteria.Take).SelectMany(x => x); } else { retVal.TotalCount = await query.CountAsync(); query = query.Skip(criteria.Skip).Take(criteria.Take); } if (criteria.Take > 0) { var pricesIds = await query.Select(x => x.Id).ToListAsync(); retVal.Results = (await _pricingService.GetPricesByIdAsync(pricesIds.ToArray())) .OrderBy(x => pricesIds.IndexOf(x.Id)) .ToList(); } } return retVal; })); }