You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
927 B
28 lines
927 B
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Microsoft.eShopWeb.ApplicationCore.Entities
|
|
{
|
|
public class Basket : BaseEntity
|
|
{
|
|
public string BuyerId { get; set; }
|
|
private readonly List<BasketItem> _items = new List<BasketItem>();
|
|
public IEnumerable<BasketItem> Items => _items.ToList();
|
|
|
|
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1)
|
|
{
|
|
if (!Items.Any(i => i.CatalogItemId == catalogItemId))
|
|
{
|
|
_items.Add(new BasketItem()
|
|
{
|
|
CatalogItemId = catalogItemId,
|
|
Quantity = quantity,
|
|
UnitPrice = unitPrice
|
|
});
|
|
return;
|
|
}
|
|
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
|
|
existingItem.Quantity += quantity;
|
|
}
|
|
}
|
|
}
|
|
|