/// <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); var images = GetImagesAsync(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 }; var vm = CreateRelatedProductsViewModel(createVmParam); return(vm); }
private CreateRelatedProductViewModelParam CreateParam() { var param = new CreateRelatedProductViewModelParam(); param.ProductsWithVariant = new List <ProductWithVariant> { new ProductWithVariant { Product = RelatedProductsViewServiceTestHelper.Product, Variant = new Variant { Id = "1" } }, new ProductWithVariant { Product = RelatedProductsViewServiceTestHelper.Product, Variant = new Variant { Id = "2" } }, }; param.CultureInfo = new CultureInfo("en-CA"); param.BaseUrl = new Uri("http://httpcolonslashslash.com"); param.Prices = new List <ProductPrice> { new ProductPrice { DefaultPrice = 99m, ProductId = "1", Pricing = new ProductPriceEntry { Price = 99m } } }; param.Images = new List <ProductMainImage> { new ProductMainImage { ProductId = "1", VariantId = "1", ImageUrl = GetRandom.WwwUrl(), FallbackImageUrl = GetRandom.WwwUrl() }, new ProductMainImage { ProductId = "1", VariantId = "2", ImageUrl = GetRandom.WwwUrl(), FallbackImageUrl = GetRandom.WwwUrl() } }; return(param); }
/// <summary> /// Creates a <see cref="RelatedProductsViewModel"/> which is just a container for a list of <see cref="RelatedProductViewModel"/> /// </summary> /// <returns></returns> protected virtual RelatedProductsViewModel CreateRelatedProductsViewModel(CreateRelatedProductViewModelParam param) { var relatedProductsViewModel = new RelatedProductsViewModel(); //iterate over the requested products foreach (var productVariant in param.ProductsWithVariant) { // call the method that actually does the mapping for an individual product var productVm = CreateRelatedProductsViewModel(param.BaseUrl, param.CultureInfo, productVariant, param.Prices, param.Images); relatedProductsViewModel.Products.Add(productVm); } return(relatedProductsViewModel); }
public RelatedProductsViewModel CreateRelatedProductsViewModelProxy(CreateRelatedProductViewModelParam param) { return(CreateRelatedProductsViewModel(param)); }