コード例 #1
0
ファイル: HomeController.cs プロジェクト: srog/React-Shop
        public ActionResult AddToCart(string sku)
        {
            var product             = _getProducts.GetBySku(sku);
            var cartList            = _getCart.Get(Identity.LoggedInUserId);
            var existingcartItemDTO = cartList.CartItems.FirstOrDefault(ci => ci.ProductId == product.Id);

            if (existingcartItemDTO != null)
            {
                var cartItem = _getCartItem.GetById(existingcartItemDTO.Id);
                cartItem.Quantity++;
                _saveCartItem.Save(cartItem);
            }
            else
            {
                _saveCartItem.Save(new CartItem
                {
                    CustomerId = Identity.LoggedInUserId,
                    Quantity   = 1,
                    ProductId  = product.Id
                });
            }

            return(RedirectToAction("Index", "Home"));
        }