Exemplo n.º 1
0
        /// <summary>
        /// Async search products by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public virtual async Task <CatalogSearchResult> SearchProductsAsync(ProductSearchCriteria criteria)
        {
            criteria = criteria.Clone();

            var workContext    = _workContextFactory();
            var searchCriteria = criteria.ToProductSearchDto(workContext);
            var result         = await _searchApi.SearchApiModule.SearchProductsAsync(workContext.CurrentStore.Id, searchCriteria);

            var products = result.Products.Select(x => x.ToProduct(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.CurrentStore)).ToList();

            if (products.Any())
            {
                var productsWithVariations = products.Concat(products.SelectMany(x => x.Variations)).ToList();
                var taskList = new List <Task>
                {
                    LoadProductInventoriesAsync(productsWithVariations),
                    LoadProductVendorsAsync(productsWithVariations),
                    _pricingService.EvaluateProductPricesAsync(productsWithVariations)
                };
                await Task.WhenAll(taskList.ToArray());
            }

            return(new CatalogSearchResult
            {
                Products = new StaticPagedList <Product>(products, criteria.PageNumber, criteria.PageSize, (int?)result.TotalCount ?? 0),
                Aggregations = !result.Aggregations.IsNullOrEmpty() ? result.Aggregations.Select(x => x.ToAggregation(workContext.CurrentLanguage.CultureName)).ToArray() : new Aggregation[] { }
            });
        }
        private async Task <CatalogSearchResult> InnerSearchProductsAsync(ProductSearchCriteria criteria, WorkContext workContext)
        {
            criteria = criteria.Clone();

            var searchCriteria = criteria.ToProductSearchDto(workContext);
            var result         = await _searchApi.SearchApiModule.SearchProductsAsync(workContext.CurrentStore.Id, searchCriteria);

            var products = result.Products?.Select(x => x.ToProduct(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.CurrentStore)).ToList() ?? new List <Product>();

            if (products.Any())
            {
                var productsWithVariations = products.Concat(products.SelectMany(x => x.Variations)).ToList();
                var taskList = new List <Task>();

                if (criteria.ResponseGroup.HasFlag(ItemResponseGroup.ItemAssociations))
                {
                    taskList.Add(LoadProductAssociationsAsync(productsWithVariations));
                }

                if (criteria.ResponseGroup.HasFlag(ItemResponseGroup.Inventory))
                {
                    taskList.Add(LoadProductInventoriesAsync(productsWithVariations));
                }

                if (criteria.ResponseGroup.HasFlag(ItemResponseGroup.ItemWithVendor))
                {
                    taskList.Add(LoadProductVendorsAsync(productsWithVariations, workContext));
                }

                if (criteria.ResponseGroup.HasFlag(ItemResponseGroup.ItemWithPrices))
                {
                    taskList.Add(_pricingService.EvaluateProductPricesAsync(productsWithVariations, workContext));
                }

                foreach (var product in productsWithVariations)
                {
                    product.IsAvailable = await _productAvailabilityService.IsAvailable(product, 1);
                }

                await Task.WhenAll(taskList.ToArray());
            }

            return(new CatalogSearchResult
            {
                Products = new StaticPagedList <Product>(products, criteria.PageNumber, criteria.PageSize, (int?)result.TotalCount ?? 0),
                Aggregations = !result.Aggregations.IsNullOrEmpty() ? result.Aggregations.Select(x => x.ToAggregation(workContext.CurrentLanguage.CultureName)).ToArray() : new Aggregation[] { }
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Search products by given criteria
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public virtual CatalogSearchResult SearchProducts(ProductSearchCriteria criteria)
        {
            criteria = criteria.Clone();

            var workContext    = _workContextFactory();
            var searchCriteria = criteria.ToProductSearchDto(workContext);
            var result         = _searchApi.SearchApiModule.SearchProducts(workContext.CurrentStore.Id, searchCriteria);
            var products       = result.Products.Select(x => x.ToProduct(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.CurrentStore)).ToList();

            if (products.Any())
            {
                var productsWithVariations = products.Concat(products.SelectMany(x => x.Variations)).ToList();
                //Unable to make parallel call because its synchronous method (in future this information pricing and inventory will be getting from search index) and this lines can be removed
                _pricingService.EvaluateProductPrices(productsWithVariations);
                LoadProductInventories(productsWithVariations);
                LoadProductVendors(productsWithVariations);
            }

            return(new CatalogSearchResult
            {
                Products = new StaticPagedList <Product>(products, criteria.PageNumber, criteria.PageSize, (int?)result.TotalCount ?? 0),
                Aggregations = !result.Aggregations.IsNullOrEmpty() ? result.Aggregations.Select(x => x.ToAggregation(workContext.CurrentLanguage.CultureName)).ToArray() : new Aggregation[] { }
            });
        }