public IActionResult Add(int id, LinkerViewModel model) { if (!ModelState.IsValid) { return(StatusCode(412)); } try { var course = _courseService.GetCourseById(id); if (course == null) { return(NotFound()); } var student = _studentService.AddStudentToCourse(id, model); if (student == null) { return(StatusCode(409)); } return(Created(Url.Link("GetStudentsOfCourse", new { id }), student)); } catch (Exception e) { return(StatusCode(500, e)); } }
public IActionResult StudentAdd(int id, [FromBody] LinkerViewModel model) { if (!ModelState.IsValid) { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); return(StatusCode(412, errors)); } try { var course = _courseService.GetCourseById(id); var student = _studentService.AddStudentToCourse(id, model, course.MaxStudents); return(Created(Url.Link("GetStudentsOfCourse", new { id }), $"{student.Name} is now enrolled in the course\n")); } catch (CustomObjectNotFoundException e) { return(NotFound(e.Message)); } catch (CustomConflictException e) { return(StatusCode(412, e.Message)); //return StatusCode(409, e.Message); } catch (CustomForbiddenException e) { return(StatusCode(412, e.Message)); //return StatusCode(403, e.Message); } catch (Exception e) { return(StatusCode(500, e.Message)); } }