public JsonResult AddToBasket(int productId)
        {
            BasketSummaryView basketSummary = new BasketSummaryView();
            Guid basketId = base.GetBasketId();
            bool createNewBasket = basketId == Guid.Empty;

            if (!createNewBasket)
            {
                ModifyBasketRequest modifyBasketRequest = new ModifyBasketRequest();
                modifyBasketRequest.ProductsToAdd.Add(productId);
                modifyBasketRequest.BasketId = basketId;

                try
                {
                    ModifyBasketResponse response = _basketService.ModifyBasket(modifyBasketRequest);
                    basketSummary = response.Basket.ConvertToSummary();
                    SaveBasketSummaryToCookie(basketSummary.NumberOfItems, basketSummary.BasketTotal);
                }
                catch (BasketDoesNotExistException ex)
                {
                    createNewBasket = true;
                }
            }
            if (createNewBasket)
            {
                CreateBasketRequest createBasketRequest = new CreateBasketRequest();
                createBasketRequest.ProductsToAdd.Add(productId);

                CreateBasketResponse response = _basketService.CreateBasket(createBasketRequest);
                SaveBasketIdToCookie(response.Basket.Id);
                basketSummary = response.Basket.ConvertToSummary();
                SaveBasketSummaryToCookie(basketSummary.NumberOfItems, basketSummary.BasketTotal);
            }
            return Json(basketSummary);
        }
        public ModifyBasketResponse ModifyBasket(ModifyBasketRequest request)
        {
            ModifyBasketResponse response = new ModifyBasketResponse();
            Basket basket = _basketRepository.FindBy(request.BasketId);
            if (basket == null)
                throw new BasketDoesNotExistException();

            AddProductsToBasket(request.ProductsToAdd, basket);
            UpdateLineQtys(request.ItemsToUpdate, basket);
            RemoveItemsFromBasket(request.ItemsToRemove, basket);

            if (request.SetShippingServiceIdTo != 0)
            {
                DeliveryOption deliveryOption = _deliveryOptionRepository.FindBy(request.SetShippingServiceIdTo);
                basket.SetDeliveryOption(deliveryOption);
            }

            ThrowExceptionIfBasketIsInvalid(basket);

            _basketRepository.Save(basket);
            _uow.Commit();

            response.Basket = basket.ConvertToBasketView();
            return response;
        }
        public JsonResult RemoveItem(int productId)
        {
            ModifyBasketRequest request = new ModifyBasketRequest();
            request.ItemsToRemove.Add(productId);

            request.BasketId = base.GetBasketId();
            ModifyBasketResponse response = _basketService.ModifyBasket(request);

            SaveBasketSummaryToCookie(response.Basket.NumberOfItems, response.Basket.BasketTotal);

            BasketDetailView basketDetailView = new BasketDetailView();
            basketDetailView.BasketSummary = new BasketSummaryView()
            {
                BasketTotal = response.Basket.BasketTotal,
                NumberOfItems = response.Basket.NumberOfItems
            };

            basketDetailView.Basket = response.Basket;
            basketDetailView.DeliveryOptions = _basketService.GetAllDispatchOptions().DeliveryOptions;

            return Json(basketDetailView);
        }
        public JsonResult UpdateShipping(int shippingServiceId)
        {
            ModifyBasketRequest request = new ModifyBasketRequest();
            request.SetShippingServiceIdTo = shippingServiceId;
            request.BasketId = base.GetBasketId();

            BasketDetailView basketDetailView = new BasketDetailView();

            ModifyBasketResponse response = _basketService.ModifyBasket(request);
            SaveBasketSummaryToCookie(response.Basket.NumberOfItems, response.Basket.BasketTotal);

            basketDetailView.BasketSummary = new BasketSummaryView
            {
                BasketTotal = response.Basket.BasketTotal,
                NumberOfItems = response.Basket.NumberOfItems
            };

            basketDetailView.Basket = response.Basket;
            basketDetailView.DeliveryOptions = _basketService.GetAllDispatchOptions().DeliveryOptions;

            return Json(basketDetailView);
        }
        public JsonResult UpdateItems(JsonBasketQtyUpdateRequest jsonBasketQtyUpdateRequest)
        {
            ModifyBasketRequest request = new ModifyBasketRequest();
            request.BasketId = base.GetBasketId();
            request.ItemsToUpdate = jsonBasketQtyUpdateRequest.ConvertToBasketItemsUpdateRequests();

            BasketDetailView basketDetailView = new BasketDetailView();
            ModifyBasketResponse response = _basketService.ModifyBasket(request);
            SaveBasketSummaryToCookie(response.Basket.NumberOfItems, response.Basket.BasketTotal);

            basketDetailView.BasketSummary = new BasketSummaryView
            {
                BasketTotal = response.Basket.BasketTotal,
                NumberOfItems = response.Basket.NumberOfItems
            };
            basketDetailView.Basket = response.Basket;
            basketDetailView.DeliveryOptions = _basketService.GetAllDispatchOptions().DeliveryOptions;
            return Json(basketDetailView);
        }