PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/MvcMusicStore/Models/ShoppingCart.cs

#
C# | 200 lines | 140 code | 36 blank | 24 comment | 19 complexity | 55de6e607a285780de41a0e2aad4efd1 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcMusicStore.Models
  7. {
  8. public partial class ShoppingCart
  9. {
  10. MusicStoreEntities storeDB = new MusicStoreEntities();
  11. string ShoppingCartId { get; set; }
  12. public const string CartSessionKey = "CartId";
  13. public static ShoppingCart GetCart(HttpContextBase context)
  14. {
  15. var cart = new ShoppingCart();
  16. cart.ShoppingCartId = cart.GetCartId(context);
  17. return cart;
  18. }
  19. // Helper method to simplify shopping cart calls
  20. public static ShoppingCart GetCart(Controller controller)
  21. {
  22. return GetCart(controller.HttpContext);
  23. }
  24. public void AddToCart(Album album)
  25. {
  26. // Get the matching cart and album instances
  27. var cartItem = storeDB.Carts.SingleOrDefault(
  28. c => c.CartId == ShoppingCartId
  29. && c.AlbumId == album.AlbumId);
  30. if (cartItem == null)
  31. {
  32. // Create a new cart item if no cart item exists
  33. cartItem = new Cart
  34. {
  35. AlbumId = album.AlbumId,
  36. CartId = ShoppingCartId,
  37. Count = 1,
  38. DateCreated = DateTime.Now
  39. };
  40. storeDB.Carts.Add(cartItem);
  41. }
  42. else
  43. {
  44. // If the item does exist in the cart, then add one to the quantity
  45. cartItem.Count++;
  46. }
  47. // Save changes
  48. storeDB.SaveChanges();
  49. }
  50. public int RemoveFromCart(int id)
  51. {
  52. // Get the cart
  53. var cartItem = storeDB.Carts.Single(
  54. cart => cart.CartId == ShoppingCartId
  55. && cart.RecordId == id);
  56. int itemCount = 0;
  57. if (cartItem != null)
  58. {
  59. if (cartItem.Count > 1)
  60. {
  61. cartItem.Count--;
  62. itemCount = cartItem.Count;
  63. }
  64. else
  65. {
  66. storeDB.Carts.Remove(cartItem);
  67. }
  68. // Save changes
  69. storeDB.SaveChanges();
  70. }
  71. return itemCount;
  72. }
  73. public void EmptyCart()
  74. {
  75. var cartItems = storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId);
  76. foreach (var cartItem in cartItems)
  77. {
  78. storeDB.Carts.Remove(cartItem);
  79. }
  80. // Save changes
  81. storeDB.SaveChanges();
  82. }
  83. public List<Cart> GetCartItems()
  84. {
  85. return storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
  86. }
  87. public int GetCount()
  88. {
  89. // Get the count of each item in the cart and sum them up
  90. int? count = (from cartItems in storeDB.Carts
  91. where cartItems.CartId == ShoppingCartId
  92. select (int?)cartItems.Count).Sum();
  93. // Return 0 if all entries are null
  94. return count ?? 0;
  95. }
  96. public decimal GetTotal()
  97. {
  98. // Multiply album price by count of that album to get
  99. // the current price for each of those albums in the cart
  100. // sum all album price totals to get the cart total
  101. decimal? total = (from cartItems in storeDB.Carts
  102. where cartItems.CartId == ShoppingCartId
  103. select (int?)cartItems.Count * cartItems.Album.Price).Sum();
  104. return total ?? decimal.Zero;
  105. }
  106. public int CreateOrder(Order order)
  107. {
  108. decimal orderTotal = 0;
  109. var cartItems = GetCartItems();
  110. // Iterate over the items in the cart, adding the order details for each
  111. foreach (var item in cartItems)
  112. {
  113. var orderDetail = new OrderDetail
  114. {
  115. AlbumId = item.AlbumId,
  116. OrderId = order.OrderId,
  117. UnitPrice = item.Album.Price,
  118. Quantity = item.Count
  119. };
  120. // Set the order total of the shopping cart
  121. orderTotal += (item.Count * item.Album.Price);
  122. storeDB.OrderDetails.Add(orderDetail);
  123. }
  124. // Set the order's total to the orderTotal count
  125. order.Total = orderTotal;
  126. // Save the order
  127. storeDB.SaveChanges();
  128. // Empty the shopping cart
  129. EmptyCart();
  130. // Return the OrderId as the confirmation number
  131. return order.OrderId;
  132. }
  133. // We're using HttpContextBase to allow access to cookies.
  134. public string GetCartId(HttpContextBase context)
  135. {
  136. if (context.Session[CartSessionKey] == null)
  137. {
  138. if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
  139. {
  140. context.Session[CartSessionKey] = context.User.Identity.Name;
  141. }
  142. else
  143. {
  144. // Generate a new random GUID using System.Guid class
  145. Guid tempCartId = Guid.NewGuid();
  146. // Send tempCartId back to client as a cookie
  147. context.Session[CartSessionKey] = tempCartId.ToString();
  148. }
  149. }
  150. return context.Session[CartSessionKey].ToString();
  151. }
  152. // When a user has logged in, migrate their shopping cart to
  153. // be associated with their username
  154. public void MigrateCart(string userName)
  155. {
  156. var shoppingCart = storeDB.Carts.Where(c => c.CartId == ShoppingCartId);
  157. foreach (Cart item in shoppingCart)
  158. {
  159. item.CartId = userName;
  160. }
  161. storeDB.SaveChanges();
  162. }
  163. }
  164. }