public async Task <IActionResult> AddToCart(int?bookID, string controlla, string acta)
        {
            if (bookID == null)
            {
                return(View("NotFound"));
            }

            //Gets the user and their id.
            var user = await _userManager.GetUserAsync(User);

            var userID = user?.Id;

            //Creates the InputModel for the Cart.
            var newCartItem = new CartInputModel()
            {
                BookID = (int)bookID,
                UserID = userID,
            };

            //Adds this the cart connection from user to book.
            _cartService.AddCartItem(newCartItem);
            //Returns the user from where they came.
            if (acta == "Details")
            {
                return(RedirectToAction(acta, controlla, new { id = bookID }));
            }
            return(RedirectToAction(acta, controlla));
        }
예제 #2
0
        public ActionResult AddDishToCartAfterCustomizing(int id, string stringOfIngredients)
        {
            var split = Regex.Split(stringOfIngredients, @"\D+");

            var listOfIngredients = new List <int>();

            foreach (var item in split)
            {
                if (int.TryParse(item, out int number))
                {
                    listOfIngredients.Add(int.Parse(item));
                }
            }

            var cart    = new Cart();
            var session = HttpContext.Session.GetString(SessionCartId);

            if (session != null && session != "")
            {
                cart = _context.Carts.First(c => c.CartId == session);

                cart = _cartService.AddCartItem(cart.CartId, id, listOfIngredients);
                HttpContext.Session.SetString(SessionCartId, cart.CartId);
            }
            else
            {
                var newCart = _cartService.CreateCart();
                cart.CartId = newCart.CartId;
                cart        = _cartService.AddCartItem(cart.CartId, id, listOfIngredients);
                HttpContext.Session.SetString(SessionCartId, cart.CartId);
            }

            return(PartialView("_CartView", cart));
        }
예제 #3
0
        public async Task <ActionResult <CartItemViewModel> > AddCartItem([FromRoute] int productId, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            bool productExists = await _productService.ProductExists(productId, cancellationToken);

            if (!productExists)
            {
                return(NotFound());
            }

            bool productInCart = await _cartService.IsInCart(productId, cancellationToken);

            if (productInCart)
            {
                return(BadRequest());
            }

            CartItemViewModel cartItem = await _cartService.AddCartItem(productId, cancellationToken);

            return(Ok(cartItem));
        }
예제 #4
0
        public async Task <IActionResult> AddItemToCart(int productId, int amount, string userName = null)
        {
            if (string.IsNullOrEmpty(userName))
            {
                userName = User.Identity.Name;
            }
            var item = await _cartService.AddCartItem(productId, amount, userName);

            return(Ok(item));
        }
예제 #5
0
        static void Main(string[] args)
        {
            try
            {
                ICartService mCartService = new CartService("Fake");
                //Create Cart
                int cartID = mCartService.CreateCartNew();

                //Add fake item to cart.
                mCartService.AddCartItem(1, 3);
                mCartService.AddCartItem(2, 3);
                mCartService.AddCartItem(3, 5);
                mCartService.AddCartItem(4, 3);
                mCartService.AddCartItem(5, 3);
                mCartService.AddCartItem(6, 3);
                mCartService.AddCartItem(7, 6);
                mCartService.AddCartItem(8, 6);
                mCartService.AddCartItem(9, 6);
                mCartService.AddCartItem(10, 6);

                //Process cart.
                Cart mCart = mCartService.CheckOutCart(cartID);

                //Print cart detail on screen.
                foreach (var item in mCart.LineItems)
                {
                    Console.WriteLine(item.ToString());
                }
                Console.WriteLine("----------------------------------------");
                Console.WriteLine(mCart.ToString());

                Console.ReadKey();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
        public async Task AddCart([FromBody] Item item)
        {
            using (Db)
            {
                await Db.Connection.OpenAsync();

                //User user = new User(Db)
                //{
                //    Id = 1,
                //    Username = "******",
                //    Password = "******",
                //    Email = "*****@*****.**"
                //};
                await CS.AddCartItem(item);
            }
        }