public IHttpActionResult AddStudent(int courseID, StudentAddToCourseViewModel incStudent) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } StudentDTO student = null; try { student = _service.AddStudentToCourse(courseID, incStudent.SSN); } catch (AppObjectNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } catch (FullCourseException) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } catch (DuplicateCourseRegistrationException) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } return(Content(HttpStatusCode.Created, student)); }
public IHttpActionResult AddStudentToCourse(int id, AddStudentViewModel model) { if (ModelState.IsValid) { try { _service.AddStudentToCourse(id, model); return(StatusCode(HttpStatusCode.Created)); } catch (AppObjectNotFoundException) { return(NotFound()); } catch (AppPersonNotFoundException) { return(NotFound()); } catch (AlreadyRegisteredException) { return(StatusCode(HttpStatusCode.PreconditionFailed)); } catch (MaxStudentsException) { return(StatusCode(HttpStatusCode.PreconditionFailed)); } } else { return(StatusCode(HttpStatusCode.PreconditionFailed)); } }
public HttpResponseMessage AddStudent(int courseID, StudentViewModel studentVM) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } bool studentAddedSuccessfully = _service.AddStudentToCourse(courseID, studentVM); if (!studentAddedSuccessfully) { throw new HttpResponseException(HttpStatusCode.NotFound); //Í IHttpActionResult.. getur gert return StatusCode(...) } //Would return Created(location, studentDTO) but the API has no location for this object.. //Instead of this, want to use IHttpActionResult and [ResponseType].. then return Content(HttpStatusCode.Created, studentDTO) return(new HttpResponseMessage(HttpStatusCode.Created)); }
public IHttpActionResult AddStudentToCourse(int id, StudentViewModel newStudent) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } try { StudentDTO student = _service.AddStudentToCourse(id, newStudent); var location = Url.Link("GetStudentInCourse", new { id = id, ssn = student.SSN }); return(Created(location, student)); } catch (CourseNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } catch (StudentNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
public IHttpActionResult EnroleStudent(int id, [FromBody] AddStudentViewModel addStudent) { if (!ModelState.IsValid) { return(BadRequest("StudentViewModel not valid!")); } try { return(Content(HttpStatusCode.Created, _service.AddStudentToCourse(id, addStudent))); } catch (NotFoundException) { return(NotFound()); } catch (PreconditionFailedException) { //return new HttpResponseMessage() { Content = new StringContent("bla"), StatusCode = HttpStatusCode.PreconditionFailed }; return(StatusCode(HttpStatusCode.PreconditionFailed)); } catch (DbException) { return(InternalServerError()); } }