Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created September 13, 2023 21:34
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/d7f88e69dc0b6318079aa8b89edb14f5 to your computer and use it in GitHub Desktop.
Save dcomartin/d7f88e69dc0b6318079aa8b89edb14f5 to your computer and use it in GitHub Desktop.
public class Basket : BaseEntity, IAggregateRoot
{
public string BuyerId { get; private set; }
private readonly List<BasketItem> _items = new List<BasketItem>();
public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly();
public int TotalItems => _items.Sum(i => i.Quantity);
public Basket(string buyerId)
{
BuyerId = buyerId;
}
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1)
{
if (!Items.Any(i => i.CatalogItemId == catalogItemId))
{
_items.Add(new BasketItem(catalogItemId, quantity, unitPrice));
return;
}
var existingItem = Items.First(i => i.CatalogItemId == catalogItemId);
existingItem.AddQuantity(quantity);
}
public void RemoveEmptyItems()
{
_items.RemoveAll(i => i.Quantity == 0);
}
public void SetNewBuyerId(string buyerId)
{
BuyerId = buyerId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment