public ActionResult CreateViewModel(string id, BookingViewModel bm)
        {
            var currentUser = getUser();

            var kennel = db.Kennel.Where(k => k.KennelID.ToUpper() == id.ToUpper()).First();

            ViewBag.KennelName = kennel.Name;
            //Loop through each date between start and end to check if the kennel is full for that date or not.
            for (DateTime date = bm.StartDate; date <= bm.EndDate; date = date.AddDays(1))
            {
                bool kennelFull = db.KennelAvailability.Where(k => k.KennelID.ToUpper() == id.ToUpper() && k.BookingDate == date).Any(a => a.Full.Equals(true));

                //If for any date in the loop kennelFull = true then a full boolean is triggered to be true.
                if (kennelFull == true)
                {
                    full = true;
                }
            }

            if (ModelState.IsValid)
            {
                int Tnights = bm.CalcTotalNights(bm.StartDate, bm.EndDate);
                if (Tnights < kennel.MaxDays)
                {
                    //If any date in the booking is full then it will not add the booking instead skip to the else.
                    if (full == false)
                    {
                        var booking = new BookingViewModel
                        {
                            StartDate = bm.StartDate,
                            EndDate   = bm.EndDate,
                            PetsName  = bm.PetsName
                        };
                        TempData["booking"] = bm;
                        return(RedirectToAction("Create", new { id = id }));
                    }
                    else
                    {
                        //return a view where it says that one or more of the dates selected is full.
                        ModelState.AddModelError("", "Kennel full on selected dates");
                        return(View(bm));
                    }
                }
                else
                {
                    //return a view where it says exceeds max days.
                    ModelState.AddModelError("", "Exceded maximum days kennel allows bookings for (Maximum: " + kennel.MaxDays + " days)");
                    return(View(bm));
                }
            }
            else
            {
                ModelState.AddModelError("", "Somethings not right please correct and try again.");
                return(View(bm));
            }
        }
Ejemplo n.º 2
0
        public void CalcTotalNightsTestFail()
        {
            var      model     = new BookingViewModel();
            DateTime startDate = DateTime.Now;
            DateTime endDate   = DateTime.Now.AddDays(7);

            int result = model.CalcTotalNights(startDate, endDate);

            Assert.AreNotEqual(17, result);
        }