Beispiel #1
0
        /// <summary>
        /// Add or update the quantity of a given product into a cart
        /// Also creates a new cart if none exists
        /// </summary>
        /// <param name="cartId"></param>
        /// <param name="item"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public Cart SetCartItem(string cartId, CartItem item, out CartOperationError error)
        {
            error = new CartOperationError();
            if (item.Quantity <= 0)
            {
                error.StatusCode = 400;
                error.Message    = "Invalid quantity";
                return(null);
            }

            if (!memoryCache.TryGetValue(string.Format(CartCacheKeyFormat, cartId), out Cart cart))
            {
                cart = CreateCart();
            }

            var quantity = item.Quantity;

            if (cart.CartItems.TryGetValue(item.ProductId, out var existingItem))
            {
                quantity -= existingItem.Quantity;
            }

            if (!productCacheService.TryUpdateRetained(quantity, item.ProductId, out var product, out error))
            {
                error.StatusCode = 400;
                error.Message    = "Failed to add product to cart";
                return(null);
            }
            cart.CartItems[item.ProductId] = item;
            StoreCartToCache(string.Format(CartCacheKeyFormat, cart.Id), cart);
            hubContext.Clients.All.SendAsync(AppEvents.ProductUpdated, product).Wait();
            return(cart);
        }
Beispiel #2
0
        /// <summary>
        /// Removes an item for a given cart
        /// </summary>
        /// <param name="cartId"></param>
        /// <param name="productId"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public Cart RemoveCartItem(string cartId, int productId, out CartOperationError error)
        {
            error = new CartOperationError();
            if (!memoryCache.TryGetValue(string.Format(CartCacheKeyFormat, cartId), out Cart cart))
            {
                error.StatusCode = 404;
                error.Message    = "Cart not found";
                return(null);
            }

            if (!cart.CartItems.TryGetValue(productId, out var existingItem))
            {
                error.StatusCode = 400;
                error.Message    = "Product not added to cart";
                return(null);
            }

            if (!productCacheService.TryUpdateRetained(-existingItem.Quantity, productId, out var product, out error))
            {
                return(null);
            }

            cart.CartItems.Remove(productId);
            StoreCartToCache(string.Format(CartCacheKeyFormat, cart.Id), cart);
            hubContext.Clients?.All?.SendAsync(AppEvents.ProductUpdated, product);
            return(cart);
        }
Beispiel #3
0
        /// <summary>
        /// Clears the items of a given cart
        /// </summary>
        /// <param name="cartId"></param>
        /// <returns></returns>
        public Cart ClearCart(string cartId, out CartOperationError error)
        {
            var cacheKey = CartCacheKey(cartId);

            error = new CartOperationError();
            if (!memoryCache.TryGetValue(cacheKey, out Cart cart))
            {
                error.StatusCode = 404;
                error.Message    = "Cart not found";
                return(null);
            }

            foreach (var retainedItem in cart.CartItems)
            {
                if (!this.productCacheService.TryUpdateRetained(-retainedItem.Value.Quantity, retainedItem.Key,
                                                                out var product, out error))
                {
                    Log.Error("Failed to clear cart " + cartId);
                    Log.Error(error.Message);
                    return(null);
                }
                this.hubContext.Clients?.All?.SendAsync(AppEvents.ProductUpdated, product).Wait();
            }
            cart.CartItems.Clear();
            StoreCartToCache(cacheKey, cart);
            return(cart);
        }