public async Task <ActionResult <Models.Product> > GetOneProduct(int productid)
        {
            var product = await _productService.GetOneAsyn <Models.Product>($"{_productServiceRootUrl}/api/product/Getone?id=" + productid);

            if (product.Quantity < 1)
            {
                product.Availability = false;
            }
            return(View(product));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddToCart(int productid)
        {
            //Read the session and get the content
            var             currentCartItems = HttpContext.Session.Get <List <CartItem> >(_cartName);
            List <CartItem> cartItem         = new List <CartItem>();

            //if session cookie is contaisn data, assign that to new listitem
            if (currentCartItems != null)
            {
                cartItem = currentCartItems;
            }

            //if the session cookie already contains the incoming productid , then increase the already eisting quantity of that product by 1
            if (currentCartItems != null && currentCartItems.Any(x => x.Product.Id == productid))
            {
                int productindex = currentCartItems.FindIndex(x => x.Product.Id == productid);
                currentCartItems[productindex].Quantity += 1;
                cartItem = currentCartItems;
            }
            //if the session doest contain the incoming productid, then create a new item with amount =1
            else
            {
                Models.Product product = await _productServiceHandler.GetOneAsyn <Models.Product>($"{_apiRootUrl}/api/product/GetOne?id=" + productid);

                CartItem newItem = new CartItem()
                {
                    Product  = product,
                    Quantity = 1
                };
                cartItem.Add(newItem);
            }

            // set the session cookie with new listofItems
            HttpContext.Session.Set(_cartName, cartItem);

            return(RedirectToAction("GetAllProducts", "Product"));
        }