This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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