Пример #1
0
        public ActionResult Create(LessonViewModels.LessonViewModel lessonVM)
        {
            Lesson lesson = new Lesson();

            if (ModelState.IsValid)
            {
                //validates that if the user would like to book a test that it is not longer than 1 hour
                if ((lessonVM.End.Hour - lessonVM.Start.Hour) > 1 && lessonVM.Test)
                {
                    ViewBag.Reload       = true;
                    ViewBag.ReloadLesson = true;
                    this.AddNotification("Sorry but tests cannot be booked for longer than 1 hour", NotificationType.ERROR);
                    return(View());
                }

                //adds the lesson to the session if the session already exists
                if (Session["Booking"] != null)
                {
                    if (lessonVM.Start.Hour >= 9 && lessonVM.End.Hour <= 20)
                    {
                        lesson = lesson.calculateCost(lessonVM);

                        lesson.Start = lessonVM.Start;
                        lesson.End   = lessonVM.End;
                        Booking booking = Session["Booking"] as Booking;
                        booking.AddLesson(lesson);
                        Session["Booking"] = booking;
                    }
                }
                //creates a session variable and adds the lesson to the session
                else
                {
                    if (lessonVM.Start.Hour >= 9 && lessonVM.End.Hour <= 20)
                    {
                        lesson = lesson.calculateCost(lessonVM);

                        //creates the lesson object to add to the lessons stored in the booking object in the session variable
                        lesson.Start = lessonVM.Start;
                        lesson.End   = lessonVM.End;
                        Booking booking = new Booking();
                        booking.AddLesson(lesson);
                        Session["Booking"] = booking;
                    }
                }

                //used to close the create partial and reload the make booking page to update the selected lessons partial
                ViewBag.Reload       = true;
                ViewBag.ReloadLesson = true;

                return(View());
            }

            return(View(lesson));
        }