Basket persistence (#41)

* Renamed Cart to Basket throughout
* Implemented cookie-based anonymous basket handling and transfer to user upon login. Still need to implement transfer upon registration.
This commit is contained in:
Steve Smith
2017-08-22 13:51:08 -04:00
committed by GitHub
parent 3a95375ae7
commit ecb4889dd3
10 changed files with 173 additions and 118 deletions

View File

@@ -6,13 +6,14 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities
public class Basket : BaseEntity
{
public string BuyerId { get; set; }
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
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()
_items.Add(new BasketItem()
{
CatalogItemId = catalogItemId,
Quantity = quantity,

View File

@@ -13,10 +13,18 @@ namespace ApplicationCore.Specifications
BasketId = basketId;
AddInclude(b => b.Items);
}
public BasketWithItemsSpecification(string buyerId)
{
BuyerId = buyerId;
AddInclude(b => b.Items);
}
public int BasketId { get; }
public int? BasketId { get; }
public string BuyerId { get; }
public Expression<Func<Basket, bool>> Criteria => b => b.Id == BasketId;
public Expression<Func<Basket, bool>> Criteria => b =>
(BasketId.HasValue && b.Id == BasketId.Value)
|| (BuyerId != null && b.BuyerId == BuyerId);
public List<Expression<Func<Basket, object>>> Includes { get; } = new List<Expression<Func<Basket, object>>>();