Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 10, 2021 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/62cacd3454252edd4805a8a890514764 to your computer and use it in GitHub Desktop.
Save dcomartin/62cacd3454252edd4805a8a890514764 to your computer and use it in GitHub Desktop.
public class ShoppingCartDomain
{
private readonly ShoppingCart _shoppingCart;
public ShoppingCartDomain(ShoppingCart shoppingCart)
{
_shoppingCart = shoppingCart;
}
public void AddItem(Guid productId, int quantity, decimal price)
{
var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId);
if (existingItem != null)
{
existingItem.Quantity += quantity;
}
else
{
_shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId, quantity, price));
}
}
public void RemoveItem(Guid productId)
{
var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId);
if (product != null)
{
_shoppingCart.Items.Remove(product);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment