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
        public async Task <ActionResult> Update(ShoppingCartUpdateModel cart, string shoppingCartId)
        {
            var parsedCart = (await _shoppingCartHelpers.ParseCart(cart)).Where(line => line.Quantity > 0).ToList();
            await _priceService.AddPrices(parsedCart);

            await _shoppingCartPersistence.Store(parsedCart, shoppingCartId);

            return(RedirectToAction(nameof(Index), new { shoppingCartId }));
        }
        public async Task <ActionResult> Update(ShoppingCartUpdateModel cart, string shoppingCartId)
        {
            ShoppingCart parsedCart = await _shoppingCartHelpers.ParseCart(cart);

            await _priceService.AddPrices(parsedCart.Items);

            await _shoppingCartPersistence.Store(parsedCart, shoppingCartId);

            return(RedirectToAction(nameof(Index), new { shoppingCartId }));
        }