/// <summary> /// Helper function for the AddStudentToCourse function. Adds a student to a /// course without any checks. An DbUpdateException is thrown if it's not possible /// and that's caught and checked in the AddStudentToCourse function. /// </summary> /// <param name="cId">The course's Id</param> /// <param name="model">AddStudentToCourseViewModel containing the student's SSN</param> private void AddStudentToCourseNoChecks(int cId, AddStudentToCourseViewModel model) { _db.StudentCourseRelations.Add(new StudentCourseRelation { CourseId = cId, StudentId = model.SSN }); // No need to add 'Deleted = false' because the DB does that by default // Table: StudentCourseRelations, Field: Deleted, default value = false _db.SaveChanges(); }
public ActionResult AddStudentToCourse(int id) { AddStudentToCourseViewModel model = new AddStudentToCourseViewModel(); model.CourseID = id; var students = _userService.GetSortedUsers("Student"); model.AvailableStudents = students; return(View(model)); }
/// <summary> /// Tries to add a student to a course. If either one or neither exist, or if /// a connection already exists, the database throws a DbUpdateException. /// That exception is used to figure out what went wrong and then an /// AppObjectNotFoundException or an AppObjectExistsException is thrown. /// If no DbUpdateException is thrown, a connection has made between the course /// and the student and (s)he's now enrolled in that course. /// </summary> /// <param name="cId">The course's Id</param> /// <param name="model">AddStudentToCourseViewModel containing the student's SSN</param> /// <exception cref="AppObjectNotFoundException" /> /// <exception cref="AppObjectExistsException" /> /// <exception cref="MaxNrOfStudentsReachedException" /> public StudentLiteDTO AddStudentToCourse(int cId, AddStudentToCourseViewModel model) { CheckIfCourseExists(cId); var maxStudents = (from c in _db.Courses where c.Id == cId select c.MaxStudents).SingleOrDefault(); var studentsInCourse = NumberOfStudentsInCourse(cId); if (maxStudents != 0 && studentsInCourse >= maxStudents) { throw new MaxNrOfStudentsReachedException(); } var relation = (from rel in _db.StudentCourseRelations where rel.CourseId == cId && rel.StudentId == model.SSN select rel).SingleOrDefault(); if (relation == null) { try { AddStudentToCourseNoChecks(cId, model); RemoveStudentFromWaitinglistIfExists(cId, model.SSN); return(GetStudentBySSN(model.SSN)); } catch (DbUpdateException e) { var sqliteException = e.InnerException as SqliteException; // SqLite Error code 19 is for constraint violations if (sqliteException == null || sqliteException.SqliteErrorCode != 19) { throw; } if (sqliteException.Message.Trim().StartsWith("SQLite Error 19: \'FOREIGN KEY")) { throw new AppObjectNotFoundException(); // CourseId and/or StudentId don't exist } if (sqliteException.Message.Trim().StartsWith("SQLite Error 19: \'UNIQUE")) { throw new AppObjectExistsException(); // There already exists a relation } throw; } } if (!relation.Deleted) { throw new AppObjectExistsException(); } relation.Deleted = false; _db.SaveChanges(); RemoveStudentFromWaitinglistIfExists(cId, model.SSN); return(GetStudentBySSN(model.SSN)); }
/// <summary> /// Tries to add a student to a course's waitinglist. If either one or neither /// exist, or if the student's already enrolled or on the waitinglist, the database /// throws a DbUpdateException. /// That exception is used to figure out what went wrong and then an /// AppObjectNotFoundException or an AppObjectExistsException is thrown. If no /// DbUpdateException is thrown, the student's been added to the course's waitinlist. /// </summary> /// <param name="cId">The course's Id</param> /// <param name="model">AddStudentToCourseViewModel containing the student's SSN</param> /// <exception cref="AppObjectNotFoundException" /> /// <exception cref="AppObjectExistsException" /> public StudentLiteDTO AddStudentToWaitinglist(int cId, AddStudentToCourseViewModel model) { CheckIfCourseExists(cId); // <- this function throws an exception if course doesn't exist var relation = (from rel in _db.StudentCourseRelations where rel.CourseId == cId && rel.StudentId == model.SSN && !rel.Deleted select rel).SingleOrDefault(); if (relation != null) { throw new AppObjectExistsException(); } var wrelation = (from wrel in _db.StudentWaitinglistRelations where wrel.CourseId == cId && wrel.StudentId == model.SSN select wrel).SingleOrDefault(); if (wrelation != null) { throw new AppObjectExistsException(); } _db.StudentWaitinglistRelations.Add(new StudentWaitinglistRelation { CourseId = cId, StudentId = model.SSN }); try { _db.SaveChanges(); } catch (DbUpdateException e) { var sqliteException = e.InnerException as SqliteException; // SqLite Error code 19 is for constraint violations if (sqliteException == null || sqliteException.SqliteErrorCode != 19) { throw; } if (sqliteException.Message.Trim().StartsWith("SQLite Error 19: \'FOREIGN KEY")) { throw new AppObjectNotFoundException(); // CourseId and/or StudentId don't exist } if (sqliteException.Message.Trim().StartsWith("SQLite Error 19: \'UNIQUE")) { throw new AppObjectExistsException(); // There already exists a relation } throw; } return(GetStudentBySSN(model.SSN)); }
public IActionResult AddStudentToWaitinglist(int id, [FromBody] AddStudentToCourseViewModel model) { if (model == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } try { var student = _service.AddStudentToWaitinglist(id, model); return(new OkObjectResult(student)); } catch (AppObjectNotFoundException) { return(new NotFoundResult()); } catch (AppObjectExistsException) { return(new StatusCodeResult(412)); } }
public IActionResult AddStudentToACourse(int id, [FromBody] AddStudentToCourseViewModel model) { if (!ModelState.IsValid) { return(new BadRequestResult()); } try { _service.AddStudentToCourse(id, model.StudentSSN); return(new NoContentResult()); } catch (AppObjectNotFoundException) { return(new NotFoundResult()); } catch (AppObjectExistsException) { return(new BadRequestResult()); } }
public IActionResult AddStudentToCourse(int id, [FromBody] AddStudentToCourseViewModel model) { if (model == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } try { var student = _service.AddStudentToCourse(id, model); var location = Url.Link("GetAllStudentsInCourse", new { id }); return(new CreatedResult(location, student)); } catch (AppObjectNotFoundException) { return(new NotFoundResult()); } catch (AppObjectExistsException) { return(new StatusCodeResult(412)); } catch (MaxNrOfStudentsReachedException) { return(new StatusCodeResult(412)); } }
//If student is not enrolled in course nor on the waiting list he will be added public void AddStudentToWaitingList(int id, AddStudentToCourseViewModel studentToAdd) { // get course var course = _db.Courses.SingleOrDefault(x => x.ID == id); if (course == null) { throw new NoSchoolNotFoundException(); } //get student var student = _db.Students.SingleOrDefault(x => x.SSN == studentToAdd.SSN); if (student == null) { throw new NoSchoolNotFoundException(); } //check if student is in course var inCourse = _db.StudentsInCourses.Where(x => x.StudentID == student.ID).SingleOrDefault(x => x.IsActive); if (inCourse != null) { throw new NoSchoolPreconditionFailedException(); } //Check if student is on waiting list and uptade list accordingly var waitingListStatus = _db.WaitingLists.Where(x => x.CourseID == course.ID).SingleOrDefault(x => x.StudentID == student.ID); if (waitingListStatus != null) { throw new NoSchoolPreconditionFailedException(); } waitingListStatus = new Entities.WaitingList(); waitingListStatus.CourseID = course.ID; waitingListStatus.StudentID = student.ID; _db.WaitingLists.Add(waitingListStatus); _db.SaveChanges(); }
public ActionResult AddStudent(AddStudentToCourseViewModel model) { if (!ModelState.IsValid) { TempData["AddUserError"] = Common.NoUser; return(Redirect("/CourseParticipants/Participants/" + model.CourseId)); } var email = model.Username.Substring(model.Username.IndexOf("(") + 1, model.Username.LastIndexOf(")") - model.Username.IndexOf("(") - 1); var student = this.studentService.GetStudentByEmail(email); var studentName = student.ApplicationUser.FirstName + " " + student.ApplicationUser.SecondName + " " + student.ApplicationUser.LastName; this.courseService.AddStudentToCourse(model.CourseId, student.Id); this.postService.Add(new Post { Content = "^t4c66efaa35043beaaeb5f7e7c6a1ed1^ " + studentName + " ^bb81ab8cfb1745708af8ab41b4b74368^", CourseId = model.CourseId, UserId = this.User.Identity.GetUserId(), Date = DateTime.Now }); return(Redirect("/CourseParticipants/Participants/" + model.CourseId)); }
public IHttpActionResult AddToWaitingList(int id, AddStudentToCourseViewModel student) { try { _service.AddStudentToWaitingList(id, student); return(Ok()); } catch (NoSchoolException nex) { if (nex is NoSchoolNotFoundException) { return(NotFound()); } if (nex is NoSchoolPreconditionFailedException) { return(StatusCode(HttpStatusCode.PreconditionFailed)); } return(StatusCode(HttpStatusCode.InternalServerError)); } }
public IHttpActionResult AddStudentToCourse(int id, AddStudentToCourseViewModel studentToAdd) { try { var student = _service.AddStudentToCourse(id, studentToAdd); return(Created("", student)); } catch (NoSchoolException nex) { if (nex is NoSchoolNotFoundException) { return(NotFound()); } if (nex is NoSchoolPreconditionFailedException) { return(StatusCode(HttpStatusCode.PreconditionFailed)); } return(StatusCode(HttpStatusCode.InternalServerError)); } }
/// <summary> /// Adds a student to a course if both exist /// Throws error if student already in course or no room for more students /// If student is on the waiting list he will be removed from there if registerd /// </summary> /// <param name="id"></param> /// <param name="studentInfo"></param> /// <returns></returns> public StudentDTO AddStudentToCourse(int id, AddStudentToCourseViewModel studentInfo) { //checkif student exists var student = _db.Students.SingleOrDefault(x => x.SSN == studentInfo.SSN); if (student == null) { throw new NoSchoolNotFoundException("Person not found"); } //checkif course exists var course = _db.Courses.SingleOrDefault(x => x.ID == id); if (course == null) { throw new NoSchoolNotFoundException("Course not found"); } //checkif course is full var studentsInCourse = _db.StudentsInCourses.Where(x => x.CourseID == course.ID).Count(x => x.IsActive == true); if (course.MaxStudent <= studentsInCourse) { throw new NoSchoolPreconditionFailedException("Max students reached"); } //check registration status of student var registration = _db.StudentsInCourses.Where(x => x.CourseID == course.ID).SingleOrDefault(x => x.StudentID == student.ID); if (registration != null && registration.IsActive) { throw new NoSchoolPreconditionFailedException("Student already registered"); } //Update registration status of student if (registration == null) { registration = new Entities.StudentInCourse(); registration.CourseID = course.ID; registration.StudentID = student.ID; registration.IsActive = true; _db.StudentsInCourses.Add(registration); } else { registration.IsActive = true; } //Check if student is on waiting list and remove if needed var waitingStatus = _db.WaitingLists.Where(x => x.CourseID == course.ID).SingleOrDefault(x => x.StudentID == student.ID); if (waitingStatus != null) { _db.WaitingLists.Remove(waitingStatus); } _db.SaveChanges(); return(new StudentDTO { Name = student.Name, SSN = student.SSN }); }
public AddStudentToCourseView() { InitializeComponent(); DataContext = new AddStudentToCourseViewModel(); }