예제 #1
0
        public void AddToCart(Car car)
        {
            // Get the matching cart and car instances
            /*var cartItem = catalogDB.Carts.SingleOrDefault(
            c => c.CartId == ShoppingCartId
            && c.CarId == car.CarId);*/

            ShoppingCartDAL dal = new ShoppingCartDAL();
            var cartItem = dal.MatchingCarAndCart(ShoppingCartId,car);

            if (cartItem == null)
            {
            // Create a new cart item if no cart item exists
            cartItem = new Cart
            {
            CarId = car.CarId,
            CartId = ShoppingCartId,
            Count = 1,
            DateCreated = DateTime.Now
            };
            /*catalogDB.Carts.Add(cartItem);*/
            dal.AddCartItem(cartItem);
            }
            else
            {
            // If the item does exist in the cart, then add one to the quantity
            cartItem.Count++;
            }
            // Save changes
            /*catalogDB.SaveChanges();*/
            dal.SaveChangestoCart();
        }
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            // Get the name of the car to display confirmation
            /*string carName = catalogDB.Carts
            .Single(item => item.RecordId == id).Car.Title;*/

            ShoppingCartDAL dal = new ShoppingCartDAL();
            string carName = dal.getCarName(id);

            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);
            // Display the confirmation message
            var results = new ShoppingCartRemoveViewModel
            {
                Message = Server.HtmlEncode(carName) +
              " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };
            return Json(results);
        }
        //
        // GET: /Catalog/AddToCart/5
        public ActionResult AddToCart(int id)
        {
            // Retrieve the car from the database
            // var addedCar = catalogDB.Cars
            // .Single(car => car.CarId == id);
            // Add it to the shopping cart
            ShoppingCartDAL dal = new ShoppingCartDAL();
            var addedCar = dal.retrieveCar(id);

            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedCar);
            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }
예제 #4
0
        public int RemoveFromCart(int id)
        {
            // Get the cart
            /*var cartItem = catalogDB.Carts.Single(
            cart => cart.CartId == ShoppingCartId
            && cart.RecordId == id);*/

            ShoppingCartDAL dal = new ShoppingCartDAL();
            var cartItem = dal.GetTheCart(ShoppingCartId, id);

            int itemCount = 0;
            if (cartItem != null)
            {
            if (cartItem.Count > 1)
            {
            cartItem.Count--;
            itemCount = cartItem.Count;
            }
            else
            {
            /*catalogDB.Carts.Remove(cartItem);*/
            dal.RemoveCartItem(cartItem);
            }
            // Save changes
            /*catalogDB.SaveChanges();*/
            dal.SaveCartChanges();
            }
            return itemCount;
        }
예제 #5
0
 public void EmptyCart()
 {
     /*  var cartItems = catalogDB.Carts.Where(cart => cart.CartId == ShoppingCartId);
     foreach (var cartItem in cartItems)
     {
     catalogDB.Carts.Remove(cartItem);
     }
     // Save changes
     catalogDB.SaveChanges();*/
     ShoppingCartDAL dal = new ShoppingCartDAL();
     dal.RemoveAllItemsFromCart(ShoppingCartId);
 }