Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/Events.cs Secret

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