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 StudentDTO AddStudentToCourseWaitingList(int id, LinkerViewModel model) { var student = GetStudentBySSN(model.SSN); if (student == null) { throw new CustomObjectNotFoundException("Student not found!\n"); } ValidateWLLink(id, model.SSN); var csLink = GetCourseStudentLink(id, model.SSN); if (csLink != null && csLink.IsActive) { throw new CustomForbiddenException($"{student.Name} is already enrolled as a student\n"); } _db.WaitingListLinkers.Add(new WaitingListLinker { SSN = model.SSN, Id = id }); _db.SaveChanges(); return(new StudentDTO { Name = student.Name }); }
public IActionResult WaitingListAdd(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.AddStudentToCourseWaitingList(id, model); return(Ok($"{student.Name} is now registered on the waiting list\n")); // return Created(Url.Link("GetCourseWaitingList", new { id }), student); } catch (CustomObjectNotFoundException e) { return(NotFound(e.Message)); } catch (CustomConflictException e) { //return StatusCode(409, e.Message); return(StatusCode(412, e.Message)); } catch (CustomForbiddenException e) { return(StatusCode(412, e.Message)); //return StatusCode(403, e.Message); } catch (Exception e) { return(StatusCode(500, e.Message)); } }
public StudentDTO AddStudentToCourse(int id, LinkerViewModel model, int capacity) { var student = GetStudentBySSN(model.SSN); if (student == null) { throw new CustomObjectNotFoundException("Student not found!\n"); } if (GetStudentsEntitiesOfCourse(id).Count >= capacity) { throw new CustomForbiddenException("Max students reached\n"); } var csLink = GetCourseStudentLink(id, model.SSN); if (csLink != null) { if (csLink.IsActive) { throw new CustomConflictException($"{student.Name} is already enrolled as a student\n"); } csLink.IsActive = true; } else { _db.CourseStudentLinkers.Add(new CourseStudentLinker { SSN = model.SSN, Id = id }); } _db.SaveChanges(); // remove from waiting list var wlLink = GetWaitingListLink(id, model.SSN); if (wlLink != null) { _db.WaitingListLinkers.Remove(wlLink); _db.SaveChanges(); } return(new StudentDTO { Name = student.Name }); }
public List <StudentDTO> AddStudentToCourse(int id, LinkerViewModel model) { var link = (from l in _db.CourseStudentLinkers where l.Id == id && l.SSN == model.SSN select l).SingleOrDefault(); if (link != null) { return(null); } _db.CourseStudentLinkers.Add(new CourseStudentLinker { SSN = model.SSN, Id = id }); _db.SaveChanges(); return(GetStudentsOfCourse(id)); }