public ActionResult checkOutBike(int rackId, [Bind] BikeShare.ViewModels.CheckoutViewModel model) { if (!authorize()) { return(RedirectToAction("authError", "Error")); } try { Bike bike = context.Bike.Find(model.selectedBikeForCheckout); bike.lastCheckedOut = DateTime.Now; CheckOut check = new CheckOut { bike = bike.bikeId, isResolved = false, rackCheckedOut = rackId, timeOut = DateTime.Now, }; bikeUser rider = context.BikeUser.Where(n => n.userName == model.userToCheckIn).First(); bikeUser dcheckOutPerson = context.BikeUser.Where(n => n.userName == User.Identity.Name).First(); if (!rider.canBorrowBikes || context.CheckOut.Where(u => u.rider == rider.bikeUserId).Where(u => !u.timeIn.HasValue).Count() > 0) { return(RedirectToAction("Index", new { rackId = rackId, message = "The user doesn't have riding privileges." })); } if (rider.lastRegistered.AddDays(context.settings.First().daysBetweenRegistrations) < DateTime.Now) { return(RedirectToAction("Index", new { rackId = rackId, message = "The user's registration is out of date. Please remind them to register." })); } bike.bikeRackId = null; check.rider = rider.bikeUserId; check.checkOutPerson = dcheckOutPerson.bikeUserId; context.CheckOut.Add(check); context.SaveChanges(); Mailing.queueCheckoutNotice(rider.email, DateTime.Now.AddHours(24)); } catch (System.InvalidOperationException) { return(RedirectToAction("Index", new { rackId = rackId, message = "That user isn't in the system." })); } catch (System.Data.Entity.Validation.DbEntityValidationException) { return(RedirectToAction("Index", new { rackId = rackId, message = "Checkout didn't validate. Sorry." })); } return(RedirectToAction("Index", new { rackId = rackId, message = "Checkout successful!" })); }