public void Case3()
        {
            CartService cartService = new CartService();

            cartService.InitCart();
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "imported bottle of perfume", Imported = true, Price = new PriceInfo(27.99), Type = ProductType.Other
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "bottle of perfume", Price = new PriceInfo(18.99), Type = ProductType.Other
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "packet of headache pills", Price = new PriceInfo(9.75), Type = ProductType.Medical
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "imported box of chocolates", Imported = true, Price = new PriceInfo(11.25), Type = ProductType.Food
                }, Quantity = 1
            });

            var cartProductsStrings = cartService.GetCartProducts();

            if (cartProductsStrings != null)
            {
                Console.WriteLine("Input 3:");
                foreach (var d in cartProductsStrings)
                {
                    Console.WriteLine(d);
                }
            }

            Console.WriteLine("\n");

            var cartDescriptionStrings = cartService.GetCartSummary();

            if (cartDescriptionStrings != null)
            {
                Console.WriteLine("Output 3:");
                foreach (var d in cartDescriptionStrings)
                {
                    Console.WriteLine(d);
                }
            }
        }
        public void Case1()
        {
            CartService cartService = new CartService();

            cartService.InitCart();
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "book", Price = new PriceInfo(12.49), Type = ProductType.Book
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "music CD", Price = new PriceInfo(14.99), Type = ProductType.Other
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "chocolate bar", Price = new PriceInfo(0.85), Type = ProductType.Food
                }, Quantity = 1
            });

            var cartProductsStrings = cartService.GetCartProducts();

            if (cartProductsStrings != null)
            {
                Console.WriteLine("Input 1:");
                foreach (var d in cartProductsStrings)
                {
                    Console.WriteLine(d);
                }
            }

            Console.WriteLine("\n");

            var cartDescriptionStrings = cartService.GetCartSummary();

            if (cartDescriptionStrings != null)
            {
                Console.WriteLine("Output 1:");
                foreach (var d in cartDescriptionStrings)
                {
                    Console.WriteLine(d);
                }
            }
        }
        public IActionResult AddProduct([FromRoute] int id)
        {
            int  userId = (int)HttpContext.Session.GetInt32("connectedUserId");
            Cart cart   = _cartService.AddProductToCart(id, userId);

            return(RedirectToAction("Index", "Cart", new { id = userId }));
        }
        public void Case2()
        {
            CartService cartService = new CartService();

            cartService.InitCart();
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "imported box of chocolates", Imported = true, Price = new PriceInfo(10), Type = ProductType.Food
                }, Quantity = 1
            });
            cartService.AddProductToCart(new CartProduct()
            {
                Product = new Product()
                {
                    Name = "imported bottle of perfume", Imported = true, Price = new PriceInfo(47.50), Type = ProductType.Other
                }, Quantity = 1
            });

            var cartProductsStrings = cartService.GetCartProducts();

            if (cartProductsStrings != null)
            {
                Console.WriteLine("Input 2:");
                foreach (var d in cartProductsStrings)
                {
                    Console.WriteLine(d);
                }
            }

            Console.WriteLine("\n");

            var cartDescriptionStrings = cartService.GetCartSummary();

            if (cartDescriptionStrings != null)
            {
                Console.WriteLine("Output 2:");
                foreach (var d in cartDescriptionStrings)
                {
                    Console.WriteLine(d);
                }
            }
        }
        public void AddProductInCartWithExistingProductShouldNotAddProduct()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddProductInCart_Cart_Db")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            var user = new User {
                UserName = "******", Cart = new Cart()
            };

            dbContext.Users.Add(user);

            var product = new Product {
                Name = "T-Shirt"
            };

            dbContext.Products.Add(product);
            dbContext.SaveChanges();

            var userService = new Mock <IUsersService>();

            userService.Setup(r => r.GetUserByUsername(user.UserName))
            .Returns(user);

            var productService = new Mock <IProductsSerivce>();

            productService.Setup(p => p.GetProductById(product.Id))
            .Returns(product);

            var cartsService = new CartService(dbContext, productService.Object, userService.Object);

            cartsService.AddProductToCart(product.Id, user.UserName);

            cartsService.AddProductToCart(product.Id, user.UserName);

            var cartProducts = dbContext.CartProducts.ToList();

            Assert.Single(cartProducts);
        }
 public IActionResult AddToCart(int productId)
 {
     if (_currentUser.IsAuthenticated)
     {
         if (_cartService.AddProductToCart(productId, _currentUser.Id))
         {
             return(Ok(new { message = "Added" }));
         }
         else
         {
             return(BadRequest(new { message = "Product no longer available" }));
         }
     }
     else
     {
         return(BadRequest(new { message = "Not authenticated" }));
     }
 }
Exemple #7
0
        private void ButtonAddToCart(object sender, RoutedEventArgs e)
        {
            using (var context = new FarmersMarketContext((Application.Current as App).ConnectionString))
            {
                var cartService   = new CartService(context);
                var sellerProduct = context.SellerProducts.SingleOrDefault(product => product.Id == SellerId);
                var customer      = context.Customers.SingleOrDefault(user => user.UserId == (Application.Current as App).currentUser.Id);


                var cart = new Cart
                {
                    SellerProduct = sellerProduct,
                    Customer      = customer,
                    Count         = sellerProduct.Count
                };
                cartService.AddProductToCart(cart);
            }
            MessageBox.Show("Товар добавлен в корзину");
        }
Exemple #8
0
        public ActionResult AddToCart(int id)
        {
            if (!CheckCartIsCreated())
            {
                Session["Cart"] = new List <ProductIdWithQuantity>();
            }

            var cart = (List <ProductIdWithQuantity>)Session["Cart"];

            _cartService.AddProductToCart(cart, id);

            Session["Cart"]      = cart;
            Session["CartCount"] = cart.Sum(p => p.Quantity);

            if (Request.UrlReferrer != null)
            {
                return(Redirect(Request.UrlReferrer.ToString()));
            }

            return(RedirectToAction("Index", "Home"));
        }
        public void AnyProductToCartShouldAddProductToCart()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "CartsService")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            var user = new User
            {
                UserName = "******",
                Cart     = new Cart()
            };

            dbContext.Add(user);
            dbContext.SaveChanges();

            var userService = new Mock <IUsersService>();

            userService.Setup(r => r.GetUserByUsername(user.UserName))
            .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == user.UserName));

            var productId      = "productId1";
            var productService = new Mock <IProductsSerivce>();

            productService.Setup(x => x.GetProductById(productId))
            .Returns(new Product {
                Name = "Ball"
            });

            var cartsService = new CartService(dbContext, productService.Object, userService.Object);

            cartsService.AddProductToCart(productId, user.UserName);

            var cartProducts = dbContext.CartProducts.ToList();

            Assert.Single(cartProducts);
            Assert.Equal(user.CartId, cartProducts.First().CartId);
        }
        public void AnyProductsShouldReturnTrueWhenThereAreProducts()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CartService")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            var user = new User {
                UserName = "******", Cart = new Cart()
            };

            dbContext.Users.Add(user);

            var product = new Product {
                Name = "USB Cable"
            };

            dbContext.Products.Add(product);
            dbContext.SaveChanges();

            var userService = new Mock <IUsersService>();

            userService.Setup(r => r.GetUserByUsername(user.UserName))
            .Returns(user);

            var productService = new Mock <IProductsSerivce>();

            productService.Setup(p => p.GetProductById(product.Id))
            .Returns(product);

            var cartsService = new CartService(dbContext, productService.Object, userService.Object);

            cartsService.AddProductToCart(product.Id, user.UserName);

            var areThereAnyProducts = cartsService.AnyProducts(user.UserName);

            Assert.True(areThereAnyProducts);
        }
        public void AddProductInCartWithInvalidProductShouldNotAddProduct()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddProductInCart_Cart_Database")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            var username = "******";
            var user     = new User()
            {
                UserName = username, Cart = new Cart()
            };

            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            var userService = new Mock <IUsersService>();

            userService.Setup(r => r.GetUserByUsername(username))
            .Returns(dbContext.Users.FirstOrDefault(x => x.UserName == username));

            var     productId      = "productId1";
            Product product        = null;
            var     productService = new Mock <IProductsSerivce>();

            productService.Setup(p => p.GetProductById(productId))
            .Returns(product);

            var cartsService = new CartService(dbContext, productService.Object, userService.Object);

            cartsService.AddProductToCart(productId, username);

            var cartProducts = dbContext.CartProducts.ToList();

            Assert.Empty(cartProducts);
        }