Exemple #1
0
        /// <summary>
        /// Removes the specified product from the cart.
        /// </summary>
        /// <param name="productId">The id of the product to remove.</param>
        public async Task RemoveProductAsync(int productId)
        {
            // First, checks there are items in the cart:
            if (this.IsEmpty())
            {
                return;
            }

            ClientCartProduct cartProduct = this.Cart.Products.FirstOrDefault(i => i.ProductId == productId);

            if (cartProduct != null)
            {
                _dbContext.ClientCartProducts.Remove(cartProduct);
                this.Cart.Products.Remove(cartProduct);
                if (this.Cart.Products.Count < 1)
                {
                    _dbContext.ClientCarts.Remove(this.Cart);
                }
                await _dbContext.SaveChangesAsync();
            }

            // If empty - deletes the cart id cookie and removes from the cache:
            if (this.IsEmpty())
            {
                if (_userIdentity.IsAuthenticated())
                {
                    User user = await _userIdentity.GetCurrentAsync();

                    user.ClientCartId = null;
                    user.ClientCart   = null;
                    _userCache.Set(user);
                    this.RemoveCacheAuthUserCart(user.Id);
                }
                else
                {
                    _httpContextAccessor.HttpContext.Response.Cookies.Delete(CART_COOKIE);
                    this.RemoveCacheClientCart(this.Cart.Id);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Sets (adds or updates) the specified product and quantity in the cart.
        /// </summary>
        /// <param name="productId">The id of the product to set.</param>
        /// <param name="quantity">The quantity to set.</param>
        public async Task SetProductAsync(int productId, int quantity)
        {
            if (quantity < 1)
            {
                throw new ArgumentException($"{nameof(quantity)} cannot be less than 1.", nameof(quantity));
            }

            // First, checks the product exists and valid in the database:
            if (!await _dbContext.Products.AnyAsync(p => p.Id == productId && p.IsAvailable))
            {
                return;
            }

            // Creates/Updates the cart:
            // Now, checks if it's the first item in the cart:
            if (this.IsEmpty())
            {
                // So creates a new cart:
                this.Cart          = new ClientCart();
                this.Cart.Products = new List <ClientCartProduct>();
                ClientCartProduct cartProduct = new ClientCartProduct()
                {
                    ClientCart = Cart,
                    ProductId  = productId,
                    Quantity   = quantity
                };
                this.Cart.Products.Add(cartProduct);
                _dbContext.ClientCarts.Add(this.Cart);
            }
            // Otherwise, it's not the first item:
            else
            {
                // Checks if the product already exists in the cart:
                ClientCartProduct cartProduct = this.Cart.Products.FirstOrDefault(p => p.ProductId == productId);
                if (cartProduct != null)
                {
                    // Updates the item:
                    cartProduct.Quantity = quantity;
                    _dbContext.ClientCartProducts.Update(cartProduct);
                }
                else
                {
                    // Adds the item:
                    cartProduct = new ClientCartProduct()
                    {
                        ClientCartId = this.Cart.Id,
                        ProductId    = productId,
                        Quantity     = quantity
                    };
                    _dbContext.ClientCartProducts.Add(cartProduct);
                }
            }

            // If the user is authenticated - connects the cart to him:
            User user = await _userIdentity.GetCurrentAsync();

            if (user != null && user.ClientCartId == null)
            {
                user.ClientCart = this.Cart;
                _dbContext.Users.Update(user);
            }

            await _dbContext.SaveChangesAsync();

            // Gets the cart back from the database:
            ClientCart clientCartBack = null;

            if (user != null)
            {
                clientCartBack    = Task.Run(() => GetAuthUserCartByDatabase()).Result;
                user.ClientCartId = clientCartBack.Id;
                _userCache.Set(user);
                this.CacheAuthUserCart(user.Id, clientCartBack);
            }
            else
            {
                clientCartBack = Task.Run(() => GetClientCartByDatabase(this.Cart.Id)).Result;
                this.CacheClientCart(clientCartBack);
            }

            // If user is anonymous - sets his cart id to the cookie:
            if (_httpContextAccessor.HttpContext.User.Identity.IsAuthenticated == false)
            {
                this.SetUserCartIdToCookie(this.Cart.Id);
            }
        }