Пример #1
0
        public async Task <IActionResult> Checkout(int id)
        {
            List <CartItemViewModel> cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");

            ViewBag.cart  = cart;
            ViewBag.total = cart.Sum(item => item.Price * item.Quantity);
            var order = new OrderViewModel();

            foreach (var item in cart)
            {
                order.Items.Add(new ItemOrder
                {
                    Prod_ID   = item.Prod_ID,
                    Prod_Name = item.Prod_Name,
                    ImagePath = item.ImagePath,
                    Quantity  = item.Quantity,
                    Price     = item.Price
                });
            }

            //var user = CartSession.GetUser(HttpContext.Session,"Token");
            //string iduser = user.UserName;
            //var result = await _order.Ordercart(iduser, order);
            //if (result.IsSuccessed)
            //{
            //    CartSession.SetObjectAsJson(HttpContext.Session, "cart", cart);
            //    cart.Clear();
            //    return RedirectToAction("Index");
            //}
            //ModelState.AddModelError("", result.Message);
            return(View());
        }
        public IActionResult Remove(int id)
        {
            List <CartItemViewModel> cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");
            int index = isExist(id);

            cart.RemoveAt(index);
            CartSession.SetObjectAsJson(HttpContext.Session, "cart", cart);
            return(RedirectToAction("Index"));
        }
        private int isExist(int id)
        {
            List <CartItemViewModel> cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");

            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].Prod_ID.Equals(id))
                {
                    return(i);
                }
            }
            return(-1);
        }
        public IActionResult Index()
        {
            var cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");

            if (cart == null)
            {
                ViewBag.cart  = "";
                ViewBag.total = 0;
                return(View());
            }
            ViewBag.cart  = cart;
            ViewBag.total = cart.Sum(item => item.Price * item.Quantity);
            return(View());
        }
        public IActionResult Info()
        {
            List <CartItemViewModel> cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");

            // int index = isExist(id);
            //List<CartItemViewModel> xuly= CartSession.GetObjectFromJson<List<CartItemViewModel>>(HttpContext.Session, "xuly");
            // xuly.Add(new CartItemViewModel { Id = 1.ToString(), Prod_ID = cart[1].Prod_ID, Prod_Name = cart[1].Prod_Name, Price = cart[1].Price, Quantity = cart[1].Quantity });
            // CartSession.SetObjectAsJson(HttpContext.Session, "xuly", cart);
            // List<CartItemViewModel> xu = CartSession.GetObjectFromJson<List<CartItemViewModel>>(HttpContext.Session, "xuly");
            // var u=  HttpContext.Session.GetString(SystemConstants.AppSettings.Token);
            //  LoginRequest user = CartSession.GetUser<LoginRequest>(HttpContext.Session, "Token");
            //User.Identity.Name;


            // cart.Clear();
            CartSession.SetObjectAsJson(HttpContext.Session, "cart", cart);
            return(View());
        }
        public async Task <IActionResult> Buy(int id)
        {
            var result = await _productApiClient.GetById(id);

            var a = result.ResultObj;
            ProductViewModel productModel = new ProductViewModel()
            {
                Prod_ID   = a.Prod_ID,
                Prod_Name = a.Prod_Name,
                Price     = a.Price,
                ImagePath = a.ImagePath
            };

            if (CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart") == null)
            {
                List <CartItemViewModel> cart = new List <CartItemViewModel>();
                cart.Add(new CartItemViewModel {
                    Id = 1.ToString(), Prod_ID = productModel.Prod_ID, Prod_Name = productModel.Prod_Name, Price = productModel.Price, Quantity = 1, ImagePath = productModel.ImagePath
                });
                CartSession.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List <CartItemViewModel> cart = CartSession.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Session, "cart");
                int index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new CartItemViewModel {
                        Prod_ID = productModel.Prod_ID, Prod_Name = productModel.Prod_Name, Price = productModel.Price, Quantity = 1, ImagePath = productModel.ImagePath
                    });
                }
                CartSession.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index"));
        }