public IList <UserInfoViewModel> GetTeachersCouldBeRemoved(string callerId, int courseId) { var editingCourse = _context.Courses.Include(c => c.Teachers) .FirstOrDefault(c => c.Id == courseId); if (editingCourse == null) { throw new NotFoundException(); } return(editingCourse.Teachers .Where(teacher => KaCakeUtils.isAppointer(editingCourse, callerId, teacher)) .Select(teacher => new UserInfoViewModel { FullName = _context.Users.Find(teacher.TeacherId).FullName, UserName = _context.Users.Find(teacher.TeacherId).UserName, UserId = teacher.TeacherId }) .ToList()); }
public CourseViewModel Edit(string callerId, int courseId, CreateViewModel course) { Course editingCourse; if ((editingCourse = _context.Courses .Include(c => c.Teachers) .Include(c => c.Students) .FirstOrDefault(c => c.Id == courseId)) != null) { // The course could be edited only by a teacher of that course if (editingCourse.Teachers.Any(teacher => teacher.TeacherId == callerId)) { editingCourse.Name = course.Name; editingCourse.Description = course.Description; if (course.TeachersToAdd != null) { course.TeachersToAdd.Select( teacherId => new CourseTeacher2 { CourseId = course.Id.GetValueOrDefault(), TeacherId = teacherId, AppointerId = callerId } ).ToList().ForEach(teacher => editingCourse.Teachers.Add(teacher)); } if (course.TeachersToRemove != null) { var teachersToRemove = course.TeachersToRemove .Where(userId => editingCourse.Teachers.Any(teacher => teacher.TeacherId.Equals(userId))) .Select(teacherId => editingCourse.Teachers.First(teacher => teacher.TeacherId.Equals(teacherId))); foreach (var teacherToRemove in teachersToRemove) { // Only appointer can remove teachers appointed by him if (KaCakeUtils.isAppointer(editingCourse, callerId, teacherToRemove)) { editingCourse.Teachers.Remove(teacherToRemove); } else { throw new IllegalAccessException(); } } } if (course.StudentsToAdd != null) { foreach (var student in course.StudentsToAdd) { editingCourse.Students.Add(new CourseEnrollment { CourseId = editingCourse.Id, UserId = student }); } } if (course.StudentsToRemove != null) { var studentsToRemove = course.StudentsToRemove .Where(userId => editingCourse.Students.Any(student => student.UserId == userId)) .Select(studentId => editingCourse.Students.First(student => student.UserId == studentId)); foreach (var student in studentsToRemove) { editingCourse.Students.Remove(student); } } _context.SaveChanges(); return(GetCourse(callerId, courseId)); } else { throw new IllegalAccessException(); } } else { throw new NotFoundException(); } }