Esempio n. 1
0
        public JsonResult CreateUpdate(CourseViewModel viewModel)
        {
            if (!ModelState.IsValid)
                return Json(new { error = true, message = "There were errors in the submission" });

            var currentCourse = _courseService.GetCourseById(viewModel.Number);

            // If the course does not exist in the db, add
            if (currentCourse == null)
            {
                try
                {
                    // Build the course object
                    var course = new Course()
                    {
                        CourseID = viewModel.Number,
                        CourseTitle = viewModel.Name,
                        HoursPerWeek = viewModel.WeeklyHours,
                        Description = viewModel.Description
                    };

                    // Add the course
                    _courseService.AddCourse(course);

                    return Json(new { error = false, message = "Course successfully created!" });

                }
                catch (CourseExistsException)
                {
                    // If the course already exists, display error
                    return Json(new {error = true, message = "Course already exists"});
                }
            }

            // Otherwise, update the fields
            currentCourse.CourseID = viewModel.Number;
            currentCourse.CourseTitle = viewModel.Name;
            currentCourse.HoursPerWeek = viewModel.WeeklyHours;
            currentCourse.Description = viewModel.Description;

            // Update the course with the changes
            _courseService.EditCourse(currentCourse);

            return Json(new {error = false, message = "The changes have been made!"});

        }
Esempio n. 2
0
 // Populate Course View Model
 public CourseViewModel PopulateViewModel(CourseViewModel viewModel)
 {
     viewModel.RegisteredCourses = _courseService.GetAllCourses();
     return viewModel;
 }