示例#1
0
        /// <summary>
        /// Adds new item to the cart. If the product exists increases its quantity
        /// </summary>
        /// <param name="productsApi">Products Api Gateway</param>
        /// <param name="newCartItem">NewCartItem to be added</param>
        internal void AddToCart(IProductsApiConnector productsApi, NewCartItem newCartItem)
        {
            if (productsApi == null)
            {
                throw new ArgumentNullException(nameof(productsApi));
            }

            if (newCartItem.UserId != this.UserId)
            {
                throw new ArgumentException(
                          "Supplied cartItem has a different user then this user cart",
                          nameof(newCartItem));
            }

            // If the product is already in the cart, there's no need to check for existence from api.
            bool newProduct = false;

            if (!this.items.TryGetValue(newCartItem.ProductId, out UserCartItem userCartItem))
            {
                userCartItem = InitializeUserCartItem(productsApi, newCartItem.ProductId);
                newProduct   = true;
            }

            userCartItem.IncreaseQuantity(productsApi, newCartItem.Quantity);
            if (newProduct)
            {
                this.items.Add(userCartItem.ProductId, userCartItem);
            }
        }
示例#2
0
        private UserCart InitializeUserCart(NewCartItem cartItem)
        {
            if (!this.userApi.UserExists(cartItem.UserId))
            {
                throw new CartException(
                          CartErrorCode.InvalidUser,
                          "Invalid user");
            }

            return(new UserCart(cartItem.UserId));
        }
示例#3
0
        /// <summary>
        /// Adds new cart item to the specified user.
        /// If the product is already in cart increases its quantity
        /// </summary>
        /// <param name="cartItem">Cart item to be added.</param>
        /// <returns>Awaitable task</returns>
        public async Task AddCartItemAsync(NewCartItem cartItem)
        {
            var userCart = await this.dataGateway.GetUserCartAsync(cartItem.UserId)
                           .ConfigureAwait(false);

            if (userCart == null)
            {
                userCart = this.InitializeUserCart(cartItem);
            }

            // No need to check for existing user since the user cart is already created.
            userCart.AddToCart(this.productsApi, cartItem);
            await this.dataGateway.SaveUserCartAsync(userCart).ConfigureAwait(false);
        }