Example #1
0
        protected virtual GetProductMainImagesParam GetImagesParam(IEnumerable <ProductWithVariant> products)
        {
            var imageRequests = products.Select(identifier =>
            {
                var imageRequest = new ProductImageRequest
                {
                    ProductId = identifier.Product.Id,
                    Variant   = identifier.Variant == null ? VariantKey.Empty : new VariantKey
                    {
                        Id = identifier.Variant.Id,
                        KeyVariantAttributeValues = identifier.Variant.PropertyBag
                    },
                    ProductDefinitionName = identifier.Product.DefinitionName
                };

                identifier.Product.PropertyBag.TryGetValue("ImageUrl", out var imageUrlProperty);
                var imageUrl = imageUrlProperty?.ToString() ?? DamProvider.GetMediaImageUrl(identifier.Product, identifier.Variant?.Id);

                if (imageUrl != null)
                {
                    imageRequest.PropertyBag = new Dictionary <string, object> {
                        ["ImageUrl"] = imageUrl
                    };
                }
                return(imageRequest);
            }).ToList();

            return(new GetProductMainImagesParam
            {
                ImageSize = ProductConfiguration.ThumbnailImageSize,
                ProductImageRequests = imageRequests
            });
        }
Example #2
0
        protected virtual async Task <List <ProductMainImage> > GetImagesAsync(IEnumerable <ProductWithVariant> products)
        {
            var imageRequests = products.Select(identifier =>
            {
                var imageRequest = new ProductImageRequest
                {
                    ProductId = identifier.Product.Id,
                    Variant   = identifier.Variant == null ? VariantKey.Empty : new VariantKey
                    {
                        Id = identifier.Variant.Id,
                        KeyVariantAttributeValues = identifier.Variant.PropertyBag
                    },
                    ProductDefinitionName = identifier.Product.DefinitionName
                };

                var imageUrl = DamProvider.GetMediaImageUrl(identifier.Product, identifier.Variant?.Id);
                if (imageUrl != null)
                {
                    imageRequest.PropertyBag = new Dictionary <string, object> {
                        ["ImageUrl"] = imageUrl
                    };
                }
                return(imageRequest);
            }).ToList();

            var imageRequestParam = new GetProductMainImagesParam
            {
                ImageSize            = ProductConfiguration.ProductSummaryImageSize,
                ProductImageRequests = imageRequests
            };

            var mainImages = await DamProvider.GetProductMainImagesAsync(imageRequestParam).ConfigureAwait(false);

            return(mainImages);
        }
Example #3
0
        /// <summary>
        /// This method retrieves all the products for a product given in the <see cref="GetRelatedProductsParam"/>
        /// then converts them to to a <see cref="RelatedProductsViewModel"/>. The change the behaviour of this method,
        /// developers should override <seealso cref="RetrieveProductsAsync"/> or <seealso cref="CreateRelatedProductsViewModel(Orckestra.Composer.Product.Parameters.CreateRelatedProductViewModelParam)"/>.
        /// This method makes a number of asynchronous calls, so in cases where only the product IDs are desired,
        /// <seealso cref="GetProductIdsAsync"/> should be used.
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async virtual Task <RelatedProductsViewModel> GetRelatedProductsAsync(GetRelatedProductsParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }

            if (!param.ProductIds.Any())
            {
                return(new RelatedProductsViewModel());
            }

            var products = await RetrieveProductsAsync(param).ConfigureAwait(false);

            // make a single request to get all product prices at once, instead of making a request for each product
            var productIds = param.ProductIds.Select(p => p.ProductId).ToList();
            var prices     = ProductRepository.CalculatePricesAsync(productIds, param.Scope, FulfillmentContext.AvailabilityAndPriceDate);
            var images     = DamProvider.GetProductMainImagesAsync(GetImagesParam(products));
            await Task.WhenAll(prices, images);

            var createVmParam = new CreateRelatedProductViewModelParam
            {
                BaseUrl             = param.BaseUrl,
                CultureInfo         = param.CultureInfo,
                ProductsWithVariant = products,
                Scope       = param.Scope,
                Prices      = await prices,
                Images      = await images,
                CurrencyIso = param.CurrencyIso
            };

            var vm = CreateRelatedProductsViewModel(createVmParam);

            return(vm);
        }
        /// <summary>
        ///     Searches the available products based on the given search criteria.
        /// </summary>
        /// <param name="param">The criteria.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        protected virtual async Task <ProductSearchResultsViewModel> SearchAsync(TParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }
            if (param.Criteria.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria.CultureInfo)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Criteria.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Criteria.Scope)), nameof(param));
            }

            var searchResult = await SearchRepository.SearchProductAsync(param.Criteria).ConfigureAwait(false);

            var cloneParam = (TParam)param.Clone();

            if (searchResult == null)
            {
                return(null);
            }

            var imageUrls = await DamProvider.GetProductMainImagesAsync(GetImagesParam(searchResult.Documents)).ConfigureAwait(false);

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <TParam>
            {
                SearchParam  = cloneParam,
                ImageUrls    = imageUrls,
                SearchResult = searchResult
            };

            if (param.Criteria.IncludeFacets &&
                param.Criteria.SelectedFacets != null &&
                param.Criteria.SelectedFacets.Any(s => s.Name?.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix) ?? false))
            {
                createSearchViewModelParam.CategoryFacetCountsResult = await SearchRepository.GetCategoryFacetCountsAsync(param.Criteria).ConfigureAwait(false);
            }

            return(await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false));
        }
Example #5
0
        /// <summary>
        /// Get the product images associate with a productId and a variantId
        /// </summary>
        /// <param name="product"></param>
        /// <param name="variants"></param>
        /// <returns></returns>
        protected virtual async Task <List <AllProductImages> > GetProductImages(
            Overture.ServiceModel.Products.Product product,
            IList <Variant> variants)
        {
            var param = new GetAllProductImagesParam
            {
                ImageSize             = ProductConfiguration.ImageSize,
                ThumbnailImageSize    = ProductConfiguration.ThumbnailImageSize,
                ProductZoomImageSize  = ProductConfiguration.ProductZoomImageSize,
                ProductId             = product.Id,
                PropertyBag           = product.PropertyBag,
                ProductDefinitionName = product.DefinitionName,
                Variants        = variants == null ? new List <Variant>() : variants.ToList(),
                MediaSet        = product.MediaSet,
                VariantMediaSet = product.VariantMediaSet
            };

            return(await DamProvider.GetAllProductImagesAsync(param).ConfigureAwait(false));
        }
        /// <summary>
        ///     Searches the available products based on the given search criteria.
        /// </summary>
        /// <param name="param">The criteria.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        protected virtual async Task <ProductSearchResultsViewModel> SearchAsync(TParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }
            if (param.Criteria.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria.CultureInfo)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Criteria.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Criteria.Scope)), nameof(param));
            }

            var searchResult = await SearchRepository.SearchProductAsync(param.Criteria).ConfigureAwait(false);

            var cloneParam = (TParam)param.Clone();

            if (searchResult == null)
            {
                return(null);
            }

            var imageUrls = await DamProvider.GetProductMainImagesAsync(GetImagesParam(searchResult.Documents)).ConfigureAwait(false);

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <TParam>
            {
                SearchParam  = cloneParam,
                ImageUrls    = imageUrls,
                SearchResult = searchResult
            };

            return(await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false));
        }
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(GetSearchQueryViewModelParams param)
        {
            SearchQueryViewModel viewModel;

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }

            var searchQueryProducts = await SearchQueryRepository.SearchQueryProductAsync(new SearchQueryProductParams
            {
                CultureName = param.CultureInfo.Name,
                QueryName   = param.QueryName,
                QueryType   = param.QueryType,
                ScopeId     = param.Scope,
                Criteria    = param.Criteria
            }).ConfigureAwait(false);

            var documents = searchQueryProducts.Result.Documents.Select(ToProductDocument).ToList();

            var inventoryLocations = await InventoryRepository.GetInventoryLocationStatusesBySkus(
                new GetInventoryLocationStatuseParam()
            {
                Skus    = documents.Select(d => d.Sku).ToList(),
                ScopeId = param.Scope,
                InventoryLocationIds = param.InventoryLocationIds
            }).ConfigureAwait(false);

            FixInventories(documents, inventoryLocations);
            documents = await FixInventoryFilter(documents, param.Scope).ConfigureAwait(false);

            var getImageParam = new GetProductMainImagesParam
            {
                ImageSize            = SearchConfiguration.DefaultImageSize,
                ProductImageRequests = documents
                                       .Select(document => new ProductImageRequest
                {
                    ProductId = document.ProductId,
                    Variant   = document.PropertyBag.ContainsKey(VariantPropertyBagKey)
                            ? new VariantKey {
                        Id = document.PropertyBag[VariantPropertyBagKey].ToString()
                    }
                            : VariantKey.Empty,
                    ProductDefinitionName = document.PropertyBag.ContainsKey("DefinitionName")
                            ? document.PropertyBag["DefinitionName"].ToString()
                            : string.Empty,
                    PropertyBag = document.PropertyBag
                }).ToList()
            };

            var imageUrls = await DamProvider.GetProductMainImagesAsync(getImageParam).ConfigureAwait(false);



            var newCriteria = param.Criteria.Clone();

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <SearchParam>
            {
                SearchParam = new SearchParam()
                {
                    Criteria = newCriteria
                },
                ImageUrls    = imageUrls,
                SearchResult = new ProductSearchResult()
                {
                    Documents  = documents,
                    TotalCount = searchQueryProducts.Result.TotalCount,
                    Facets     = searchQueryProducts.Result.Facets
                }
            };

            viewModel = new SearchQueryViewModel
            {
                SelectedFacets =
                    await GetSelectedFacetsAsync(createSearchViewModelParam.SearchParam).ConfigureAwait(false),
                ProductSearchResults =
                    await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false),
            };

            if (searchQueryProducts.SelectedFacets != null)
            {
                foreach (var facet in searchQueryProducts.SelectedFacets)
                {
                    foreach (var value in facet.Values)
                    {
                        if (viewModel.SelectedFacets.Facets.All(f => f.Value != value))
                        {
                            viewModel.SelectedFacets.Facets.Add(new SelectedFacet()
                            {
                                Value       = value,
                                FieldName   = facet.FacetName,
                                DisplayName = value,
                                IsRemovable = false
                            });
                        }
                    }
                }

                foreach (var selectedFacet in searchQueryProducts.SelectedFacets)
                {
                    foreach (var facet in viewModel.ProductSearchResults.Facets.Where(d => d.FieldName == selectedFacet.FacetName))
                    {
                        foreach (var facetValue in selectedFacet.Values.Select(value => facet.FacetValues.FirstOrDefault(f => f.Value == value)).Where(facetValue => facetValue != null))
                        {
                            facetValue.IsSelected = true;
                        }
                    }
                }
            }

            viewModel.Context[nameof(viewModel.ProductSearchResults.SearchResults)] = viewModel.ProductSearchResults.SearchResults;
            viewModel.Context[nameof(SearchConfiguration.MaxItemsPerPage)]          = SearchConfiguration.MaxItemsPerPage;
            viewModel.Context["ListName"] = "Search Query";

            return(viewModel);
        }
        public async Task <SearchQueryViewModel> GetSearchQueryViewModelAsync(GetSearchQueryViewModelParams param)
        {
            SearchQueryViewModel viewModel;

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.Criteria == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.Criteria)), nameof(param));
            }

            var searchQueryProducts = await SearchQueryRepository.SearchQueryProductAsync(new SearchQueryProductParams
            {
                CultureName = param.CultureInfo.Name,
                QueryName   = param.QueryName,
                QueryType   = param.QueryType,
                ScopeId     = param.Scope,
                Criteria    = param.Criteria
            }).ConfigureAwait(false);

            var documents = searchQueryProducts.Result.Documents.Select(ToProductDocument).ToList();

            var inventoryLocations = await InventoryRepository.GetInventoryLocationStatusesBySkus(
                new GetInventoryLocationStatuseParam()
            {
                Skus    = documents.Select(d => d.Sku).ToList(),
                ScopeId = param.Scope,
                InventoryLocationIds = param.InventoryLocationIds
            }).ConfigureAwait(false);

            FixInventories(documents, inventoryLocations);
            documents = await FixInventoryFilter(documents, param.Scope).ConfigureAwait(false);

            var getImageParam = new GetProductMainImagesParam
            {
                ImageSize            = SearchConfiguration.DefaultImageSize,
                ProductImageRequests = documents
                                       .Select(document => new ProductImageRequest
                {
                    ProductId = document.ProductId,
                    Variant   = document.PropertyBag.ContainsKey(VariantPropertyBagKey)
                            ? new VariantKey {
                        Id = document.PropertyBag[VariantPropertyBagKey].ToString()
                    }
                            : VariantKey.Empty,
                    ProductDefinitionName = document.PropertyBag.ContainsKey("DefinitionName")
                            ? document.PropertyBag["DefinitionName"].ToString()
                            : string.Empty,
                    PropertyBag = document.PropertyBag
                }).ToList()
            };

            var imageUrls = await DamProvider.GetProductMainImagesAsync(getImageParam).ConfigureAwait(false);

            var newCriteria = param.Criteria.Clone();

            var createSearchViewModelParam = new CreateProductSearchResultsViewModelParam <SearchParam>
            {
                SearchParam = new SearchParam()
                {
                    Criteria = newCriteria
                },
                ImageUrls    = imageUrls,
                SearchResult = new ProductSearchResult()
                {
                    Documents  = documents,
                    TotalCount = searchQueryProducts.Result.TotalCount,
                    Facets     = searchQueryProducts.Result.Facets,
                },
            };

            if (param.QueryType != Overture.ServiceModel.SearchQueries.SearchQueryType.ProductSet &&
                param.Criteria.IncludeFacets &&
                param.Criteria.SelectedFacets != null &&
                param.Criteria.SelectedFacets.Any(s => s.Name?.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix) ?? false))
            {
                createSearchViewModelParam.CategoryFacetCountsResult = await SearchQueryRepository.GetCategoryFacetCountsAsync(param.Criteria, searchQueryProducts).ConfigureAwait(false);
            }

            viewModel = new SearchQueryViewModel
            {
                QueryName     = param.QueryName,
                QueryType     = param.QueryType,
                FacetSettings = new FacetSettingsViewModel()
                {
                    SelectedFacets =
                        await GetSelectedFacetsAsync(createSearchViewModelParam.SearchParam).ConfigureAwait(false),
                },
                ProductSearchResults =
                    await CreateProductSearchResultsViewModelAsync(createSearchViewModelParam).ConfigureAwait(false),
                ListName = "Search Query"
            };

            ProcessFacets(viewModel, searchQueryProducts);

            viewModel.FacetSettings.CategoryFacetValuesTree = await BuildCategoryFacetValuesTree(viewModel.ProductSearchResults.Facets,
                                                                                                 viewModel.FacetSettings.SelectedFacets,
                                                                                                 viewModel.ProductSearchResults.CategoryFacetCounts).ConfigureAwait(false);

            if (viewModel.FacetSettings.CategoryFacetValuesTree != null)
            {
                viewModel.FacetSettings.CategoryFacetValuesTree.TotalCount = createSearchViewModelParam.CategoryFacetCountsResult != null ? createSearchViewModelParam.CategoryFacetCountsResult.TotalCount : viewModel.ProductSearchResults.TotalCount;
                viewModel.FacetSettings.Context["CategoryFacetValuesTree"] = viewModel.FacetSettings.CategoryFacetValuesTree;
            }

            // Json context for Facets
            viewModel.FacetSettings.Context["SelectedFacets"]      = viewModel.FacetSettings.SelectedFacets;
            viewModel.FacetSettings.Context["Facets"]              = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix));
            viewModel.FacetSettings.Context["PromotedFacetValues"] = viewModel.ProductSearchResults.PromotedFacetValues;

            viewModel.Context[nameof(viewModel.ProductSearchResults.SearchResults)] = viewModel.ProductSearchResults.SearchResults;
            viewModel.Context[nameof(viewModel.MaxItemsPerPage)] = viewModel.MaxItemsPerPage;
            viewModel.Context["ListName"] = viewModel.ListName;

            return(viewModel);
        }