Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 10, 2021 22:46
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/5815c12fd721dc69f5e0493f2031d07f to your computer and use it in GitHub Desktop.
Save dcomartin/5815c12fd721dc69f5e0493f2031d07f to your computer and use it in GitHub Desktop.
public class ShoppingCartEventDomain
{
private List<object> _events = new List<object>();
private readonly ShoppingCart _shoppingCart;
public ShoppingCartEventDomain(ShoppingCart shoppingCart)
{
_shoppingCart = shoppingCart;
}
public List<object> GetEvents()
{
return _events;
}
public void AddItem(Guid productId, int quantity, decimal price)
{
var existingItem = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId);
if (existingItem != null)
{
_events.Add(new QuantityIncremented
{
ShoppingCartId = _shoppingCart.ShoppingCartId,
ProductId = productId,
Quantity = quantity
});
}
else
{
_shoppingCart.Items.Add(new ShoppingCartItem(_shoppingCart.ShoppingCartId, productId));
_events.Add(new ItemAdded
{
ShoppingCartId = _shoppingCart.ShoppingCartId,
ProductId = productId,
Quantity = quantity,
Price = price
});
}
}
public void RemoveItem(Guid productId)
{
var product = _shoppingCart.Items.SingleOrDefault(x => x.ProductId == productId);
if (product != null)
{
_events.Add(new ItemRemoved
{
ShoppingCartId = _shoppingCart.ShoppingCartId,
ProductId = productId,
});
}
}
}
public class ItemAdded
{
public Guid ShoppingCartId { get; set; }
public Guid ProductId { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
public class ItemRemoved
{
public Guid ShoppingCartId { get; set; }
public Guid ProductId { get; set; }
}
public class QuantityIncremented
{
public Guid ShoppingCartId { get; set; }
public Guid ProductId { get; set; }
public int Quantity { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment