Beispiel #1
0
        // GET: Checkout
        public ActionResult Index(int rackId, string message = "")
        {
            if (!authorize())
            {
                return(RedirectToAction("authError", "Error"));
            }
            var model = new BikeShare.ViewModels.CheckoutViewModel();

            model.currentRack    = context.BikeRack.Find(rackId);
            model.errorMessage   = message;
            model.userToCheckIn  = null;
            model.checkOutPerson = User.Identity.Name;
            var baseBikeList = context.Bike.Where(b => !b.isArchived).Where(r => r.bikeRackId.Value == rackId || !r.bikeRackId.HasValue).ToList();

            model.availableBikes          = baseBikeList.Where(i => i.isAvailable() && i.bikeRackId.HasValue);
            model.unavailableBikes        = baseBikeList.Where(i => !i.isAvailable());
            model.checkedOutBikes         = baseBikeList.Where(r => !r.bikeRackId.HasValue);
            model.lastCheckoutUserForBike = new Dictionary <int, string>();
            model.lastCheckoutTimeForBike = new Dictionary <int, string>();
            var listBikeIds = baseBikeList.Select(i => i.bikeId).ToList();
            var checkouts   = context.CheckOut.Where(c => listBikeIds.Contains(c.bike)).ToList().OrderByDescending(t => t.timeOut).ToList();

            foreach (var bike in baseBikeList)
            {
                try
                {
                    var first = checkouts.Where(b => b.bike == bike.bikeId).First();
                    checkouts = checkouts.Where(b => b.bike != bike.bikeId).ToList();
                    checkouts.Add(first);
                }
                catch
                {
                    model.lastCheckoutTimeForBike.Add(bike.bikeId, "no data");
                    model.lastCheckoutUserForBike.Add(bike.bikeId, "no data");
                }
            }
            foreach (var checkout in checkouts)
            {
                int lastPerson = checkout.rider;
                try
                {
                    model.lastCheckoutUserForBike.Add(checkout.bike, context.BikeUser.Find(lastPerson).userName);
                    if (checkout.timeIn.HasValue)
                    {
                        model.lastCheckoutTimeForBike.Add(checkout.bike, checkout.timeIn.ToString());
                    }
                    else
                    {
                        model.lastCheckoutTimeForBike.Add(checkout.bike, checkout.timeOut.ToString());
                    }
                }
                catch (NullReferenceException)
                {
                    model.lastCheckoutTimeForBike.Add(checkout.bike, "no data");
                    model.lastCheckoutUserForBike.Add(checkout.bike, "no data");
                }
            }

            return(View(model));
        }
Beispiel #2
0
        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!" }));
        }
Beispiel #3
0
 public ActionResult checkInBike(int rackId, [Bind] BikeShare.ViewModels.CheckoutViewModel model)
 {
     if (!authorize())
     {
         return(RedirectToAction("authError", "Error"));
     }
     try
     {
         var checkout = context.CheckOut.Where(t => !t.timeIn.HasValue).Where(b => b.bike == model.selectedBikeForCheckout).First();
         checkout.isResolved = true;
         checkout.timeIn     = DateTime.Now;
         var bike = context.Bike.Find(model.selectedBikeForCheckout);
         bike.bikeRackId = rackId;
         context.SaveChanges();
     }
     catch (System.Data.Entity.Validation.DbEntityValidationException)
     {
         return(RedirectToAction("Index", new { rackId = rackId, message = "There was an issue checking the bike in." }));
     }
     return(RedirectToAction("Index", new { rackId = rackId, message = "Bike successfully checked in." }));
 }
        // GET: Checkout
        public ActionResult Index(int rackId, string message = "")
        {
            if (!authorize()) { return RedirectToAction("authError", "Error"); }
            var model = new BikeShare.ViewModels.CheckoutViewModel();

            model.currentRack = context.BikeRack.Find(rackId);
            model.errorMessage = message;
            model.userToCheckIn = null;
            model.checkOutPerson = User.Identity.Name;
            var baseBikeList = context.Bike.Where(b => !b.isArchived).Where(r => r.bikeRackId.Value == rackId || !r.bikeRackId.HasValue).ToList();
            model.availableBikes = baseBikeList.Where(i => i.isAvailable() && i.bikeRackId.HasValue);
            model.unavailableBikes = baseBikeList.Where(i => !i.isAvailable());
            model.checkedOutBikes = baseBikeList.Where(r => !r.bikeRackId.HasValue);
            model.lastCheckoutUserForBike = new Dictionary<int, string>();
            model.lastCheckoutTimeForBike = new Dictionary<int, string>();
            var listBikeIds = baseBikeList.Select(i => i.bikeId).ToList();
            var checkouts = context.CheckOut.Where(c=> listBikeIds.Contains(c.bike)).ToList().OrderByDescending(t => t.timeOut).ToList();
            foreach(var bike in baseBikeList)
            {
                try
                {
                    var first = checkouts.Where(b => b.bike == bike.bikeId).First();
                    checkouts = checkouts.Where(b => b.bike != bike.bikeId).ToList();
                    checkouts.Add(first);
                }
                catch
                {
                    model.lastCheckoutTimeForBike.Add(bike.bikeId, "no data");
                    model.lastCheckoutUserForBike.Add(bike.bikeId, "no data");
                }
            }
            foreach(var checkout in checkouts)
            {
                int lastPerson = checkout.rider;
                try
                {

                    model.lastCheckoutUserForBike.Add(checkout.bike, context.BikeUser.Find(lastPerson).userName);
                    if (checkout.timeIn.HasValue)
                    {
                        model.lastCheckoutTimeForBike.Add(checkout.bike, checkout.timeIn.ToString());
                    }
                    else
                    {
                        model.lastCheckoutTimeForBike.Add(checkout.bike, checkout.timeOut.ToString());
                    }
                }
                catch(NullReferenceException)
                {
                    model.lastCheckoutTimeForBike.Add(checkout.bike, "no data");
                    model.lastCheckoutUserForBike.Add(checkout.bike, "no data");
                }
            }

            return View(model);
        }