public ActionResult AddToCart(ShoppingCart cart, string movie)
        {
            var movieObj = _moviesRepository.GetMovieByTitle(movie.ReplaceDashes(" "));

            _shoppingCartService.AddToCart(ref cart, movieObj);

            return RedirectToAction("Index", cart);
        }
        public void MigrateShoppingCart(string email, ShoppingCart shoppingCart)
        {
            if (string.IsNullOrEmpty(email))
                throw new ArgumentException();
            if (shoppingCart == null)
                throw new ArgumentException();

            ShoppingCartDb.Add(shoppingCart);
        }
        public ActionResult CartSummary(ShoppingCart cart)
        {
            var viewModel = new ShoppingCartCartSummaryViewModel
                                {
                                    CartItemCount = cart.CartItems.Count
                                };

            return PartialView("_CartSummary", viewModel);
        }
 public void SaveOrUpdate(ShoppingCart shoppingCart)
 {
     if (_shoppingCarts.Contains(shoppingCart)) {
         _shoppingCarts.Remove(shoppingCart);
         _shoppingCarts.Add(shoppingCart);
     }
     else
         _shoppingCarts.Add(shoppingCart);
 }
        public ActionResult Index(ShoppingCart cart)
        {
            var viewModel = new ShoppingCartIndexViewModel
                                {
                                    CartItems = cart.CartItems,
                                    TotalValue = _shoppingCartService.ComputeTotalValue(cart)
                                };

            return View(viewModel);
        }
        public void Can_add_movie_to_shopping_cart()
        {
            var cart = new ShoppingCart();
            var movieName = _moviesRepository.GetAll()[0].Title;

            Assert.AreEqual(0, cart.CartItems.Count);

            _shoppingCartController.AddToCart(cart, movieName);

            Assert.Greater(cart.CartItems.Count, 0);
        }
        public void CartService_can_be_emptied()
        {
            var cart = new ShoppingCart();

            cart.CartItems.Add(new CartItem
                                   {
                                       Movie = new Movie(),
                                       Quantity = 2
                                   });

            Assert.AreEqual(1, cart.CartItems.Count);
            _shoppingCartService.EmptyCart(ref cart);
            Assert.AreEqual(0, cart.CartItems.Count);
        }
        public void RemoveFromCart(ref ShoppingCart shoppingCart, Guid movieId, out int quantity)
        {
            var cartItemToRemove = shoppingCart.CartItems
                .SingleOrDefault(x => x.Movie.Id == movieId);

            if(cartItemToRemove.Quantity > 1) {
                cartItemToRemove.Quantity--;
                quantity = cartItemToRemove.Quantity;
            }
            else {
                shoppingCart.CartItems.Remove(cartItemToRemove);
                quantity = 0;
            }
        }
        public void Can_remove_movie_from_shopping_cart()
        {
            const int quantity = 2;
            var movie = _moviesRepository.GetAll()[0];
            var cart = new ShoppingCart();
            cart.CartItems.Add(new CartItem
                                   {
                                       Movie = movie,
                                       Quantity = quantity
                                   });

            Assert.AreEqual(quantity, cart.CartItems[0].Quantity);

            _shoppingCartController.RemoveFromCart(cart, movie.Id);

            Assert.Less(cart.CartItems[0].Quantity, quantity);
        }
        public FakeAuthenticationService()
        {
            Users = new List<User>();
            _cartItems = new List<CartItem>();
            _cartItems.Add(new CartItem
                               {
                                   Movie = new Movie
                                               {
                                                   Title = "Movie_1",
                                                   Price = 19.99m
                                               },
                                   Quantity = 3
                               });
            _cartItems.Add(new CartItem
                               {
                                   Movie = new Movie
                                               {
                                                   Title = "Movie_2",
                                                   Price = 49.99m
                                               },
                                   Quantity = 1
                               });

            var cart = new ShoppingCart
                           {
                               CartItems = _cartItems
                           };

            User = new User
                       {
                           Email = "ValidEmail",
                           Password = "******",
                           ShoppingCart = cart,
                           UserProfile = new UserProfile
                                             {
                                                 Address = "Address",
                                                 City = "City",
                                                 Country = "Country",
                                                 FirstName = "FirstName",
                                                 LastName = "LastName",
                                                 PostalCode = "PostalCode"
                                             }
                       };

            Users.Add(User);
        }
        public void AddToCart(ref ShoppingCart shoppingCart, Movie movie)
        {
            Check.Require(shoppingCart != null);
            Check.Require(movie != null);

            var cartItem = shoppingCart.CartItems.FirstOrDefault(x => x.Movie.Id == movie.Id);

            if(cartItem == null) {
                cartItem = new CartItem {
                    Movie = movie,
                    Quantity = 1
                };

                shoppingCart.CartItems.Add(cartItem);
            }
            else {
                cartItem.Quantity++;
            }
        }
        public void CartService_decements_quantity_of_catItem_if_more_then_one()
        {
            int newQuantity;
            const int expectedCartItemCount = 1;
            const int expectedQuantity = 2;
            const int expectedNewQuantity = 1;
            var movies = GetAllMovies();
            var m1 = movies[0];

            var cart = new ShoppingCart();
            _shoppingCartService.AddToCart(ref cart, m1);
            _shoppingCartService.AddToCart(ref cart, m1);

            Assert.AreEqual(expectedCartItemCount, cart.CartItems.Count);
            Assert.AreEqual(expectedQuantity, cart.CartItems[0].Quantity);

            _shoppingCartService.RemoveFromCart(ref cart, m1.Id, out newQuantity);

            Assert.AreEqual(expectedNewQuantity, cart.CartItems[0].Quantity);
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.Model != null)
                throw new InvalidOperationException("Cannot update instances");

            ShoppingCart cart;
            if (controllerContext.HttpContext.User.Identity.IsAuthenticated) {
                var user = _userRepository.GetUserByEmail(controllerContext.HttpContext.User.Identity.Name);
                cart = user.ShoppingCart;
            }
            else {
                cart = controllerContext.HttpContext.Session[ShoppingCartService.CartSessionKey] as ShoppingCart;

                if (cart == null) {
                    cart = new ShoppingCart();
                    controllerContext.HttpContext.Session[ShoppingCartService.CartSessionKey] = cart;
                }
            }

            return cart;
        }
        public void AddToCart(ref ShoppingCart shoppingCart, Movie movie)
        {
            Check.Require(shoppingCart != null);
            Check.Require(movie != null);

            var cartItem = shoppingCart.CartItems.FirstOrDefault(x => x.Movie.Id == movie.Id);

            if (cartItem == null) {
                cartItem = new CartItem
                               {
                                   Movie = movie,
                                   Quantity = 1
                               };

                shoppingCart.CartItems.Add(cartItem);
            }
            else {
                cartItem.Quantity++;
            }

            if (_authenticationService.IsAuthenticated())
                _cartItemRepository.SaveOrUpdate(cartItem);
        }
        public ActionResult RemoveFromCart(ShoppingCart cart, Guid id)
        {
            int newQuantity;

            var movieTitle = _moviesRepository.Get(id).Title;

            _shoppingCartService.RemoveFromCart(ref cart, id, out newQuantity);

            var results = new ShoppingCartRemoveViewModel
                              {
                                  Message = Server.HtmlEncode(movieTitle) +
                                            " has been removed from your shopping cart.",
                                  CartItemCount = cart.CartItems.Sum(x => x.Quantity),
                                  TotalValue = _shoppingCartService.ComputeTotalValue(cart),
                                  DeletedItem = new ShoppingCartRemoveViewModel.DeletedCartItem
                                                    {
                                                        Id = id,
                                                        Quantity = newQuantity
                                                    }
                              };

            return Json(results);
        }
        public void CartService_combines_movies_with_same_CartItem()
        {
            var movies = GetAllMovies();
            var m1 = movies[0];
            var m2 = movies[1];
            const int expectedMovieLineCount = 2;
            const int expectedM1Quantity = 2;
            const int expectedM2Quantity = 1;

            var cart = new ShoppingCart();
            _shoppingCartService.AddToCart(ref cart, m1);
            _shoppingCartService.AddToCart(ref cart, m1);
            _shoppingCartService.AddToCart(ref cart, m2);

            Assert.AreEqual(expectedMovieLineCount, cart.CartItems.Count);
            Assert.AreEqual(expectedM1Quantity, cart.CartItems
                                                    .FirstOrDefault(x => x.Movie.Id == m1.Id)
                                                    .Quantity);

            Assert.AreEqual(expectedM2Quantity, cart.CartItems
                                                    .FirstOrDefault(x => x.Movie.Id == m2.Id)
                                                    .Quantity);
        }
        public override MembershipUser CreateUser(string username, string password,
                                                  string email, string passwordQuestion,
                                                  string passwordAnswer, bool isApproved,
                                                  object providerUserKey, out MembershipCreateStatus status)
        {
            var args = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);

            if (args.Cancel) {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (RequiresUniqueEmail && GetUserNameByEmail(email) != string.Empty) {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            var user = GetUser(email, true);

            if (user == null) {
                var shoppingCart = new ShoppingCart();
                _shoppingCartRepository.SaveOrUpdate(shoppingCart);

                var userProfile = new UserProfile();
                _userProfileRepository.SaveOrUpdate(userProfile);

                var userObj = new User
                                  {
                                      Email = email,
                                      Password = _hashProvider.ComputeHash(password),
                                      ShoppingCart = shoppingCart,
                                      UserProfile = userProfile
                                  };

                _userRepository.SaveOrUpdate(userObj);

                status = MembershipCreateStatus.Success;

                return GetUser(userObj.Email, true);
            }

            status = MembershipCreateStatus.DuplicateEmail;

            return user;
        }
        public void MigrateShoppingCart(string email, ShoppingCart shoppingCart)
        {
            Check.Require(!string.IsNullOrEmpty(email));

            var user = _userRepository.GetUserByEmail(email);

            foreach (var ci in shoppingCart.CartItems) {
                _cartItemRepository.SaveOrUpdate(ci);
                user.ShoppingCart.CartItems.Add(ci);
            }

            _shoppingCartRepository.SaveOrUpdate(user.ShoppingCart);
        }
        public void CartService_TotalValue_is_sum_of_price_times_quantity()
        {
            var movies = GetAllMovies();

            var cart = new ShoppingCart();
            _shoppingCartService.AddToCart(ref cart, movies[0]);
            _shoppingCartService.AddToCart(ref cart, movies[1]);
            _shoppingCartService.AddToCart(ref cart, movies[2]);

            var expectedTotalPrice = cart.CartItems.Sum(x => x.Movie.Price * x.Quantity);

            Assert.AreEqual(expectedTotalPrice, _shoppingCartService.ComputeTotalValue(cart));
        }
        public void RemoveFromCart(ref ShoppingCart shoppingCart, Guid movieId, out int quantity)
        {
            Check.Require(shoppingCart != null, "provided shopping cart is null");

            var cartItemToRemove = shoppingCart.CartItems
                .SingleOrDefault(x => x.Movie.Id == movieId);

            if (cartItemToRemove.Quantity > 1) {
                cartItemToRemove.Quantity--;
                quantity = cartItemToRemove.Quantity;

                if(_authenticationService.IsAuthenticated())
                    _cartItemRepository.SaveOrUpdate(cartItemToRemove);
            }
            else {
                shoppingCart.CartItems.Remove(cartItemToRemove);
                quantity = 0;

                if(_authenticationService.IsAuthenticated())
                    _cartItemRepository.Delete(cartItemToRemove);
            }
        }
        public void SaveOrUpdate(ShoppingCart shoppingCart)
        {
            Check.Require(shoppingCart != null, "ShoppingCart is null");

            Session.SaveOrUpdate(shoppingCart);
        }
        public ActionResult ClearCart(ShoppingCart cart)
        {
            _shoppingCartService.EmptyCart(ref cart);

            return RedirectToAction("Index", cart);
        }
 public void EmptyCart(ref ShoppingCart shoppingCart)
 {
     throw new NotImplementedException();
 }
 public decimal ComputeTotalValue(ShoppingCart shoppingCart)
 {
     return shoppingCart.CartItems.Sum(x => x.Movie.Price * x.Quantity);
 }
        public void EmptyCart(ref ShoppingCart shoppingCart)
        {
            shoppingCart.CartItems.Clear();

            if(_authenticationService.IsAuthenticated())
                _shoppingCartRepository.SaveOrUpdate(shoppingCart);
        }