public IActionResult AddStudentToCourse(int courseID, [FromBody] StudentToCourseViewModel student) { if (student == null) // View model failed - 400 { return(BadRequest()); } student.CourseID = courseID; try { _service.AddStudentToCourse(student); } catch (ArgumentException ex) // No course could be found - 404 { return(NotFound()); } // Student successfully registered to the course return(CreatedAtRoute("GetStudents", new { studentSSN = student.SSN }, student)); }
/*[Authorize(Roles = "Marketing Coodinator")]*/ public ActionResult AssignStudentToCourse() { var currentUserId = User.Identity.GetUserId(); /*var showMineClass = _context.Classes.Where(m => m.CoordinatorId == currentUserId).Include(m => m.Coordinator).ToList();*/ //Get account in role student var role = (from r in _context.Roles where r.Name.Contains("Student") select r) .FirstOrDefault(); var showAllStudentInRole = _context.Users.Where(x => x.Roles.Select(y => y.RoleId) .Contains(role.Id)) .ToList(); var newStudentInCourseViewModel = new StudentToCourseViewModel { Students = showAllStudentInRole, /*Classes = showMineClass*/ }; return(View(newStudentInCourseViewModel)); }
/// <summary> /// Adds an existing student to an existing course using StudentToCourseViewModel /// </summary> public void AddStudentToCourse(StudentToCourseViewModel studentInNewCourse) { // Check whether the student and course already exist, they should var student = _db.Students.SingleOrDefault(x => x.SSN == studentInNewCourse.SSN); var course = _db.Courses.SingleOrDefault(x => x.ID == studentInNewCourse.CourseID); // Else throw an exception if (student == null || course == null) { throw new ArgumentException(); } // Check whether the the student is already registered in the course var inTheDb = _db.StudentsInCourses.SingleOrDefault(x => x.StudentID == student.ID && x.CourseID == course.ID); if (inTheDb == null) // Add a new entity to the connection table { _db.StudentsInCourses.Add(new StudentsInCourses { StudentID = student.ID, CourseID = course.ID }); _db.SaveChanges(); } }