public void AddToCart(int userId, int productId, int quantity) { var cart = GetCartByUserId(userId); if (cart != null) { var index = cart.CartItems.FindIndex(c => c.ProductId == productId); if (index < 0) { cart.CartItems.Add(new CartItem { ProductId = productId, Quantity = quantity, CartId = cart.Id }); } else { cart.CartItems[index].Quantity += quantity; } _cartDal.UpdateObj(cart); } }