public IActionResult AddToCart(int?id)
        {
            Items item = customerObject.GetItemById(id);

            customerObject.AddToCart(item);
            return(View(item));
        }
 public ActionResult AddToCart(CompanyProduct selected)
 {
     if (CheckIfCustomer())
     {
         if (ModelState.IsValid)
         {
             CompanyProduct product = comprodrepo.GetById(selected.ProductId);
             if (product.Quantity >= selected.Quantity)
             {
                 var inCart = customerrepo.GetProductFromCart(selected.ProductId);
                 if (inCart == null)
                 {
                     product.Quantity = selected.Quantity;
                     customerrepo.AddToCart(product);
                     return(RedirectToAction("Index", "CompanyProduct"));
                 }
                 else
                 {
                     var count = inCart.Quantity + selected.Quantity;
                     if (product.Quantity >= count)
                     {
                         product.Quantity = selected.Quantity;
                         customerrepo.AddToCart(product);
                         return(RedirectToAction("Index", "CompanyProduct"));
                     }
                     else
                     {
                         var canget = product.Quantity - inCart.Quantity;
                         TempData["Error"] = "Your previous cart quantiry and recent cart quantity are extending available quantity. You can add max " + canget + " Of this product to your cart";
                         return(RedirectToAction("ViewProduct", "CompanyProduct", new { id = selected.ProductId }));
                     }
                 }
             }
             TempData["Error"] = "Quantity must be less then or equals to available quantity";
             return(RedirectToAction("ViewProduct", "CompanyProduct", new { id = selected.ProductId }));
         }
         else
         {
             TempData["Error"] = "Can't be empty and Quentity must be in between 1 to available quantities";
             return(RedirectToAction("ViewProduct", "CompanyProduct", new { id = selected.ProductId }));
         }
     }
     else
     {
         return(RedirectToAction("Login", "Home"));
     }
 }
示例#3
0
        public void AddToCartTest()
        {
            //Arrange
            using (var context = new AppDbContext(options))
            {
                var customer = new Customer {
                    Id = 1
                };
                var repo = new CustomerRepository(context);

                customer.Cart       = new Cart();
                customer.Cart.Goods = new List <GoodCart>();
                Good good = new Good {
                    Id = 1, Name = "test"
                };
                //Act
                repo.AddToCart(good, customer);
                var expectedResult = 1;
                var actualResult   = customer.Cart.Goods.Count;

                //Assert
                Assert.AreEqual(expectedResult, actualResult);
            }
        }