Exemple #1
0
        public List <CartItem> AddCartItemForUser(User user, long productId)
        {
            if (_productRepository.FindById(productId) == null)
            {
                throw new KeyNotFoundException();
            }

            var cartItem = _cartItemRepository.FindById(user.UserId, productId);

            if (cartItem == null)
            {
                _cartItemRepository.Create(new CartItem
                {
                    UserId    = user.UserId,
                    ProductId = productId,
                    Quantity  = 1
                });
            }
            else // If it already exists, we increment quantity
            {
                cartItem.Quantity++;
                _cartItemRepository.Update(cartItem);
            }

            _cartItemRepository.SaveChanges();
            return(GetAllItemsForUser(user));
        }