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; } }
public ActionResult GetDeliveryLocations(int universityId) { var db = new MyLunchBoxDevelopmentEntities(); var deliveries = db.University_Delivery.Where(i => i.UniversityId == universityId).Select(i => new { i.UniversityDeliveryId, i.Location.BusinessName, i.DeliveryTime }).AsEnumerable() .Select(i => new SelectListItem() { Text = i.BusinessName + " " + i.DeliveryTime.ToString("hh:mm tt"), Value = i.UniversityDeliveryId.ToString() }); return PartialView(deliveries); }
public bool SendOrderConfirmationMail(int orderId) { MyLunchBoxDevelopmentEntities db = new MyLunchBoxDevelopmentEntities(); var myLunchBoxMailer = new MyLunchBoxMailer(); var client = new MyLunchBoxSMTPClient(); var order = db.Orders.Single(i => i.OrderId == orderId); if (order != null) { decimal subtotal, discount, paid, cash; subtotal = order.Gross; discount = order.Savings; if (order.PaymentStatus == PaymentStatusLevel.WaitingForPayment.ToString()) { paid = 0; cash = order.FinalAmount; } else { paid = order.FinalAmount; cash = 0; } var orderSummary = string.Format(@"<strong>Sub Total</strong>={0}<br /> <strong>Discount</strong>={1}<br /> <strong>Paid</strong>={2}<br /> <strong>Cash</strong>=<span style='font-size:1.5em;color:#c84c0b'>{3}</span>", order.Gross, order.Savings, paid, cash); var orderItems = "<tr>" + string.Join("<tr/><tr>", order.OrderItems.Select(i => string.Format("<td>{0}</td><td>{1}</td><td>{2}</td>", i.Item.ItemDescription, i.Quantity, i.LineItemCost))) + "</tr>"; var deliveryLocation = order.DeliveryLocation.BusinessName; var deliveryTime = ""; if (order.DeliveryTime.HasValue) { deliveryTime = order.DeliveryTime.Value.ToString("yyyy-MM-dd HH:mm tt"); } try { myLunchBoxMailer.SendTemplateMail( new Dictionary<string, string>() { {"FirstName", order.ReceiverFirstName}, {"LastName", order.ReceiverLastName}, {"ReceiverPhoneNumber", order.ReceiverPhoneNumber}, {"DeliveryLocation", deliveryLocation}, {"DeliveryTime", deliveryTime}, {"OrderId", order.OrderId.ToString()}, {"OrderItems", orderItems}, {"OrderSummary", orderSummary} }, "orderConfirmation", order.PayerEmail, ConfigurationManager.AppSettings["SupportEmail"], ConfigurationManager.AppSettings["SupportEmail"], "", client); } catch { return false; } } return false; }
// 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); }
// // GET: /Account/Register public ActionResult Register(string returnUrl) { var currentUniversity = MyLunchBox.Models.LocationHelper.GetSelectedUniversity(HttpContext); var deliveryLocation = MyLunchBox.Models.LocationHelper.GetDeliveryLocation(HttpContext); var db = new MyLunchBox.Models.MyLunchBoxDevelopmentEntities(); 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 == (currentUniversity != null ? currentUniversity.UniversityId : -1)) }); int currentSelectedUniversityId = currentUniversity == null ? Convert.ToInt32(universities.First().Value) : currentUniversity.UniversityId; var universityDeliveries = db.University_Delivery.Where(i => i.UniversityId == currentSelectedUniversityId).Select(i => new { i.UniversityDeliveryId, i.Location.LocationName }).AsEnumerable().Select(i => new SelectListItem() { Text = i.LocationName, Value = i.UniversityDeliveryId.ToString(), Selected = (i.UniversityDeliveryId == (deliveryLocation != null ? deliveryLocation.UniversityDeliveryId : -1)) }); return View(new RegisterModel() { Universities = universities, UniversityDeliveries = universityDeliveries } ); }
public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.Email, model.Password)) { FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe); // assign selected university and selected location by user profile var db = new MyLunchBoxDevelopmentEntities(); var userId = MembershipHelper.GetUserIdByEmail(model.Email); if(userId.HasValue) { try { var userDetails = db.UserDetails.Single(i=>i.UserId == userId); if(userDetails.UniversityId.HasValue && LocationHelper.GetSelectedUniversity(HttpContext) == null) { LocationHelper.SetSelectedUniversity(HttpContext, userDetails.University); } if (userDetails.UniversityDeliveryId.HasValue && LocationHelper.GetDeliveryLocation(HttpContext) == null) { LocationHelper.SetDeliveryLocation(HttpContext, userDetails.University_Delivery); } }catch {} } // redirect to returnurl if necessary if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(model); }