public async Task <ActionResult> Index(string shoppingCartId = null)
        {
            ShoppingCart cart = await _shoppingCartPersistence.Retrieve(shoppingCartId);

            IDictionary <string, ProductPart> products =
                await _productService.GetProductDictionary(cart.Items.Select(line => line.ProductSku));

            var items = await _priceService.AddPrices(cart.Items);

            ShoppingCartLineViewModel[] lines = await Task.WhenAll(items.Select(async item =>
            {
                ProductPart product          = products[item.ProductSku];
                Amount price                 = _priceStrategy.SelectPrice(item.Prices);
                ContentItemMetadata metaData = await _contentManager.GetContentItemMetadataAsync(product);
                return(new ShoppingCartLineViewModel
                {
                    Quantity = item.Quantity,
                    ProductSku = item.ProductSku,
                    ProductName = product.ContentItem.DisplayText,
                    UnitPrice = price,
                    LinePrice = item.Quantity *price,
                    ProductUrl = Url.RouteUrl(metaData.DisplayRouteValues),
                    Attributes = item.Attributes.ToDictionary(attr => attr.AttributeName)
                });
            }));

            var model = new ShoppingCartViewModel
            {
                Id     = shoppingCartId,
                Lines  = lines,
                Totals = lines.GroupBy(l => l.LinePrice.Currency).Select(g => new Amount(g.Sum(l => l.LinePrice.Value), g.Key))
            };

            return(View(model));
        }
예제 #2
0
 private Task BuildViewModel(OrderPartViewModel model, OrderPart part)
 => Task.Run(async() =>
 {
     model.ContentItem = part.ContentItem;
     IDictionary <string, ProductPart> products =
         await _productService.GetProductDictionary(part.LineItems.Select(line => line.ProductSku));
     OrderLineItemViewModel[] lineItems = await Task.WhenAll(part.LineItems.Select(async lineItem =>
     {
         ProductPart product          = products[lineItem.ProductSku];
         ContentItemMetadata metaData = await _contentManager.GetContentItemMetadataAsync(product);
         return(new OrderLineItemViewModel
         {
             Quantity = lineItem.Quantity,
             ProductSku = lineItem.ProductSku,
             ProductName = product.ContentItem.DisplayText,
             UnitPrice = lineItem.UnitPrice,
             LinePrice = lineItem.LinePrice,
             ProductRouteValues = metaData.DisplayRouteValues,
             Attributes = lineItem.Attributes.ToDictionary(attr => attr.Key, attr => attr.Value)
         });
     }));
     model.LineItems = lineItems;
     model.OrderPart = part;
 });