committed by
GitHub
11 changed files with 56 additions and 39 deletions
@ -1,39 +1,39 @@ |
|||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Linq; |
using System.Linq; |
||||
|
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate |
namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate |
||||
{ |
{ |
||||
public class Basket : BaseEntity, IAggregateRoot |
public class Basket : BaseEntity, IAggregateRoot |
||||
{ |
{ |
||||
public string BuyerId { get; private set; } |
public string BuyerId { get; private set; } |
||||
private readonly List<BasketItem> _items = new List<BasketItem>(); |
private readonly List<BasketItem> _items = new List<BasketItem>(); |
||||
public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly(); |
public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly(); |
||||
|
|
||||
public Basket(string buyerId) |
public Basket(string buyerId) |
||||
{ |
{ |
||||
BuyerId = buyerId; |
BuyerId = buyerId; |
||||
} |
} |
||||
|
|
||||
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) |
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) |
||||
{ |
{ |
||||
if (!Items.Any(i => i.CatalogItemId == catalogItemId)) |
if (!Items.Any(i => i.CatalogItemId == catalogItemId)) |
||||
{ |
{ |
||||
_items.Add(new BasketItem(catalogItemId, quantity, unitPrice)); |
_items.Add(new BasketItem(catalogItemId, quantity, unitPrice)); |
||||
return; |
return; |
||||
} |
} |
||||
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); |
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); |
||||
existingItem.AddQuantity(quantity); |
existingItem.AddQuantity(quantity); |
||||
} |
} |
||||
|
|
||||
public void RemoveEmptyItems() |
public void RemoveEmptyItems() |
||||
{ |
{ |
||||
_items.RemoveAll(i => i.Quantity == 0); |
_items.RemoveAll(i => i.Quantity == 0); |
||||
} |
} |
||||
|
|
||||
public void SetNewBuyerId(string buyerId) |
public void SetNewBuyerId(string buyerId) |
||||
{ |
{ |
||||
BuyerId = buyerId; |
BuyerId = buyerId; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue