public IHttpActionResult AddStudent(int id, Student newStudent) { // Checking if the object is not valid if (newStudent == null) throw new HttpResponseException(HttpStatusCode.PreconditionFailed); if (!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.PreconditionFailed); // Finding the course var course = _courses.Find(x => id == x.ID); if (course == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } // Course was found, creating the student and adding // it to the user list Student student = new Student { SSN = newStudent.SSN, Name = newStudent.Name }; course.Students.Add(student); var location = Url.Link("GetStudents", new { id = course.ID }); return Created(location, student); }
public IHttpActionResult AddStudentToCourse(int id, Student student) { //checking if the student being added is not of the right data type if (student == null) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } foreach (Course c in _courses) { if (c.ID == id) { _courses[_courses.IndexOf(c)].Students.Add(student); var location = Url.Link("GetCourse", new { id = c.ID }); return Created(location, student); } } //return 404 return NotFound(); }