Пример #1
0
        public ActionResult <CheckoutBasket> Update(BasketUpdateRequest basketUpdateRequest)
        {
            var menuItem = _menuItemRepository.Get(basketUpdateRequest.Id);

            if (menuItem == null)
            {
                return(NotFound());
            }

            var checkOutBasket = GetCheckOutBasket();

            if (basketUpdateRequest.Quantity > 0)
            {
                if (checkOutBasket.BasketItems.Any(x => x.MenuItem.Id == basketUpdateRequest.Id))
                {
                    checkOutBasket.BasketItems.First(x => x.MenuItem.Id == menuItem.Id).Quantity = basketUpdateRequest.Quantity;
                }
                else
                {
                    checkOutBasket.BasketItems.Add(new CheckoutBasketItem(menuItem)
                    {
                        Quantity = basketUpdateRequest.Quantity
                    });
                }

                SaveCheckoutBasket(checkOutBasket);
            }
            else
            {
                DeleteMenuItem(basketUpdateRequest.Id);
            }

            return(GetCheckOutBasket());
        }
Пример #2
0
 public async Task<SuccessResponse> UpdateBasket(Product product, int quantity)
 {
     var request = new BasketUpdateRequest
     {
         Product = product,
         Quantity = quantity
     };
     return await PutApi<SuccessResponse, BasketUpdateRequest>("api/basket", request);
 }
Пример #3
0
        public async Task <IActionResult> Put([FromBody] BasketUpdateRequest request)
        {
            var basketKeyHeader = Request.Headers["BasketKey"];

            if (basketKeyHeader == StringValues.Empty)
            {
                throw new InvalidOperationException("BasketKey was missing");
            }

            var basketKey = basketKeyHeader[0];
            await _basketRepository.UpdateBasket(basketKey, request.Product, request.Quantity);

            return(Json(new SuccessResponse
            {
                Success = true
            }));
        }