public static ShoppingCart GetCart(HttpContextBase context)
        {
            MyLunchBoxDevelopmentEntities db = new MyLunchBoxDevelopmentEntities();
            var cartId = ShoppingCartHelper.GetCartId(context);
            try
            {
                var shoppingCart = db.ShoppingCarts.Single(i => i.ShoppingCartId == cartId);
                return shoppingCart;
            }
            catch (Exception ex)
            {
                var shoppingCart = new ShoppingCart();
                shoppingCart.CreatedAt = DateTime.Now;
                shoppingCart.LastUpdatedAt = DateTime.Now;
                shoppingCart.UserId = ShoppingCartHelper.GetCurrentUserId(context);
                db.ShoppingCarts.AddObject(shoppingCart);
                db.SaveChanges();

                HttpCookie cartCookie = new HttpCookie(CartSessionKey);
                cartId = shoppingCart.ShoppingCartId;
                cartCookie.Value = shoppingCart.ShoppingCartId.ToString();
                cartCookie.Expires = DateTime.Now.AddYears(2);
                context.Response.Cookies.Add(cartCookie);
                return shoppingCart;
            }
        }
        // We're using HttpContextBase to allow access to cookies.
        public static int GetCartId(HttpContextBase context)
        {
            int cartId;
            if (context.Request.Cookies[CartSessionKey] == null || context.Request.Cookies[CartSessionKey].Value == null)
            {
                MyLunchBoxDevelopmentEntities db = new MyLunchBoxDevelopmentEntities();
                var shoppingCart = new ShoppingCart();
                shoppingCart.CreatedAt = DateTime.Now;
                shoppingCart.LastUpdatedAt = DateTime.Now;
                shoppingCart.UserId = ShoppingCartHelper.GetCurrentUserId(context);
                db.ShoppingCarts.AddObject(shoppingCart);
                db.SaveChanges();

                HttpCookie cartCookie = new HttpCookie(CartSessionKey);
                cartId = shoppingCart.ShoppingCartId;
                cartCookie.Value = shoppingCart.ShoppingCartId.ToString();
                cartCookie.Expires = DateTime.Now.AddYears(2);
                context.Response.Cookies.Add(cartCookie);
                return cartId;
            }
            else
            {
                return int.Parse(context.Request.Cookies[CartSessionKey].Value.ToString());
            }
        }
Beispiel #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the ShoppingCarts EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToShoppingCarts(ShoppingCart shoppingCart)
 {
     base.AddObject("ShoppingCarts", shoppingCart);
 }
Beispiel #4
0
 /// <summary>
 /// Create a new ShoppingCart object.
 /// </summary>
 /// <param name="shoppingCartId">Initial value of the ShoppingCartId property.</param>
 /// <param name="createdAt">Initial value of the CreatedAt property.</param>
 /// <param name="lastUpdatedAt">Initial value of the LastUpdatedAt property.</param>
 public static ShoppingCart CreateShoppingCart(global::System.Int32 shoppingCartId, global::System.DateTime createdAt, global::System.DateTime lastUpdatedAt)
 {
     ShoppingCart shoppingCart = new ShoppingCart();
     shoppingCart.ShoppingCartId = shoppingCartId;
     shoppingCart.CreatedAt = createdAt;
     shoppingCart.LastUpdatedAt = lastUpdatedAt;
     return shoppingCart;
 }