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

            if (createNewBasket == false)
            {
                ModifyBasketRequest modifyBasketRequest =
                    new ModifyBasketRequest();

                modifyBasketRequest.ProductsToAdd.Add(productId);
                modifyBasketRequest.BasketId = basketId;

                try
                {
                    ModifyBasketResponse response = _basketService
                                                    .ModifyBasket(modifyBasketRequest);
                    basketSummaryView = response.Basket.ConvertToSummary();
                    SaveBasketSummaryToCookie(basketSummaryView.NumberOfItems,
                                              basketSummaryView.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);
                basketSummaryView = response.Basket.ConvertToSummary();
                SaveBasketSummaryToCookie(basketSummaryView.NumberOfItems,
                                          basketSummaryView.BasketTotal);
            }

            return(Json(basketSummaryView));
        }
        public BasketSummaryView AddToBasket(int productId)
        {
            BasketSummaryView basketSummaryView = new BasketSummaryView();
            Guid basketId        = base.GetBasketId();
            bool createNewBasket = basketId == Guid.Empty;

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

                try {
                    var response = _basketService.ModifyBasket(modifyBasketRequest);
                    basketSummaryView = DTOs.BasketMapper.ConvertToSummary(response.Basket);
                    SaveBasketSummaryToCookie(basketSummaryView.NumberOfItems,
                                              basketSummaryView.BasketTotal);
                } catch (BasketDoesNotExistException ex) {
                    LoggingFactory.GetLogger().Log(
                        String.Format("Creating new basket because exception. {0}", ex.ToString()));
                    createNewBasket = true;
                }
            }

            if (createNewBasket)
            {
                var createBasketRequest = new CreateBasketRequest();
                createBasketRequest.ProductsToAdd.Add(productId);

                var response = _basketService.CreateBasket(createBasketRequest);

                SaveBasketIdToCookie(response.Basket.Id);
                basketSummaryView = DTOs.BasketMapper.ConvertToSummary(response.Basket);
                SaveBasketSummaryToCookie(basketSummaryView.NumberOfItems,
                                          basketSummaryView.BasketTotal);
            }

            return(basketSummaryView);
        }
示例#3
0
        public IActionResult AddToBasket(int productId)
        {
            BasketSummaryView basketSummaryView = new BasketSummaryView();
            Guid basketId        = 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);
                    basketSummaryView = response.Basket.ConvertToSummary();
                }
                catch (BasketDoesNotExistException)
                {
                    createNewBasket = true;
                }
            }

            if (createNewBasket)
            {
                CreateBasketRequest createBasketRequest = new CreateBasketRequest();
                createBasketRequest.CustomerEmail = _cookieAuthentication.GetAuthenticationToken();

                createBasketRequest.ProductsToAdd.Add(productId);

                CreateBasketResponse response = _basketService.CreateBasket(createBasketRequest);
                basketSummaryView = response.Basket.ConvertToSummary();
            }

            return(Ok(basketSummaryView));
        }