ShopPlatform.Web 1.0.0.0
농산물, 소프트웨어 라이선스와 개발 용역을 직접 판매하는 CodeMaru 직영 쇼핑몰입니다.
로딩중...
검색중...
일치하는것 없음
CartService.cs
이 파일의 문서화 페이지로 가기
2
4
13public sealed class CartService
14{
23 private readonly List<CartItem> _items = new();
24
33 public IReadOnlyList<CartItem> Items => _items;
42 public decimal Total => _items.Sum(i => i.Subtotal);
51 public int Count => _items.Sum(i => i.Quantity);
52
77 public void Add(Product product, int quantity = 1)
78 {
79 var existing = _items.FirstOrDefault(i => i.ProductId == product.Id);
80 if (existing != null)
81 existing.Quantity += quantity;
82 else
83 _items.Add(new CartItem
84 {
85 ProductId = product.Id,
86 ProductName = product.Name,
87 UnitPrice = product.Price,
88 Quantity = quantity
89 });
90 }
91
108 public void Remove(int productId)
109 => _items.RemoveAll(i => i.ProductId == productId);
110
135 public void UpdateQuantity(int productId, int quantity)
136 {
137 var item = _items.FirstOrDefault(i => i.ProductId == productId);
138 if (item == null) return;
139 if (quantity <= 0) Remove(productId);
140 else item.Quantity = quantity;
141 }
142
151 public void Clear() => _items.Clear();
152}
readonly List< CartItem > _items
void UpdateQuantity(int productId, int quantity)
void Add(Product product, int quantity=1)
IReadOnlyList< CartItem > Items