Beispiel #1
0
        public JsonResult Index(CourseViewModel selection)
        {
            // Save the course
            try
            {
                // Populate
                selection = PopulateViewModel(selection);
                // Validate
                if (!ModelState.IsValid)
                    return Json(new {status = "error", message = "Validation Errors"});

                // Map
                var course = Mapper.Map<CourseViewModel, Course>(selection);

                // Add
                _courseService.AddCourse(course);

                // Respond
                return Json(new
                {
                    status = "success",
                    message = $"{course.CourseNumber} {course.CourseName} has been created",
                    id = course.CourseNumber
                });
            }
                // The course name or number already exists
            catch (Exception ex) when (ex is CourseNumberNotUniqueException || ex is CourseNameNotUniqueException)
            {
                return Json(
                    new
                    {
                        status = "error",
                        message = "This Course Name or Number already exists"
                    });
            }
        }
Beispiel #2
0
        public JsonResult Edit(CourseViewModel selection)
        {
            try
            {
                _courseService.EditCourse(selection.CourseNumber, selection.CourseName, selection.CourseDescription,
                    selection.WeeklyHours);

                // Respond
                return Json(new
                {
                    status = "success",
                    message = $"{selection.CourseNumber} {selection.CourseName} has been edited",
                    id = selection.CourseNumber
                });
            }
            catch (CourseDoesNotExistException)
            {
                return Json(new {status = "error", message = "Course Does not exist"});
            }
            catch (CourseNameNotUniqueException)
            {
                return Json(new {status = "error", message = "The Course Name must be Unique"});
            }
        }
Beispiel #3
0
 /// <summary>
 ///     Populates the view model.
 /// </summary>
 /// <param name="courseViewModel">The course view model.</param>
 /// <returns></returns>
 private CourseViewModel PopulateViewModel(CourseViewModel courseViewModel)
 {
     courseViewModel.Courses = _courseService.GetCourses;
     return courseViewModel;
 }
Beispiel #4
0
        /// <summary>
        ///     Return a partial view for the Add Course form
        /// </summary>
        /// <param name="courseNumber">The course number.</param>
        /// <returns></returns>
        public PartialViewResult Add(string courseNumber = null)
        {
            var course = _courseService.GetCourses.SingleOrDefault(c => c.CourseNumber == courseNumber);
            if (course == null) return PartialView("_addCourseContent", new CourseViewModel());

            var vm = new CourseViewModel
            {
                CourseDescription = course.CourseDescription,
                WeeklyHours = course.WeeklyHours,
                CourseNumber = course.CourseNumber,
                CourseName = course.CourseName,
                IsEdit = true
            };

            return PartialView("_addCourseContent", vm);
        }