public ActionResult Index() { // Iterate through all the order details in the DB IList <OrderDetailModel> OrderList = market.OrderDetails.ToList(); foreach (OrderDetailModel order in OrderList) { if (order.Status == null) { market.OrderDetails.Remove(order); market.SaveChanges(); log.Info("Order for " + order.ItemName + " removed (ID: " + order.OrderDetailId + ")"); continue; } if (order.Status == (int)OrderStatus.ORDER_TERMINATED || order.Status == (int)OrderStatus.ORDER_DISPUTE || order.Status == (int)OrderStatus.ORDER_CLOSED_HAPPY || order.Status == (int)OrderStatus.ORDER_CLOSED_UNHAPPY) { continue; } else { switch (order.Status) { case (int)OrderStatus.ORDER_TENTATIVE: // Check if today is pickup date. If so send reminder if (order.PickupDate.Date.Equals(DateTime.Today.Date)) { // Send reminder email with links to complete the order SendNotificationEmail(EmailType.PICKUP_REMINDER, true, order); log.Info("Pickup reminder sent to owner for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); SendNotificationEmail(EmailType.PICKUP_REMINDER, false, order); log.Info("Pickup reminder sent to borrower for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); break; } if ((DateTime.Today.Date - order.PickupDate.Date).Days > 0) { // Mark the order as Late order.Status = (int)OrderStatus.ORDER_LATE; market.Entry(order).State = System.Data.EntityState.Modified; market.SaveChanges(); log.Info("Order for " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); break; } break; case (int)OrderStatus.ORDER_LATE: if ((DateTime.Today.Date - order.PickupDate.Date).Days > 1) { // Mark the order as Terminated order.Status = (int)OrderStatus.ORDER_TERMINATED; market.Entry(order).State = System.Data.EntityState.Modified; market.SaveChanges(); // Send email to owner and renter informing them of the news SendNotificationEmail(EmailType.ORDER_TERMINATED, true, order); log.Info("Order Terminated Notification sent to owner for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); SendNotificationEmail(EmailType.ORDER_TERMINATED, false, order); log.Info("Order Terminated Notification sent to borrower for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); // Todo: Refund the renter for the full amount break; } break; case (int)OrderStatus.ORDER_COMPLETE: if (order.DrofoffDate.Date.Equals(DateTime.Today.Date)) { // Todo: Send reminder email with links to Owner to close the order and indicate receipt of items SendNotificationEmail(EmailType.RETURN_REMINDER, true, order); log.Info("Return reminder sent to owner for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); SendNotificationEmail(EmailType.RETURN_REMINDER, false, order); log.Info("Return reminder sent to borrower for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); break; } if ((DateTime.Today.Date - order.DrofoffDate.Date).Days > 0) { // Mark the order as Delayed_Return order.Status = (int)OrderStatus.ORDER_DELAYED_RETURN; market.Entry(order).State = System.Data.EntityState.Modified; market.SaveChanges(); log.Info("Order for " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); break; } break; case (int)OrderStatus.ORDER_DELAYED_RETURN: if ((DateTime.Today.Date - order.DrofoffDate.Date).Days > 1) { // Mark the order as Close_Happy. No action from Owner means the transaction went fine. order.Status = (int)OrderStatus.ORDER_CLOSED_HAPPY; market.Entry(order).State = System.Data.EntityState.Modified; market.SaveChanges(); log.Info("Order for " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); // Send thank you email to owner and renter informing them that order was successful. SendNotificationEmail(EmailType.ORDER_CLOSED, true, order); log.Info("Order closed notification sent to owner for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); SendNotificationEmail(EmailType.ORDER_CLOSED, false, order); log.Info("Order closed notification sent to borrower for ordered item " + order.ItemName + " (ID: " + order.OrderDetailId + "; Status: " + order.Status.ToString() + ")"); break; } break; default: break; } } } return(RedirectToAction("Index", "Home")); }
public ActionResult Edit(UserModel usermodel) { try { var user = db.Users.SingleOrDefault(u => u.Email == User.Identity.Name); ViewBag.RegionId = new SelectList(db.Regions, "Id", "Name", usermodel.RegionId); ViewBag.Name = usermodel.FirstName + " " + usermodel.LastName; ViewBag.UserEarnings = user.Earnings; ViewBag.ProfilePicture = (user.ProfilePicture != null); ViewBag.isFacebookUser = user.isFacebookUser; if (user.isFacebookUser) { ViewBag.FacebookId = user.FacebookProfile.Id; } foreach (string inputTagName in Request.Files) { HttpPostedFileBase file = Request.Files[inputTagName]; if (file != null && file.ContentLength > 0) { using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); if (user.UserImageFileModelId != null) { var profilePic = db.Files.SingleOrDefault(f => f.UserModelEmail == User.Identity.Name); profilePic.Name = Path.GetFileName(file.FileName); profilePic.Contents = ms.GetBuffer(); profilePic.ContentType = file.ContentType; db.Entry(profilePic).State = EntityState.Modified; } else { string id = Guid.NewGuid().ToString(); db.Files.Add(new UserImageFileModel { Id = id, Name = Path.GetFileName(file.FileName), Contents = ms.GetBuffer(), UserModelEmail = user.Email, ContentType = file.ContentType }); usermodel.UserImageFileModelId = id; } } } /* This is redundant logic. * else * { * usermodel.UserImageFileModelId = user.UserImageFileModelId; * }*/ } if (ModelState.IsValid) { usermodel.Email = user.Email; usermodel.isFacebookUser = user.isFacebookUser; usermodel.FacebookUserId = user.FacebookUserId; usermodel.Earnings = user.Earnings; if (user.UserImageFileModelId != null) { usermodel.UserImageFileModelId = user.UserImageFileModelId; } usermodel.CountryId = 1; db.Entry(user).State = EntityState.Detached; db.SaveChanges(); db.Users.Attach(usermodel); db.Entry(usermodel).State = EntityState.Modified; db.SaveChanges(); Session["USER_F_NAME"] = usermodel.FirstName; return(RedirectToAction("Index")); } return(View(usermodel)); } catch (Exception ex) { ModelState.AddModelError("", "An error has occured."); ViewBag.RegionId = new SelectList(db.Regions, "Id", "Name", usermodel.RegionId); ViewBag.UserEarnings = usermodel.Earnings; //do something return(View(usermodel)); } }
public ActionResult ProcessSecurityCode(string order, string item, ProcessSecurityCodeViewModel UserCode) { var getOrder = market.Orders.SingleOrDefault(o => o.OrderId == order); if (getOrder == null) { ErrorMessage.ErrorCode = ErrorCode.UNKNOWN; return(View("ErrorMessage", ErrorMessage)); } var orderDetail = getOrder.OrderDetails.SingleOrDefault(m => m.OrderDetailId.ToString() == item); if (orderDetail == null) { ErrorMessage.ErrorCode = ErrorCode.UNKNOWN; return(View("ErrorMessage", ErrorMessage)); } var result = new ProcessSecurityCodeViewModel { Order = getOrder, OrderDetail = orderDetail }; var changedOrder = orderDetail; try { if (User.Identity.Name == changedOrder.Item.Owner.Email) { if (UserCode.SecurityCode == changedOrder.RenterCode) { changedOrder.Status = (int)OrderStatus.ORDER_COMPLETE; market.Entry(changedOrder).Property(o => o.Status).IsModified = true; market.SaveChanges(); PayOwner(changedOrder); return(RedirectToAction("SecurityCodeCorrect", new { Order = getOrder.OrderId, Item = orderDetail.OrderDetailId })); //return Redirect("/PostCheckOut/SecurityCodeCorrect/?Order=" + getOrder.OrderId + "&Item=" + orderDetail.ToList()[0].OrderDetailId); } else { ModelState.AddModelError("", "The Security Code you entered is not valid. Please enter the correct Security Code"); return(View(result)); } } else if (User.Identity.Name == getOrder.User.Email) { if (UserCode.SecurityCode == orderDetail.OwnerCode) { changedOrder.Status = (int)OrderStatus.ORDER_COMPLETE; market.Entry(changedOrder).Property(o => o.Status).IsModified = true; market.SaveChanges(); PayOwner(changedOrder); return(RedirectToAction("SecurityCodeCorrect", new { Order = getOrder.OrderId, Item = orderDetail.OrderDetailId })); } else { ModelState.AddModelError("", "The Security Code you entered is not valid. Please enter the correct Security Code"); return(View(result)); } } } catch (Exception ex) { ErrorMessage.ErrorCode = ErrorCode.UNKNOWN; return(View("ErrorMessage", ErrorMessage)); } return(View(result)); }
public ActionResult Create(ItemModel item) { Session["CategoryId"] = new SelectList(market.Categories, "Id", "Name"); Session["AgreementId"] = new SelectList(market.Agreements.Where(a => a.UserModelEmail == User.Identity.Name), "Id", "FileName"); Session["RegionId"] = new SelectList(market.Regions, "Id", "Name"); if (ModelState.IsValid) { // So somehow by setting Session here as well prevents fatal exceptions. Don't ask me how. var user = market.Users.SingleOrDefault(u => u.Email == User.Identity.Name); string itemID = System.Guid.NewGuid().ToString(); string blackOutDates = Request.Params.Get("datepicker"); string upload_result = string.Empty; int valid_file_count = 0; item.Id = itemID; if (Request.Files.Count > 0) { for (int i = 0; i < Request.Files.Count; i++) { if (Request.Files[i].ContentLength <= 0) { continue; } valid_file_count++; upload_result = UploadPicture(Request.Files[i], itemID); if (upload_result == "valid") { item.ImageCount += 1; } else if (upload_result == "exception_wrong_format") { ErrorMessage.ErrorCode = ErrorCode.IMAGE_FORMAT_NOT_SUPPORTED; return(View("ErrorMessage", ErrorMessage)); } else if (upload_result == "exception_image_too_big") { ErrorMessage.ErrorCode = ErrorCode.IMAGE_SIZE_TOO_BIG; return(View("ErrorMessage", ErrorMessage)); } else if (upload_result == "exception") { ErrorMessage.ErrorCode = ErrorCode.IMAGE_UPLOAD_FAILED; return(View("ErrorMessage", ErrorMessage)); } } } if (valid_file_count == 0) { ErrorMessage.ErrorCode = ErrorCode.NO_IMAGE_UPLOADED; return(View("ErrorMessage", ErrorMessage)); } if (String.IsNullOrEmpty(blackOutDates) == false) { string[] dates = blackOutDates.Split(','); foreach (string d in dates) { DateTime bOutDate = DateTime.Parse(d); market.BlackoutDates.Add(new BlackoutDate { Date = bOutDate, ItemModelId = item.Id }); } } item.UserModelEmail = User.Identity.Name; item.DateCreated = System.DateTime.Now; item.isActive = true; item.CountryId = 1; market.Addresses.Add(new Address { ItemModelId = itemID, StreetAddress1 = Request.Params.Get("PickupLocation.StreetAddress1"), StreetAddress2 = Request.Params.Get("PickupLocation.StreetAddress2"), City = Request.Params.Get("PickupLocation.City"), PostalCode = Request.Params.Get("PickupLocation.PostalCode") }); market.Items.Add(item); market.SaveChanges(); // Save item's pickup location to user profile if requested by user if (String.IsNullOrEmpty(Request.Params.Get("saveLocationToProfile")) == false) { user.Address1 = Request.Params.Get("PickupLocation.StreetAddress1"); user.Address2 = Request.Params.Get("PickupLocation.StreetAddress2"); user.City = Request.Params.Get("PickupLocation.City"); user.PostalCode = Request.Params.Get("PickupLocation.PostalCode"); user.RegionId = item.RegionId; user.CountryId = item.CountryId; market.Entry(user).State = EntityState.Modified; market.SaveChanges(); } log.Info("Item - New item " + item.Id + " created by user " + User.Identity.Name); return(RedirectToAction("Details", new { id = item.Id })); } else { return(View(item)); } }
// ************************************** // URL: /Account/Register // ************************************** public ActionResult Register() { if (FacebookWebContext.Current.IsAuthenticated() && FacebookWebContext.Current.IsAuthorized()) { FacebookClient fbClient = new FacebookClient(FacebookWebContext.Current.AccessToken); dynamic me = fbClient.Get("me"); string facebookId = (string)me.id; string userEmail = (string)me.email; if (facebookId == null || userEmail == null) { throw new FacebookOAuthException(); } var user = market.Users.SingleOrDefault(u => u.Email == userEmail); if (user == null) { //User does not exist in our DB, let us make him var location = me.location; if (location == null) { location = me.hometown; if (location == null) { var model = new RegisterModel(); ModelState.AddModelError("", "Oops, your Facebook profile does not have your location information. Please use the form below to request access."); fbClient.Delete(facebookId + "/permissions"); FormsAuthentication.SignOut(); return(View(model)); } } string[] address = ((string)location.name).Split(','); string city = address[0].Trim(); string prov = address[1].Trim(); //Let us create a new user market.Users.Add(new UserModel { Email = userEmail, FirstName = (string)me.first_name, LastName = (string)me.last_name, City = city, RegionId = market.Regions.SingleOrDefault(r => r.Name.Contains(prov)).Id, CountryId = 1, isFacebookUser = true, FacebookUserId = facebookId, ActivationId = Guid.NewGuid().ToString() }); //Let us add a facebook user to our DB with isApproved set to false market.FacebookUsers.Add(new FacebookUser { Id = facebookId, UserModelEmail = (string)me.email, AccessToken = FacebookWebContext.Current.AccessToken, IsApproved = false, Expires = FacebookWebContext.Current.Session.Expires }); market.SaveChanges(); Session["REGISTERED_USER"] = true; log.Info("Account - New user registered (" + userEmail.ToLower() + ")"); return(View()); } else { if (user.isFacebookUser == false) { market.FacebookUsers.Add(new FacebookUser { Id = facebookId, UserModelEmail = (string)me.email, AccessToken = FacebookWebContext.Current.AccessToken, IsApproved = Membership.GetUser(user.Email).IsApproved, Expires = FacebookWebContext.Current.Session.Expires }); market.SaveChanges(); user.isFacebookUser = true; market.Entry(user).State = EntityState.Modified; market.SaveChanges(); if (Membership.GetUser(user.Email).IsApproved == true) { FormsAuthentication.SetAuthCookie((string)me.email, false); Session["USER_F_NAME"] = (string)me.first_name; log.Info("Account - User logged in (" + (string)me.email + ")"); return(RedirectToAction("Index", "Home")); } else { UserMailer.Welcome(newUser: user).Send(); Session["REGISTERED_USER"] = true; log.Info("Account - New user registered (" + user.Email.ToLower() + ")"); return(View()); } } } } ViewBag.PasswordLength = MembershipService.MinPasswordLength; ViewBag.RegionId = new SelectList(market.Regions, "Id", "Name"); return(View()); }