Beispiel #1
0
        public async Task <ActionResult <CartResponse> > GetMyCart()
        {
            var customerId  = getUserMetaData().Id;
            var cachedValue = await cache.TryGet(customerId);

            if (cachedValue != null)
            {
                return(Ok(CartResponse.Of(cachedValue)));
            }

            var newCart = new Cart(customerId);

            await cache.Set(newCart);

            return(Ok(CartResponse.Of(newCart)));
        }
Beispiel #2
0
        public async Task <ActionResult <CartResponse> > UpdateMyCart([FromBody] UpdateCartRequest request)
        {
            var customerId = getUserMetaData().Id;
            var cart       = await cache.TryGet(customerId);

            if (cart == null)
            {
                cart = new Cart(customerId);
            }

            if (request.Count <= 0)
            {
                throw new WWSException("Count must be positive", StatusCodes.Status400BadRequest);
            }

            try
            {
                var item = await inventoryApiClient.GetInventoryItem(request.ItemId).ConfigureAwait(false);

                var updatedCart = cartService.AddCartItem(cart, item, request);

                await cache.Invalidate(customerId);

                await cache.Set(updatedCart);

                return(Ok(CartResponse.Of(updatedCart)));
            }
            catch (StockIsNotEnoughException)
            {
                throw new WWSException("Not enough item in stock", StatusCodes.Status400BadRequest);
            }
            catch (Refit.ApiException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new WWSException("Item not found", StatusCodes.Status404NotFound);
                }
                else
                {
                    throw;
                }
            }
        }