8.2.1 Defining the Cart Entity; Listing 8-12 The Cart and CartLine Classes in the Cart.cs File
parent
62e5982ef8
commit
2b1fee6172
|
@ -0,0 +1,54 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SportsStore.Domain.Entities
|
||||||
|
{
|
||||||
|
class Cart
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -75,6 +75,7 @@
|
||||||
<Compile Include="Abstract\IProductRepository.cs" />
|
<Compile Include="Abstract\IProductRepository.cs" />
|
||||||
<Compile Include="Concrete\EFDbContext.cs" />
|
<Compile Include="Concrete\EFDbContext.cs" />
|
||||||
<Compile Include="Concrete\EFProductRepository.cs" />
|
<Compile Include="Concrete\EFProductRepository.cs" />
|
||||||
|
<Compile Include="Entities\Cart.cs" />
|
||||||
<Compile Include="Entities\Product.cs" />
|
<Compile Include="Entities\Product.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue