public IHttpActionResult CreateCourse(CourseCreator courseCreator) { coursesService = new CoursesService(); subjectsService = new SubjectsService(); teachersService = new TeachersService(); subjectToExposeService = new SubjectToExposeService(); courseToExposeService = new CourseToExposeService(); if (courseCreator == null) { return(BadRequest()); } bool exists = false; exists = coursesService.CheckIfCourseAlreadyExists(courseCreator.Title); if (exists) { return(BadRequest()); } List <CourseSubject> courseSubjects = coursesService.CreateCourse(courseCreator); List <SubjectToExpose> subjectsToExpose = new List <SubjectToExpose>(); foreach (CourseSubject courseSubject in courseSubjects) { Subject subject = new Subject(); subject = subjectsService.Find(courseSubject.SubjectId); if (subject == null) { return(BadRequest()); } if (subject.ID == 0) { return(BadRequest()); } Teacher teacher = new Teacher(); teacher = teachersService.GetTeacherNameByID(subject.TeacherId); if (teacher == null) { return(BadRequest()); } SubjectToExpose subjectToExpose = subjectToExposeService.TrimSubject(subject); subjectsToExpose.Add(subjectToExpose); } CourseToExpose course = courseToExposeService .TrimSubject(courseSubjects, courseCreator, subjectsToExpose); return(Created(new Uri(Request.RequestUri + "/" + course.Id), course)); }
public IHttpActionResult UpdateCourse(int id, CourseCreator courseCreator) { coursesService = new CoursesService(); courseToExposeService = new CourseToExposeService(); if (courseCreator == null) { return(NotFound()); } if (courseCreator.SubjectIds.Count() > 5) { return(BadRequest()); } // check if course exists; if it doesn't, send NotFound() Course courseToUpdate = coursesService.GetCourseByCourseId(id); if (courseToUpdate == null) { return(BadRequest()); } // check if title changed and if so update it courseToUpdate = coursesService.UpdateTitle(id, courseCreator.Title); if (courseToUpdate == null) { return(BadRequest()); } // list of subjects of course before update List <int> oldSubjectList = coursesService.GetCourseSubjectsIds(courseToUpdate); // new list of subjects List <int> newSubjectList = courseCreator.SubjectIds; // compare list of subjects List <int> subjectsToRemove = new List <int>(); List <int> subjectsToAdd = new List <int>(); foreach (int oldSubject in oldSubjectList) { if (!newSubjectList.Contains(oldSubject)) { subjectsToRemove.Add(oldSubject); } } foreach (int newSubject in newSubjectList) { if (!oldSubjectList.Contains(newSubject)) { subjectsToAdd.Add(newSubject); } } // create courseSubjects to new subjects List <CourseSubject> newCourseSubjects = coursesService .CreateCourseSubjectsByIdAndListOfSubjects(courseToUpdate.Id, subjectsToAdd); // remove course subjects coursesService.RemoveCourseSubjetcsByListOfSubjects(id, subjectsToRemove); // create List of subjects to expose List <Subject> subjectsToTrim = coursesService.GetSubjects(newSubjectList); List <SubjectToExpose> subjectsToExpose = coursesService.TrimSubjects(subjectsToTrim); // create Course to expose CourseToExpose courseToExpose = courseToExposeService .TrimUpdateCourse(id, courseCreator, subjectsToExpose); return(Ok(courseToExpose)); }