2021-09-09 00:02:29 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SportsStore.Domain.Entities
|
|
|
|
|
{
|
2021-09-13 00:11:39 +10:00
|
|
|
|
public class Cart
|
2021-09-09 00:02:29 +10:00
|
|
|
|
{
|
|
|
|
|
private List<CartLine> lineCollection = new List<CartLine>();
|
|
|
|
|
|
|
|
|
|
public void AddItem(Product product, int quantity)
|
|
|
|
|
{
|
|
|
|
|
CartLine line = lineCollection
|
|
|
|
|
.Where(p => p.Product.ProductID == product.ProductID)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (line == null)
|
|
|
|
|
{
|
|
|
|
|
lineCollection.Add(new CartLine
|
|
|
|
|
{
|
|
|
|
|
Product = product,
|
|
|
|
|
Quantity = quantity
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
line.Quantity += quantity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveLine(Product product)
|
|
|
|
|
{
|
|
|
|
|
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 00:11:39 +10:00
|
|
|
|
public decimal ComputeTotalValue()
|
|
|
|
|
{
|
|
|
|
|
return lineCollection.Sum(e => e.Product.Price * e.Quantity);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 00:02:29 +10:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
lineCollection.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<CartLine> Lines
|
|
|
|
|
{
|
|
|
|
|
get { return lineCollection; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CartLine
|
|
|
|
|
{
|
|
|
|
|
public Product Product { get; set; }
|
|
|
|
|
public int Quantity { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|