public IActionResult DeleteCart(int id)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                var action = this.cartsService.DeleteAll(this.User.Identity.Name);

                if (!action.Result)
                {
                    return(this.RedirectToAction(nameof(this.GetCart)));
                }

                return(this.Redirect("/"));
            }

            var sessionCart = SessionExtensions
                              .GetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart");

            if (sessionCart == null)
            {
                return(this.Redirect("/"));
            }

            sessionCart = new List <CartProductViewModel>();

            SessionExtensions
            .SetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart", sessionCart);

            return(this.Redirect("/"));
        }
        public IActionResult Add(int id)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                var action = this.cartsService.AddProduct(id, this.User.Identity.Name);

                if (!action.Result)
                {
                    return(this.Redirect("/"));
                }

                return(this.RedirectToAction(nameof(this.GetCart)));
            }

            var sessionCart = SessionExtensions
                              .GetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart");

            if (sessionCart == null)
            {
                sessionCart = new List <CartProductViewModel>();
            }

            var itemToAdd = this.shopProductsService.GetProductById <ShopProductViewModel>(id);

            if (itemToAdd == null)
            {
                return(this.Redirect("/"));
            }

            var sessionItem = new CartProductViewModel()
            {
                Quantity = 1,
                Details  = itemToAdd,
            };

            sessionCart.Add(sessionItem);

            SessionExtensions
            .SetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart", sessionCart);

            return(this.RedirectToAction(nameof(this.GetCart)));
        }
        public IActionResult DeleteCartProduct(int id)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                var action = this.cartsService.DeleteProduct(id, this.User.Identity.Name);

                if (!action.Result)
                {
                    return(this.Redirect("/"));
                }

                return(this.RedirectToAction(nameof(this.GetCart)));
            }

            var sessionCart = SessionExtensions
                              .GetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart");

            if (sessionCart == null)
            {
                return(this.Redirect("/"));
            }

            var itemToDelete = sessionCart.FirstOrDefault(p => p.Details.Id == id);

            if (itemToDelete == null)
            {
                return(this.Redirect("/"));
            }

            sessionCart.Remove(itemToDelete);

            SessionExtensions
            .SetDataObject <List <CartProductViewModel> >(this.HttpContext.Session, "shoppingCart", sessionCart);

            return(this.RedirectToAction(nameof(this.GetCart)));
        }