comparison Demo/Asp.Net/BusinessLogic/Cart.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3
4 namespace PetShop.BusinessLogic
5 {
6 using ObjectModel;
7
8 [Serializable]
9 public class Cart
10 {
11 /// <summary>
12 /// Calculate the total for all the cartItems in the Cart
13 /// </summary>
14 public decimal Total
15 {
16 get
17 {
18 decimal total = 0;
19
20 foreach (CartItem item in _items.Values)
21 total += item.Subtotal;
22
23 return total;
24 }
25 }
26
27 /// <summary>
28 /// Update the quantity for item that exists in the cart
29 /// </summary>
30 /// <param name="itemId">Item Id</param>
31 /// <param name="qty">Quantity</param>
32 public void SetQuantity(string itemId, int qty)
33 {
34 _items[itemId].Quantity = qty;
35 }
36
37 /// <summary>
38 /// Return the number of unique items in cart
39 /// </summary>
40 public int Count
41 {
42 get { return _items.Count; }
43 }
44
45 /// <summary>
46 /// Add an item to the cart.
47 /// When ItemId to be added has already existed, this method will update the quantity instead.
48 /// </summary>
49 /// <param name="itemId">Item Id of item to add</param>
50 public void Add(string itemId)
51 {
52 CartItem cartItem;
53
54 if (!_items.TryGetValue(itemId, out cartItem))
55 {
56 Item item = new ProductManager().GetItem(itemId);
57
58 if (item != null)
59 {
60 cartItem = new CartItem();
61
62 cartItem.ItemID = itemId;
63 cartItem.Name = item.ProductName;
64 cartItem.Price = (decimal)item.Price;
65 cartItem.Type = item.Name;
66 cartItem.CategoryID = item.CategoryID;
67 cartItem.ProductID = item.ProductID;
68
69 _items.Add(itemId, cartItem);
70 }
71 }
72
73 cartItem.Quantity++;
74 }
75
76 /// <summary>
77 /// Add an item to the cart.
78 /// When ItemId to be added has already existed, this method will update the quantity instead.
79 /// </summary>
80 /// <param name="item">Item to add</param>
81 public void Add(CartItem item)
82 {
83 CartItem cartItem;
84
85 if (!_items.TryGetValue(item.ItemID, out cartItem))
86 _items.Add(item.ItemID, item);
87 else
88 cartItem.Quantity += item.Quantity;
89 }
90
91 /// <summary>
92 /// Remove item from the cart based on <paramref name="itemId"/>
93 /// </summary>
94 /// <param name="itemId">ItemId of item to remove</param>
95 public void Remove(string itemId)
96 {
97 _items.Remove(itemId);
98 }
99
100 // Internal storage for a cart
101 private Dictionary<string, CartItem> _items = new Dictionary<string, CartItem>();
102
103 /// <summary>
104 /// Returns all items in the cart. Useful for looping through the cart.
105 /// </summary>
106 /// <returns>Collection of CartItemInfo</returns>
107 public ICollection<CartItem> Items
108 {
109 get { return _items.Values; }
110 }
111
112 /// <summary>
113 /// Method to convert all cart items to order line items
114 /// </summary>
115 /// <returns>A new array of order line items</returns>
116 public OrderLineItem[] GetOrderLineItems()
117 {
118 OrderLineItem[] items = new OrderLineItem[_items.Count];
119
120 int lineNum = 0;
121
122 foreach (CartItem item in _items.Values)
123 {
124 OrderLineItem line = new OrderLineItem();
125
126 items[lineNum] = line;
127
128 line.ItemID = item.ItemID;
129 line.Line = ++lineNum;
130 line.Quantity = item.Quantity;
131 line.Price = item.Price;
132 }
133
134 return items;
135 }
136
137 /// <summary>
138 /// Clear the cart
139 /// </summary>
140 public void Clear()
141 {
142 _items.Clear();
143 }
144 }
145 }