public virtual async Task <PromotionSearchResult> SearchPromotionsAsync(PromotionSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), nameof(SearchPromotionsAsync), criteria.GetCacheKey()); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(PromotionSearchCacheRegion.CreateChangeToken()); var result = AbstractTypeFactory <PromotionSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var sortInfos = BuildSortExpression(criteria); var query = BuildQuery(repository, criteria); //https://github.com/zzzprojects/EntityFramework-Plus/issues/293 //Workaround ArgumentException with CountAsync have no predicate when query has a Cast or OfType expression result.TotalCount = await query.CountAsync(x => true); if (criteria.Take > 0) { var ids = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id) .Select(x => x.Id) .Skip(criteria.Skip).Take(criteria.Take) .ToArrayAsync(); result.Results = (await _promotionService.GetPromotionsByIdsAsync(ids)) .OrderBy(x => Array.IndexOf(ids, x.Id)).ToList(); } } return result; })); }
public virtual async Task <PromotionSearchResult> SearchPromotionsAsync(PromotionSearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), "SearchPromotionsAsync", criteria.GetCacheKey()); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(PromotionCacheRegion.CreateChangeToken()); var retVal = AbstractTypeFactory <PromotionSearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { var query = GetPromotionsQuery(repository, criteria); var sortInfos = criteria.SortInfos; if (sortInfos.IsNullOrEmpty()) { sortInfos = new[] { new SortInfo { SortColumn = ReflectionUtility.GetPropertyName <Promotion>(x => x.Priority), SortDirection = SortDirection.Descending } }; } query = query.OrderBySortInfos(sortInfos); retVal.TotalCount = await query.CountAsync(); if (criteria.Take > 0) { var ids = await query.Select(x => x.Id) .Skip(criteria.Skip) .Take(criteria.Take).ToArrayAsync(); var promotions = await _promotionService.GetPromotionsByIdsAsync(ids); retVal.Results = promotions.OrderBy(p => ids.ToList().IndexOf(p.Id)).ToList(); } } return retVal; })); }
public GenericSearchResult <Promotion> SearchPromotions(PromotionSearchCriteria criteria) { var cacheKey = GetCacheKey("IPromotionSearchService.SearchPromotions", criteria.GetCacheKey()); var retVal = _cacheManager.Get(cacheKey, RegionName, () => _promoSearchService.SearchPromotions(criteria)); return(retVal); }