public async Task <IActionResult> Buy(Guid id)
        {
            //ProductViewModel productModel = new ProductViewModel();

            var productsList = await _productUserService.GetAllProductsAsync();

            if (SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart") == null)
            {
                List <Item> cart = new List <Item>();

                cart.Add(new Item {
                    Product = productsList.Find(x => x.ProductID == id), Quantity = 1
                });
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List <Item> cart  = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item {
                        Product = productsList.Find(x => x.ProductID == id), Quantity = 1
                    });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index"));
        }
Пример #2
0
        public async Task <IActionResult> GetAllProducts()
        {
            var productsList = await _productUserService.GetAllProductsAsync();

            return(View(productsList));
        }