// GET: /Cart/Add
        public ActionResult Add(Guid id)
        {
            var cartId = GetCartId();

            var createNewCart = cartId == Guid.Empty;

            CartSummaryViewModel summaryModel;

            if (createNewCart == false)
            {
                var editCartRequest = new EditCartRequest();

                editCartRequest.ItemsToAdd.Add(id);

                editCartRequest.Id = cartId;

                try
                {
                    var editCartResponse = _cartService.EditCart(editCartRequest);

                    summaryModel = editCartResponse.Cart.ConvertToCartSummaryViewModel();

                    SaveCartSummaryToCookie(summaryModel.NumberOfItems, summaryModel.CartTotal.ToString(CultureInfo.InvariantCulture));
                }
                catch (CartDoesNotExistException)
                {
                    createNewCart = true;
                }
            }

            if (!createNewCart) { return RedirectToLocal("/Cart"); }

            var createCartRequest = new CreateCartRequest();

            createCartRequest.AlbumsToAdd.Add(id);

            var response = _cartService.CreateCart(createCartRequest);

            SaveCartIdToCookie(response.Cart.Id);

            summaryModel = response.Cart.ConvertToCartSummaryViewModel();

            SaveCartSummaryToCookie(summaryModel.NumberOfItems, summaryModel.CartTotal.ToString(CultureInfo.InvariantCulture));

            return RedirectToLocal("/Cart");
        }
        public EditCartResponse EditCart(EditCartRequest request)
        {
            var response = new EditCartResponse();

            var cart = _cartRepository.FindBy(request.Id);

            if (cart == null) { throw new CartDoesNotExistException(); }

            AddAlbumsToCart(request.ItemsToAdd, cart);

            UpdateLineQtys(request.ItemsToUpdate, cart);

            RemoveAlbumsFromCart(request.ItemsToRemove, cart);

            ThrowExceptionIfCartIsInvalid(cart);

            _cartRepository.Save(cart);

            _uow.Commit();

            response.Cart = cart.ConvertToCartView();

            return response;
        }
        public ActionResult Update(Guid cartItemId, Guid albumId, int newQty)
        {
            var qtyUpdateRequest = new AlbumQtyUpdateRequest { AlbumId = albumId, NewQty = newQty };

            var request = new EditCartRequest();

            request.ItemsToUpdate.Add(qtyUpdateRequest);
            request.Id = GetCartId();

            var response = _cartService.EditCart(request);

            SaveCartSummaryToCookie(response.Cart.NumberOfItems, response.Cart.CartTotal.ToString(CultureInfo.InvariantCulture));

            var albumName = response.Cart.Items.Single(item => item.Album.Id == albumId).Album.Title;

            var results = new UpdateCartItemViewModel
            {
                Message = "The qty of " + Server.HtmlEncode(albumName) + " has been refreshed in your shopping cart.",
                CartTotal = response.Cart.CartTotal.FormatMoney(),
                CartCount = response.Cart.NumberOfItems,
                ItemQty = response.Cart.Items.Single(item => item.Album.Id == albumId).Qty,
                DeleteId = cartItemId
            };

            return Json(results);
        }
        public ActionResult Remove(Guid cartItemId, Guid albumId)
        {
            var album = _albumService.GetAlbum(new GetAlbumRequest { Id = albumId }).Album;

            var request = new EditCartRequest();

            request.ItemsToRemove.Add(albumId);
            request.Id = GetCartId();

            var response = _cartService.EditCart(request);

            SaveCartSummaryToCookie(response.Cart.NumberOfItems, response.Cart.CartTotal.ToString(CultureInfo.InvariantCulture));

            var results = new RemoveCartItemViewModel
            {
                Message = Server.HtmlEncode(album.Title) + " has been removed from your cart.",
                CartTotal = response.Cart.CartTotal.FormatMoney(),
                CartCount = response.Cart.NumberOfItems,
                ItemQty = 0,
                DeleteId = cartItemId
            };

            return Json(results);
        }