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()); } }
// When a user has logged in, migrate their shopping cart to // be associated with their email public static void MergeCart(HttpContextBase context, string email) { MyLunchBoxDevelopmentEntities db = new MyLunchBoxDevelopmentEntities(); var userId = MembershipHelper.GetUserIdByEmail(email); if (userId.HasValue) { var shoppingCart = db.ShoppingCarts.Single( c => c.UserId == userId.Value ); var unmergedCart = GetCart(context); if(shoppingCart != null) { foreach(var shoppingCartItem in shoppingCart.ShoppingCartItems) { unmergedCart.ShoppingCartItems.Add(shoppingCartItem); } db.ShoppingCarts.DeleteObject(shoppingCart); } db.ShoppingCarts.AddObject(unmergedCart); db.SaveChanges(); } }
public ActionResult Register(RegisterModel model) { var db = new MyLunchBoxDevelopmentEntities(); if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; var user = Membership.CreateUser(model.Email, model.Password, model.Email, model.PasswordQuestion, model.PasswordAnswer, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { var userId = MembershipHelper.GetUserIdByEmail(model.Email); if (userId.HasValue) { var details = db.UserDetails.Single(i => i.UserId == userId.Value); details.FirstName = model.FirstName; details.LastName = model.LastName; details.UniversityId = model.UniversityId; details.UniversityDeliveryId = model.UniversityDeliveryId; db.UserDetails.ApplyCurrentValues(details); db.SaveChanges(); } Roles.AddUserToRole(model.Email, MyLunchBoxRoleType.Customer.ToString()); FormsAuthentication.SetAuthCookie(model.Email, false /* createPersistentCookie */); if (!string.IsNullOrEmpty(Request.QueryString["returnUrl"])) { return Redirect(Request.QueryString["returnUrl"]); } return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, redisplay form var universities = db.Universities.Select(i => new { i.UniversityName, i.UniversityId }).AsEnumerable().Select(i => new SelectListItem() { Text = i.UniversityName, Value = i.UniversityId.ToString(), Selected = (i.UniversityId == model.UniversityId) }); var universityDeliveries = db.University_Delivery.Where(i => i.UniversityId == model.UniversityId).Select(i => new { i.UniversityDeliveryId, i.Location.LocationName }).AsEnumerable().Select(i => new SelectListItem() { Text = i.LocationName, Value = i.UniversityDeliveryId.ToString(), Selected = (i.UniversityDeliveryId == model.UniversityDeliveryId) }); model.Universities = universities; model.UniversityDeliveries = universityDeliveries; return View(model); }