Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace AdmissionsRepresentatives.Models
- {
- public class ShoppingCart
- {
- string ShoppingCartId { get; set; }
- AdmissionsStoreEntities storeDB = new AdmissionsStoreEntities();
- public const string CartSessionKey = "CartId";
- public static ShoppingCart GetCart(HttpContextBase context)
- {
- var cart = new ShoppingCart();
- cart.ShoppingCartId = cart.GetCartId(context);
- return cart;
- }
- // Helper method to simplify shopping cart calls
- public static ShoppingCart GetCart(Controller controller)
- {
- return GetCart(controller.HttpContext);
- }
- public void AddToCart(Product product)
- {
- // Get the matching cart and product instances
- var cartItem = storeDB.Carts.SingleOrDefault(
- c => c.CartId == ShoppingCartId
- && c.ProductId == product.ProductId);
- if (cartItem == null)
- {
- // Create a new cart item if no cart item exists
- cartItem = new Cart
- {
- ProductId = product.ProductId,
- Product = product,
- CartId = ShoppingCartId,
- Count = 1,
- DateCreated = DateTime.Now
- };
- storeDB.Carts.Add(cartItem);
- }
- else
- {
- // If the item does exist in the cart, then add one to the quantity.
- cartItem.Count++;
- }
- System.Diagnostics.Debug.WriteLine(cartItem.ProductId);
- // Save Changes
- storeDB.SaveChanges();
- System.Diagnostics.Debug.WriteLine(cartItem.ProductId);
- }
- public int RemoveFromCart(int id)
- {
- // Get the cart
- var cartItem = storeDB.Carts.Single(
- cart => cart.CartId == ShoppingCartId
- && cart.RecordId == id);
- int itemCount = 0;
- if (cartItem != null)
- {
- if (cartItem.Count > 1)
- {
- cartItem.Count--;
- itemCount = cartItem.Count;
- }
- else
- {
- storeDB.Carts.Remove(cartItem);
- }
- //Save changes
- storeDB.SaveChanges();
- }
- return itemCount;
- }
- public void EmptyCart()
- {
- var cartItems = storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId);
- foreach (var cartItem in cartItems)
- {
- storeDB.Carts.Remove(cartItem);
- }
- // Save changes
- storeDB.SaveChanges();
- }
- public List<Cart> GetCartItems()
- {
- return storeDB.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
- }
- public int GetCount()
- {
- // Get the count of each item in the cart and sum them up
- int? count = (from cartItems in storeDB.Carts
- where cartItems.CartId == ShoppingCartId
- select (int?)cartItems.Count).Sum();
- // Return 0 if all entries are null
- return count ?? 0;
- }
- public int CreateOrder(Order order)
- {
- var cartItems = GetCartItems();
- // Iterate over the items in the car, adding the order details for each
- foreach (var item in cartItems)
- {
- var orderDetail = new OrderDetail
- {
- ProductId = item.ProductId,
- OrderId = order.OrderId,
- Quantity = item.Count
- };
- storeDB.OrderDetails.Add(orderDetail);
- }
- storeDB.SaveChanges();
- EmptyCart();
- return order.OrderId;
- }
- public string GetCartId(HttpContextBase context)
- {
- if (context.Session[CartSessionKey] == null)
- {
- if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
- {
- context.Session[CartSessionKey] = context.User.Identity.Name;
- }
- else
- {
- Guid tempCartId = Guid.NewGuid();
- context.Session[CartSessionKey] = tempCartId.ToString();
- }
- }
- return context.Session[CartSessionKey].ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment