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;
            }));
        }
        public virtual async Task <Price[]> GetPricesByIdAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(GetPricesByIdAsync), string.Join("-", ids));
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
            {
                Price[] result = null;
                if (ids != null)
                {
                    using (var repository = _repositoryFactory())
                    {
                        cacheEntry.AddExpirationToken(PricesCacheRegion.CreateChangeToken(ids));

                        result = (await repository.GetPricesByIdsAsync(ids)).Select(x => x.ToModel(AbstractTypeFactory <Price> .TryCreateInstance())).ToArray();
                    }
                }

                return(result);
            });

            return(result.Select(x => x.Clone() as Price).ToArray());
        }
Exemple #3
0
        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;
            }));
        }