public async Task <ActionResult> Create([Bind(Include = "Name,Street,City,PhoneNumber,CheckIn,CheckOut,ConfirmationFileUrl,Price")] Accommodation accommodation)
        {
            try
            {
                //check valid check-in and check-out
                if (!accommodation.IsCheckInBeforeCheckOut())
                {
                    ModelState.AddModelError("", "Please check the check-in and check-out dates. Check-out cannot be before check-in.");
                }
                else if (ModelState.IsValid)
                {
                    int tripId = Int32.Parse(accommodation.ConfirmationFileUrl); //temporarily storing tripid in confirmationurl

                    Trip trip = await db.Trips.FindAsync(tripId);

                    if (trip.TripOwner != User.Identity.GetUserId())
                    {
                        return(View("CustomisedError", new HandleErrorInfo(
                                        new UnauthorizedAccessException("Oops, this trip doesn't seem to be yours, you cannot add an accommodation to it."),
                                        "Trip", "Index")));
                    }
                    //if before trip start date -> error
                    if (accommodation.CheckIn < trip.StartDate)
                    {
                        ModelState.AddModelError("", "The check-in date is before the trip start date (" + trip.StartDate.ToShortDateString() + "). Please correct.");
                    }
                    else
                    {
                        try
                        {
                            AssignAccommodationToStep(accommodation, trip, true);

                            accommodation.ConfirmationFileUrl = null;
                            db.Accommodations.Add(accommodation);

                            //increase trip budget
                            trip.TotalCost += accommodation.Price;

                            await db.SaveChangesAsync();

                            //return update view in case user wants to attach confirmation file
                            return(RedirectToAction("Edit", new { id = accommodation.AccommodationId }));
                        }
                        catch (ArgumentException ex)
                        {
                            //give feedback to user about which step to check
                            ViewBag.ErrorMessage = ex.Message;
                        }
                    }
                }
            }
            catch (RetryLimitExceededException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, contact the system administrator.");
            }
            ViewBag.TripId   = accommodation.ConfirmationFileUrl;
            ViewBag.CheckIn  = String.Format("{0:dd-MM-yyyy hh:mm tt}", accommodation.CheckIn);
            ViewBag.CheckOut = String.Format("{0:dd-MM-yyyy hh:mm tt}", accommodation.CheckOut);
            return(View(accommodation));
        }
Esempio n. 2
0
        public void CanCheckInBeforeCheckingOut()
        {
            Accommodation acc = new Accommodation()
            {
                CheckIn  = new DateTime(2018, 2, 3, 14, 0, 0),
                CheckOut = new DateTime(2018, 2, 4, 9, 0, 0)
            };

            Assert.AreEqual(true, acc.IsCheckInBeforeCheckOut());
        }
Esempio n. 3
0
        public void CannotCheckOutBeforeCheckingIn()
        {
            Accommodation acc1 = new Accommodation()
            {
                CheckIn  = new DateTime(2018, 2, 3, 9, 0, 0),
                CheckOut = new DateTime(2018, 2, 3, 14, 0, 0)
            };

            Accommodation acc2 = new Accommodation()
            {
                CheckIn  = new DateTime(2018, 2, 4, 14, 0, 0),
                CheckOut = new DateTime(2018, 2, 3, 9, 0, 0)
            };

            Assert.AreEqual(false, acc1.IsCheckInBeforeCheckOut());
            Assert.AreEqual(false, acc2.IsCheckInBeforeCheckOut());
        }