public void AddToCart(Products product)
        {
            var cartItems = _cartRepo.GetAll();

            if (cartItems.Count == 0)
            {
                var cartItem = new ShoppingCarts(product, 1, ShoppingCartId);
                _cartRepo.Save(cartItem);
            }
            else if (cartItems.Count > 0)
            {
                var cartItem =
                    cartItems.FirstOrDefault(x => x.CartId == ShoppingCartId && x.Product.Name == product.Name);
                if (cartItem == null)
                {
                    cartItem = new ShoppingCarts(product, 1, ShoppingCartId);
                    _cartRepo.Save(cartItem);
                }
                else
                {
                    cartItem.Quantity++;
                }
                _cartRepo.Update(cartItem);
            }
        }
 public void Update(ShoppingCarts cartItem)
 {
     using (_session.BeginTransaction())
     {
         _session.Update(cartItem);
         _session.Transaction.Commit();
     }
 }
 public void Save(ShoppingCarts entity)
 {
     _session.Save(entity);
 }
Exemplo n.º 4
0
        public void ShoppingCart()
        {
            var conn = new NHibernateConnection();
            var repo = new ShoppingCartRepository(conn);
            var producRepo = new ProductsRepository(conn);
            IList<Products> products = producRepo.GetAll();

            Products product1 = products.First();
            IEnumerable<Products> product2 = products.Where(x => x.IsRecent).Take(1);

            var shoppingCart = new ShoppingCarts(product1, 1, "12345678", DateTime.Now);

            repo.Save(shoppingCart);

            ShoppingCarts fromDB = repo.GetAll().Where(x => x.CartId == "12345678").FirstOrDefault();

            Assert.IsNotNull(fromDB);
        }