Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/ShoppingCartDomain.cs Secret

Created Feb 10, 2021
Embed
What would you like to do?
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