Exemple #1
0
        public IActionResult BuyConfirmed([Bind("Id", "quantity")] int Id, int quantity)
        {
            // read the selected item with id
            var item = _item.ReadItem(Id);

            // if squantity to buy is greater than amount of item in stock
            // display error message and go back to Home/Index
            if (item.AmountInStock < quantity)
            {
                TempData["Error"] = "Not enough product in stock!\nPlease reduce number to buy.";
                return(RedirectToAction("Index", "Home"));
            }
            // read the currently logged on user id
            var userId = _userManager.GetUserId(HttpContext.User);
            // read the shopping cart associated with current user
            var shoppingCart = _cart.ReadShoppingCart(userId);
            // read all read CartItem associated with current user and matches Item
            var cartItem = _cartItem.ReadAllCartItems().FirstOrDefault(
                c => c.Name == item.Name && c.Type == item.Type && c.Price == item.Price);

            // if CartItem is null
            // create a new one
            if (cartItem == null)
            {
                cartItem          = new CartItems();
                cartItem.Name     = item.Name;
                cartItem.Type     = item.Type;
                cartItem.Price    = item.Price;
                cartItem.Quantity = quantity;
                cartItem.CartId   = shoppingCart.Id;
                cartItem.Cart     = shoppingCart;
                _cartItem.CreateCartItem(cartItem);
            }
            // if same item easists in CartItem, add selected quantity to it
            else
            {
                cartItem.Quantity += quantity;
            }
            // reduce the amount of item in stock by selected quantity
            item.AmountInStock -= quantity;
            // update item in stock
            _item.UpdateItem(item.Id, item);
            // redirect to Home/Index
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult BuyConfirmed([Bind("id", "quantity")] int id, int quantity)
        {
            var item = _itemRepository.ReadItem(id);

            if (quantity > item.AmountInStock)
            {
                return(RedirectToAction("Buy", "ShoppingCart", new { id = item.Id }));
            }

            var applicationUserId = _userManager.GetUserId(HttpContext.User);
            var shoppingCart      = _shoppingCartRepository.ReadShoppingCart(applicationUserId);
            var cartItem          = _cartItemRepository.ReadAllCartItems().FirstOrDefault(c => c.Name == item.Name && c.Type == item.Type && c.Price == item.Price);

            if (cartItem == null)
            {
                cartItem              = new CartItem();
                cartItem.Name         = item.Name;
                cartItem.Type         = item.Type;
                cartItem.Price        = item.Price;
                cartItem.Quantity     = quantity;
                cartItem.CartId       = shoppingCart.Id;
                cartItem.ShoppingCart = shoppingCart;
                _cartItemRepository.CreateCartItem(cartItem);
            }


            else
            {
                cartItem.Quantity += quantity;
            }

            item.AmountInStock -= quantity;
            _itemRepository.UpdateItem(item.Id, item);

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