Guest User

Untitled

a guest
Mar 28th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace AdmissionsRepresentatives.Models
  8. {
  9.     public class ShoppingCart
  10.     {
  11.         string ShoppingCartId { get; set; }
  12.         AdmissionsStoreEntities storeDB = new AdmissionsStoreEntities();
  13.         public const string CartSessionKey = "CartId";
  14.  
  15.         public static ShoppingCart GetCart(HttpContextBase context)
  16.         {
  17.             var cart = new ShoppingCart();
  18.             cart.ShoppingCartId = cart.GetCartId(context);
  19.             return cart;
  20.         }
  21.  
  22.         // Helper method to simplify shopping cart calls
  23.         public static ShoppingCart GetCart(Controller controller)
  24.         {
  25.             return GetCart(controller.HttpContext);
  26.         }
  27.  
  28.         public void AddToCart(Product product)
  29.         {
  30.             // Get the matching cart and product instances
  31.             var cartItem = storeDB.Carts.SingleOrDefault(
  32.                 c => c.CartId == ShoppingCartId
  33.                 && c.ProductId == product.ProductId);
  34.  
  35.             if (cartItem == null)
  36.             {
  37.                 // Create a new cart item if no cart item exists
  38.                 cartItem = new Cart
  39.                 {
  40.                     ProductId = product.ProductId,
  41.                     Product = product,
  42.                     CartId = ShoppingCartId,
  43.                     Count = 1,
  44.                     DateCreated = DateTime.Now
  45.                 };
  46.  
  47.                 storeDB.Carts.Add(cartItem);
  48.             }
  49.             else
  50.             {
  51.                 // If the item does exist in the cart, then add one to the quantity.
  52.                 cartItem.Count++;
  53.             }
  54.  
  55.             System.Diagnostics.Debug.WriteLine(cartItem.ProductId);
  56.             // Save Changes
  57.             storeDB.SaveChanges();
  58.  
  59.             System.Diagnostics.Debug.WriteLine(cartItem.ProductId);
  60.         }
  61.  
  62.         public int RemoveFromCart(int id)
  63.         {
  64.             // Get the cart
  65.             var cartItem = storeDB.Carts.Single(
  66.                 cart => cart.CartId == ShoppingCartId
  67.                 && cart.RecordId == id);
  68.  
  69.             int itemCount = 0;
  70.  
  71.             if (cartItem != null)
  72.             {
  73.                 if (cartItem.Count > 1)
  74.                 {
  75.                     cartItem.Count--;
  76.                     itemCount = cartItem.Count;
  77.                 }
  78.                 else
  79.                 {
  80.                     storeDB.Carts.Remove(cartItem);
  81.                 }
  82.  
  83.                 //Save changes
  84.                 storeDB.SaveChanges();
  85.             }
  86.  
  87.             return itemCount;
  88.         }
  89.  
  90.         public void EmptyCart()
  91.         {
  92.             var cartItems = storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId);
  93.  
  94.             foreach (var cartItem in cartItems)
  95.             {
  96.                 storeDB.Carts.Remove(cartItem);
  97.             }
  98.  
  99.             // Save changes
  100.             storeDB.SaveChanges();
  101.         }
  102.  
  103.         public List<Cart> GetCartItems()
  104.         {
  105.             return storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
  106.         }
  107.  
  108.         public int GetCount()
  109.         {
  110.             // Get the count of each item in the cart and sum them up
  111.             int? count = (from cartItems in storeDB.Carts
  112.                           where cartItems.CartId == ShoppingCartId
  113.                           select (int?)cartItems.Count).Sum();
  114.  
  115.             // Return 0 if all entries are null
  116.             return count ?? 0;
  117.         }
  118.  
  119.         public int CreateOrder(Order order)
  120.         {
  121.             var cartItems = GetCartItems();
  122.  
  123.             // Iterate over the items in the car, adding the order details for each
  124.             foreach (var item in cartItems)
  125.             {
  126.                 var orderDetail = new OrderDetail
  127.                 {
  128.                     ProductId = item.ProductId,
  129.                     OrderId = order.OrderId,
  130.                     Quantity = item.Count
  131.                 };
  132.  
  133.                 storeDB.OrderDetails.Add(orderDetail);
  134.             }
  135.  
  136.             storeDB.SaveChanges();
  137.             EmptyCart();
  138.  
  139.             return order.OrderId;
  140.         }
  141.  
  142.         public string GetCartId(HttpContextBase context)
  143.         {
  144.             if (context.Session[CartSessionKey] == null)
  145.             {
  146.                 if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
  147.                 {
  148.                     context.Session[CartSessionKey] = context.User.Identity.Name;
  149.                 }
  150.                 else
  151.                 {
  152.                     Guid tempCartId = Guid.NewGuid();
  153.  
  154.                     context.Session[CartSessionKey] = tempCartId.ToString();
  155.                 }
  156.             }
  157.             return context.Session[CartSessionKey].ToString();
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment