| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.frontiers.config.AllowApiKeyAccess; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 9 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 10 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 11 | import edu.ucsb.cs156.frontiers.enums.School; | |
| 12 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 13 | import edu.ucsb.cs156.frontiers.errors.InvalidInstallationTypeException; | |
| 14 | import edu.ucsb.cs156.frontiers.models.CourseWarning; | |
| 15 | import edu.ucsb.cs156.frontiers.models.CurrentUser; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 18 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 19 | import edu.ucsb.cs156.frontiers.repositories.InstructorRepository; | |
| 20 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 21 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 22 | import edu.ucsb.cs156.frontiers.services.CanvasApiTokenSecurityService; | |
| 23 | import edu.ucsb.cs156.frontiers.services.OrganizationLinkerService; | |
| 24 | import edu.ucsb.cs156.jobs.repositories.JobsRepository; | |
| 25 | import io.swagger.v3.oas.annotations.Operation; | |
| 26 | import io.swagger.v3.oas.annotations.Parameter; | |
| 27 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 28 | import java.security.NoSuchAlgorithmException; | |
| 29 | import java.security.spec.InvalidKeySpecException; | |
| 30 | import java.util.ArrayList; | |
| 31 | import java.util.List; | |
| 32 | import java.util.Map; | |
| 33 | import java.util.Objects; | |
| 34 | import java.util.Optional; | |
| 35 | import java.util.stream.Collectors; | |
| 36 | import java.util.stream.StreamSupport; | |
| 37 | import lombok.extern.slf4j.Slf4j; | |
| 38 | import org.springframework.beans.factory.annotation.Autowired; | |
| 39 | import org.springframework.http.HttpHeaders; | |
| 40 | import org.springframework.http.HttpStatus; | |
| 41 | import org.springframework.http.ResponseEntity; | |
| 42 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 43 | import org.springframework.transaction.annotation.Transactional; | |
| 44 | import org.springframework.web.bind.annotation.*; | |
| 45 | ||
| 46 | @Tag(name = "Course") | |
| 47 | @RequestMapping("/api/courses") | |
| 48 | @RestController | |
| 49 | @Slf4j | |
| 50 | public class CoursesController extends ApiController { | |
| 51 | ||
| 52 | @Autowired private CourseRepository courseRepository; | |
| 53 | ||
| 54 | @Autowired private UserRepository userRepository; | |
| 55 | ||
| 56 | @Autowired private RosterStudentRepository rosterStudentRepository; | |
| 57 | ||
| 58 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 59 | ||
| 60 | @Autowired private InstructorRepository instructorRepository; | |
| 61 | ||
| 62 | @Autowired private AdminRepository adminRepository; | |
| 63 | ||
| 64 | @Autowired private CanvasApiTokenSecurityService canvasApiTokenSecurityService; | |
| 65 | ||
| 66 | @Autowired private OrganizationLinkerService linkerService; | |
| 67 | ||
| 68 | @Autowired private JobsRepository jobsRepository; | |
| 69 | ||
| 70 | /** | |
| 71 | * This method creates a new Course. | |
| 72 | * | |
| 73 | * @param courseName the name of the course | |
| 74 | * @param term the term of the course | |
| 75 | * @param school the school of the course | |
| 76 | * @param canvasApiToken the Canvas API token (optional) | |
| 77 | * @param canvasCourseId the Canvas course ID (optional) | |
| 78 | */ | |
| 79 | @Operation(summary = "Create a new course") | |
| 80 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_INSTRUCTOR')") | |
| 81 | @PostMapping("/post") | |
| 82 | public InstructorCourseView postCourse( | |
| 83 | @Parameter(name = "courseName") @RequestParam String courseName, | |
| 84 | @Parameter(name = "term") @RequestParam String term, | |
| 85 | @Parameter(name = "school") @RequestParam School school, | |
| 86 | @Parameter(name = "canvasApiToken") @RequestParam(required = false) String canvasApiToken, | |
| 87 | @Parameter(name = "canvasCourseId") @RequestParam(required = false) String canvasCourseId) { | |
| 88 | // get current date right now and set status to pending | |
| 89 | CurrentUser currentUser = getCurrentUser(); | |
| 90 | Course course = | |
| 91 | Course.builder() | |
| 92 | .courseName(courseName) | |
| 93 | .term(term) | |
| 94 | .school(school) | |
| 95 | .instructorEmail(currentUser.getUser().getEmail().strip()) | |
| 96 | .canvasApiToken(canvasApiTokenSecurityService.encrypt(canvasApiToken)) | |
| 97 | .canvasCourseId(canvasCourseId) | |
| 98 | .build(); | |
| 99 | Course savedCourse = courseRepository.save(course); | |
| 100 | ||
| 101 | User user = currentUser.getUser(); | |
| 102 | CourseStaff courseStaff = | |
| 103 | CourseStaff.builder() | |
| 104 | .firstName(user.getGivenName()) | |
| 105 | .lastName(user.getFamilyName()) | |
| 106 | .email(savedCourse.getInstructorEmail()) | |
| 107 | .course(savedCourse) | |
| 108 | .user(user) | |
| 109 | .orgStatus(OrgStatus.PENDING) | |
| 110 | .build(); | |
| 111 | courseStaffRepository.save(courseStaff); | |
| 112 | ||
| 113 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::postCourse → KILLED |
return new InstructorCourseView(savedCourse); |
| 114 | } | |
| 115 | ||
| 116 | /** Projection of Course entity with fields that are relevant for instructors and admins */ | |
| 117 | public static record InstructorCourseView( | |
| 118 | Long id, | |
| 119 | String installationId, | |
| 120 | String orgName, | |
| 121 | String courseName, | |
| 122 | String term, | |
| 123 | School school, | |
| 124 | String instructorEmail, | |
| 125 | boolean hideBasePermissionWarning, | |
| 126 | boolean hideFreePlanWarning, | |
| 127 | int numStudents, | |
| 128 | int numStaff) { | |
| 129 | ||
| 130 | // Creates view from Course entity | |
| 131 | public InstructorCourseView(Course c) { | |
| 132 | this( | |
| 133 | c.getId(), | |
| 134 | c.getInstallationId(), | |
| 135 | c.getOrgName(), | |
| 136 | c.getCourseName(), | |
| 137 | c.getTerm(), | |
| 138 | c.getSchool(), | |
| 139 | c.getInstructorEmail(), | |
| 140 | c.getHideBasePermissionWarning(), | |
| 141 | c.getHideFreePlanWarning(), | |
| 142 |
1
1. <init> : negated conditional → KILLED |
c.getRosterStudents() != null ? c.getRosterStudents().size() : 0, |
| 143 |
1
1. <init> : negated conditional → KILLED |
c.getCourseStaff() != null ? c.getCourseStaff().size() : 0); |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * This method returns a list of courses. | |
| 149 | * | |
| 150 | * @return a list of all courses for an instructor. | |
| 151 | */ | |
| 152 | @Operation(summary = "List all courses for an instructor") | |
| 153 | @PreAuthorize("hasRole('ROLE_INSTRUCTOR')") | |
| 154 | @GetMapping("/allForInstructors") | |
| 155 | public Iterable<InstructorCourseView> allForInstructors() { | |
| 156 | CurrentUser currentUser = getCurrentUser(); | |
| 157 | String instructorEmail = currentUser.getUser().getEmail(); | |
| 158 | List<Course> courses = courseRepository.findByInstructorEmail(instructorEmail); | |
| 159 | ||
| 160 | List<InstructorCourseView> courseViews = | |
| 161 | courses.stream().map(InstructorCourseView::new).collect(Collectors.toList()); | |
| 162 |
1
1. allForInstructors : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CoursesController::allForInstructors → KILLED |
return courseViews; |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * This method returns a list of courses. | |
| 167 | * | |
| 168 | * @return a list of all courses for an admin. | |
| 169 | */ | |
| 170 | @Operation(summary = "List all courses for an admin") | |
| 171 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 172 | @GetMapping("/allForAdmins") | |
| 173 | public Iterable<InstructorCourseView> allForAdmins() { | |
| 174 | List<Course> courses = courseRepository.findAll(); | |
| 175 | ||
| 176 | List<InstructorCourseView> courseViews = | |
| 177 | courses.stream().map(InstructorCourseView::new).collect(Collectors.toList()); | |
| 178 |
1
1. allForAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CoursesController::allForAdmins → KILLED |
return courseViews; |
| 179 | } | |
| 180 | ||
| 181 | /** | |
| 182 | * This method returns single course by its id | |
| 183 | * | |
| 184 | * @return a course | |
| 185 | */ | |
| 186 | @Operation(summary = "Get course by id") | |
| 187 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #id)") | |
| 188 | @GetMapping("/{id}") | |
| 189 | public InstructorCourseView getCourseById(@Parameter(name = "id") @PathVariable Long id) { | |
| 190 | Course course = | |
| 191 | courseRepository | |
| 192 | .findById(id) | |
| 193 |
1
1. lambda$getCourseById$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$getCourseById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 194 | // Convert to InstructorCourseView | |
| 195 | InstructorCourseView courseView = new InstructorCourseView(course); | |
| 196 |
1
1. getCourseById : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::getCourseById → KILLED |
return courseView; |
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 200 | * This method returns the Canvas course ID and partially obscured Canvas token for a course by | |
| 201 | * its id. If the token is less than or equal to 3 characters long, it is returned in full. | |
| 202 | * Otherwise, all but the last three characters are replaced with asterisks. This is okay because | |
| 203 | * such short tokens are not generated by Canvas. | |
| 204 | * | |
| 205 | * @param courseId the id of the course | |
| 206 | * @return a map with courseId, canvasCourseId, and obscured canvasApiToken | |
| 207 | */ | |
| 208 | @Operation(summary = "Get course Canvas course ID and Canvas token (partially obscured)") | |
| 209 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 210 | @GetMapping("getCanvasInfo") | |
| 211 | public Map<String, String> getCourseCanvasInfo( | |
| 212 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 213 | Course course = | |
| 214 | courseRepository | |
| 215 | .findById(courseId) | |
| 216 |
1
1. lambda$getCourseCanvasInfo$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$getCourseCanvasInfo$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 217 | ||
| 218 | String obscuredToken = null; | |
| 219 | ||
| 220 |
1
1. getCourseCanvasInfo : negated conditional → KILLED |
if (course.getCanvasApiToken() != null) { |
| 221 | String token = canvasApiTokenSecurityService.decrypt(course.getCanvasApiToken()); | |
| 222 |
2
1. getCourseCanvasInfo : negated conditional → KILLED 2. getCourseCanvasInfo : changed conditional boundary → KILLED |
if (token.length() < 4) { |
| 223 | obscuredToken = token; | |
| 224 | } else { | |
| 225 |
1
1. getCourseCanvasInfo : Replaced integer subtraction with addition → KILLED |
String lastThree = token.substring(token.length() - 3); |
| 226 |
1
1. getCourseCanvasInfo : Replaced integer subtraction with addition → KILLED |
obscuredToken = "*".repeat(token.length() - 3) + lastThree; |
| 227 | } | |
| 228 | } | |
| 229 |
1
1. getCourseCanvasInfo : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/CoursesController::getCourseCanvasInfo → KILLED |
return Map.of( |
| 230 | "courseId", course.getId().toString(), | |
| 231 |
1
1. getCourseCanvasInfo : negated conditional → KILLED |
"canvasCourseId", course.getCanvasCourseId() != null ? course.getCanvasCourseId() : "", |
| 232 |
1
1. getCourseCanvasInfo : negated conditional → KILLED |
"canvasApiToken", obscuredToken != null ? obscuredToken : ""); |
| 233 | } | |
| 234 | ||
| 235 | /** | |
| 236 | * This is the outgoing method, redirecting from Frontiers to GitHub to allow a Course to be | |
| 237 | * linked to a GitHub Organization. It redirects from Frontiers to the GitHub app installation | |
| 238 | * process, and will return with the {@link #addInstallation(Optional, String, String, Long) | |
| 239 | * addInstallation()} endpoint | |
| 240 | * | |
| 241 | * @param courseId id of the course to be linked to | |
| 242 | * @return dynamically loaded url to install Frontiers to a Github Organization, with the courseId | |
| 243 | * marked as the state parameter, which GitHub will return. | |
| 244 | */ | |
| 245 | @Operation(summary = "Authorize Frontiers to a Github Course") | |
| 246 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 247 | @GetMapping("/redirect") | |
| 248 | public ResponseEntity<Void> linkCourse(@Parameter Long courseId) | |
| 249 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 250 | String newUrl = linkerService.getRedirectUrl(); | |
| 251 | newUrl += "/installations/new?state=" + courseId; | |
| 252 | // found this convenient solution here: | |
| 253 | // https://stackoverflow.com/questions/29085295/spring-mvc-restcontroller-and-redirect | |
| 254 |
1
1. linkCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::linkCourse → KILLED |
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) |
| 255 | .header(HttpHeaders.LOCATION, newUrl) | |
| 256 | .build(); | |
| 257 | } | |
| 258 | ||
| 259 | /** | |
| 260 | * @param installation_id id of the incoming GitHub Organization installation | |
| 261 | * @param setup_action whether the permissions are installed or updated. Required RequestParam but | |
| 262 | * not used by the method. | |
| 263 | * @param code token to be exchanged with GitHub to ensure the request is legitimate and not | |
| 264 | * spoofed. | |
| 265 | * @param state id of the Course to be linked with the GitHub installation. | |
| 266 | * @return ResponseEntity, returning /success if the course was successfully linked or /noperms if | |
| 267 | * the user does not have the permission to install the application on GitHub. Alternately | |
| 268 | * returns 403 Forbidden if the user is not the creator. | |
| 269 | */ | |
| 270 | @Operation(summary = "Link a Course to a Github Organization by installing Github App") | |
| 271 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_INSTRUCTOR')") | |
| 272 | @GetMapping("link") | |
| 273 | public ResponseEntity<Void> addInstallation( | |
| 274 | @Parameter(name = "installationId") @RequestParam Optional<String> installation_id, | |
| 275 | @Parameter(name = "setupAction") @RequestParam String setup_action, | |
| 276 | @Parameter(name = "code") @RequestParam String code, | |
| 277 | @Parameter(name = "state") @RequestParam Long state) | |
| 278 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 279 |
1
1. addInstallation : negated conditional → KILLED |
if (installation_id.isEmpty()) { |
| 280 |
1
1. addInstallation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::addInstallation → KILLED |
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) |
| 281 | .header(HttpHeaders.LOCATION, "/courses/nopermissions") | |
| 282 | .build(); | |
| 283 | } else { | |
| 284 | Course course = | |
| 285 | courseRepository | |
| 286 | .findById(state) | |
| 287 |
1
1. lambda$addInstallation$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$addInstallation$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, state)); |
| 288 |
1
1. addInstallation : negated conditional → KILLED |
if (!isCurrentUserAdmin() |
| 289 |
1
1. addInstallation : negated conditional → KILLED |
&& !course.getInstructorEmail().equals(getCurrentUser().getUser().getEmail())) { |
| 290 |
1
1. addInstallation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::addInstallation → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 291 | } else { | |
| 292 | String orgName = linkerService.getOrgName(installation_id.get()); | |
| 293 |
1
1. addInstallation : removed call to edu/ucsb/cs156/frontiers/entities/Course::setInstallationId → KILLED |
course.setInstallationId(installation_id.get()); |
| 294 |
1
1. addInstallation : removed call to edu/ucsb/cs156/frontiers/entities/Course::setOrgName → KILLED |
course.setOrgName(orgName); |
| 295 | course | |
| 296 | .getRosterStudents() | |
| 297 |
1
1. addInstallation : removed call to java/util/List::forEach → KILLED |
.forEach( |
| 298 | rs -> { | |
| 299 |
1
1. lambda$addInstallation$1 : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED |
rs.setOrgStatus(OrgStatus.JOINCOURSE); |
| 300 | }); | |
| 301 | course | |
| 302 | .getCourseStaff() | |
| 303 |
1
1. addInstallation : removed call to java/util/List::forEach → KILLED |
.forEach( |
| 304 | cs -> { | |
| 305 |
1
1. lambda$addInstallation$2 : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
cs.setOrgStatus(OrgStatus.JOINCOURSE); |
| 306 | }); | |
| 307 | courseRepository.save(course); | |
| 308 |
1
1. addInstallation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::addInstallation → KILLED |
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) |
| 309 | .header(HttpHeaders.LOCATION, "/login/success") | |
| 310 | .build(); | |
| 311 | } | |
| 312 | } | |
| 313 | } | |
| 314 | ||
| 315 | /** | |
| 316 | * This method handles the InvalidInstallationTypeException. | |
| 317 | * | |
| 318 | * @param e the exception | |
| 319 | * @return a map with the type and message of the exception | |
| 320 | */ | |
| 321 | @ExceptionHandler({InvalidInstallationTypeException.class}) | |
| 322 | @ResponseStatus(HttpStatus.BAD_REQUEST) | |
| 323 | public Object handleInvalidInstallationType(Throwable e) { | |
| 324 |
1
1. handleInvalidInstallationType : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::handleInvalidInstallationType → KILLED |
return Map.of( |
| 325 | "type", e.getClass().getSimpleName(), | |
| 326 | "message", e.getMessage()); | |
| 327 | } | |
| 328 | ||
| 329 | public record RosterStudentCoursesDTO( | |
| 330 | Long id, | |
| 331 | String installationId, | |
| 332 | String orgName, | |
| 333 | String courseName, | |
| 334 | String term, | |
| 335 | String school, | |
| 336 | OrgStatus studentStatus, | |
| 337 | Long rosterStudentId) {} | |
| 338 | ||
| 339 | /** | |
| 340 | * This method returns a list of courses that the current user is enrolled. | |
| 341 | * | |
| 342 | * @return a list of courses in the DTO form along with the student status in the organization. | |
| 343 | */ | |
| 344 | @Operation(summary = "List all courses for the current student, including their org status") | |
| 345 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 346 | @GetMapping("/list") | |
| 347 | public List<RosterStudentCoursesDTO> listCoursesForCurrentUser() { | |
| 348 | String email = getCurrentUser().getUser().getEmail(); | |
| 349 | Iterable<RosterStudent> rosterStudentsIterable = rosterStudentRepository.findAllByEmail(email); | |
| 350 | List<RosterStudent> rosterStudents = new ArrayList<>(); | |
| 351 |
1
1. listCoursesForCurrentUser : removed call to java/lang/Iterable::forEach → KILLED |
rosterStudentsIterable.forEach(rosterStudents::add); |
| 352 |
1
1. listCoursesForCurrentUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CoursesController::listCoursesForCurrentUser → KILLED |
return rosterStudents.stream() |
| 353 | .map( | |
| 354 | rs -> { | |
| 355 | Course course = rs.getCourse(); | |
| 356 | RosterStudentCoursesDTO rsDto = | |
| 357 | new RosterStudentCoursesDTO( | |
| 358 | course.getId(), | |
| 359 | course.getInstallationId(), | |
| 360 | course.getOrgName(), | |
| 361 | course.getCourseName(), | |
| 362 | course.getTerm(), | |
| 363 | course.getSchool().getDisplayName(), | |
| 364 | rs.getOrgStatus(), | |
| 365 | rs.getId()); | |
| 366 |
1
1. lambda$listCoursesForCurrentUser$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$listCoursesForCurrentUser$0 → KILLED |
return rsDto; |
| 367 | }) | |
| 368 | .collect(Collectors.toList()); | |
| 369 | } | |
| 370 | ||
| 371 | public record StaffCoursesDTO( | |
| 372 | Long id, | |
| 373 | String installationId, | |
| 374 | String orgName, | |
| 375 | String courseName, | |
| 376 | String term, | |
| 377 | School school, | |
| 378 | OrgStatus studentStatus, | |
| 379 | Long staffId) {} | |
| 380 | ||
| 381 | public enum EmailTypes { | |
| 382 | STUDENTS, | |
| 383 | STAFF, | |
| 384 | ALL | |
| 385 | } | |
| 386 | ||
| 387 | public enum EmailFormats { | |
| 388 | COMMA_SEPARATED, | |
| 389 | ONE_PER_LINE | |
| 390 | } | |
| 391 | ||
| 392 | /** | |
| 393 | * student see what courses they appear as staff in | |
| 394 | * | |
| 395 | * @param studentId the id of the student making request | |
| 396 | * @return a list of all courses student is staff in | |
| 397 | */ | |
| 398 | @Operation(summary = "Student see what courses they appear as staff in") | |
| 399 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 400 | @GetMapping("/staffCourses") | |
| 401 | public List<StaffCoursesDTO> staffCourses() { | |
| 402 | CurrentUser currentUser = getCurrentUser(); | |
| 403 | User user = currentUser.getUser(); | |
| 404 | ||
| 405 | String email = user.getEmail(); | |
| 406 | ||
| 407 | List<CourseStaff> staffMembers = courseStaffRepository.findAllByEmail(email); | |
| 408 |
1
1. staffCourses : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CoursesController::staffCourses → KILLED |
return staffMembers.stream() |
| 409 | .map( | |
| 410 | s -> { | |
| 411 | Course course = s.getCourse(); | |
| 412 | StaffCoursesDTO sDto = | |
| 413 | new StaffCoursesDTO( | |
| 414 | course.getId(), | |
| 415 | course.getInstallationId(), | |
| 416 | course.getOrgName(), | |
| 417 | course.getCourseName(), | |
| 418 | course.getTerm(), | |
| 419 | course.getSchool(), | |
| 420 | s.getOrgStatus(), | |
| 421 | s.getId()); | |
| 422 |
1
1. lambda$staffCourses$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$staffCourses$0 → KILLED |
return sDto; |
| 423 | }) | |
| 424 | .collect(Collectors.toList()); | |
| 425 | } | |
| 426 | ||
| 427 | @Operation(summary = "Update instructor email for a course (admin only)") | |
| 428 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 429 | @PutMapping("/updateInstructor") | |
| 430 | public InstructorCourseView updateInstructorEmail( | |
| 431 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 432 | @Parameter(name = "instructorEmail") @RequestParam String instructorEmail) { | |
| 433 | ||
| 434 | instructorEmail = instructorEmail.strip(); | |
| 435 | ||
| 436 | Course course = | |
| 437 | courseRepository | |
| 438 | .findById(courseId) | |
| 439 |
1
1. lambda$updateInstructorEmail$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$updateInstructorEmail$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 440 | ||
| 441 | // Validate that the email exists in either instructor or admin table | |
| 442 | boolean isInstructor = instructorRepository.existsByEmail(instructorEmail); | |
| 443 | boolean isAdmin = adminRepository.existsByEmail(instructorEmail); | |
| 444 | ||
| 445 |
2
1. updateInstructorEmail : negated conditional → KILLED 2. updateInstructorEmail : negated conditional → KILLED |
if (!isInstructor && !isAdmin) { |
| 446 | throw new IllegalArgumentException("Email must belong to either an instructor or admin"); | |
| 447 | } | |
| 448 | ||
| 449 |
1
1. updateInstructorEmail : removed call to edu/ucsb/cs156/frontiers/entities/Course::setInstructorEmail → KILLED |
course.setInstructorEmail(instructorEmail); |
| 450 | Course savedCourse = courseRepository.save(course); | |
| 451 | ||
| 452 |
1
1. updateInstructorEmail : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::updateInstructorEmail → KILLED |
return new InstructorCourseView(savedCourse); |
| 453 | } | |
| 454 | ||
| 455 | @Operation(summary = "Get course emails") | |
| 456 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 457 | @GetMapping("/emails") | |
| 458 | @AllowApiKeyAccess | |
| 459 | public String getCourseEmails( | |
| 460 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 461 | @Parameter(name = "type") @RequestParam(defaultValue = "STUDENTS") EmailTypes type, | |
| 462 | @Parameter(name = "team") @RequestParam(required = false) String team, | |
| 463 | @Parameter(name = "format") @RequestParam(defaultValue = "ONE_PER_LINE") | |
| 464 | EmailFormats format) { | |
| 465 | ||
| 466 | List<String> staffEmails = | |
| 467 | StreamSupport.stream(courseStaffRepository.findByCourseId(courseId).spliterator(), false) | |
| 468 | .map(CourseStaff::getEmail) | |
| 469 | .filter(Objects::nonNull) | |
| 470 | .sorted() | |
| 471 | .collect(Collectors.toList()); | |
| 472 | ||
| 473 | // Dropped students are not part of the current roster, so they are never included | |
| 474 | List<String> studentEmails = | |
| 475 | StreamSupport.stream(rosterStudentRepository.findByCourseId(courseId).spliterator(), false) | |
| 476 |
2
1. lambda$getCourseEmails$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$getCourseEmails$0 → KILLED 2. lambda$getCourseEmails$0 : negated conditional → KILLED |
.filter(student -> student.getRosterStatus() != RosterStatus.DROPPED) |
| 477 |
4
1. lambda$getCourseEmails$1 : negated conditional → KILLED 2. lambda$getCourseEmails$1 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$getCourseEmails$1 → KILLED 3. lambda$getCourseEmails$1 : negated conditional → KILLED 4. lambda$getCourseEmails$1 : negated conditional → KILLED |
.filter(student -> team == null || team.isBlank() || student.getTeams().contains(team)) |
| 478 | .map(RosterStudent::getEmail) | |
| 479 | .filter(Objects::nonNull) | |
| 480 | .sorted() | |
| 481 | .collect(Collectors.toList()); | |
| 482 | ||
| 483 | List<String> emails = studentEmails; | |
| 484 |
1
1. getCourseEmails : negated conditional → KILLED |
if (type == EmailTypes.STAFF) { |
| 485 | emails = staffEmails; | |
| 486 |
1
1. getCourseEmails : negated conditional → KILLED |
} else if (type == EmailTypes.ALL) { |
| 487 | emails = new ArrayList<>(staffEmails); | |
| 488 | emails.addAll(studentEmails); | |
| 489 | } | |
| 490 | ||
| 491 |
1
1. getCourseEmails : negated conditional → KILLED |
String separator = format == EmailFormats.COMMA_SEPARATED ? "," : "\r\n"; |
| 492 |
1
1. getCourseEmails : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/CoursesController::getCourseEmails → KILLED |
return String.join(separator, emails); |
| 493 | } | |
| 494 | ||
| 495 | @Operation(summary = "Delete a course") | |
| 496 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 497 | @DeleteMapping("") | |
| 498 | @Transactional | |
| 499 | public Object deleteCourse(@RequestParam Long courseId) | |
| 500 | throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| 501 | Course course = | |
| 502 | courseRepository | |
| 503 | .findById(courseId) | |
| 504 |
1
1. lambda$deleteCourse$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$deleteCourse$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 505 | ||
| 506 | // Check if course has roster students or staff | |
| 507 |
2
1. deleteCourse : negated conditional → KILLED 2. deleteCourse : negated conditional → KILLED |
if (!course.getRosterStudents().isEmpty() || !course.getCourseStaff().isEmpty()) { |
| 508 | throw new IllegalArgumentException("Cannot delete course with students or staff"); | |
| 509 | } | |
| 510 | ||
| 511 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::unenrollOrganization → KILLED |
linkerService.unenrollOrganization(course); |
| 512 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/jobs/repositories/JobsRepository::deleteByScopeTypeAndScopeId → KILLED |
jobsRepository.deleteByScopeTypeAndScopeId("course", courseId); |
| 513 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/frontiers/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
| 514 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::deleteCourse → KILLED |
return genericMessage("Course with id %s deleted".formatted(course.getId())); |
| 515 | } | |
| 516 | ||
| 517 | /** | |
| 518 | * This method updates an existing course. | |
| 519 | * | |
| 520 | * @param courseId the id of the course to update | |
| 521 | * @param courseName the new name of the course | |
| 522 | * @param term the new term of the course | |
| 523 | * @param school the new school of the course | |
| 524 | * @return the updated course | |
| 525 | */ | |
| 526 | @Operation(summary = "Update an existing course") | |
| 527 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 528 | @PutMapping("") | |
| 529 | public InstructorCourseView updateCourse( | |
| 530 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 531 | @Parameter(name = "courseName") @RequestParam String courseName, | |
| 532 | @Parameter(name = "term") @RequestParam String term, | |
| 533 | @Parameter(name = "school") @RequestParam School school) { | |
| 534 | Course course = | |
| 535 | courseRepository | |
| 536 | .findById(courseId) | |
| 537 |
1
1. lambda$updateCourse$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$updateCourse$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 538 | ||
| 539 |
1
1. updateCourse : removed call to edu/ucsb/cs156/frontiers/entities/Course::setCourseName → KILLED |
course.setCourseName(courseName); |
| 540 |
1
1. updateCourse : removed call to edu/ucsb/cs156/frontiers/entities/Course::setTerm → KILLED |
course.setTerm(term); |
| 541 |
1
1. updateCourse : removed call to edu/ucsb/cs156/frontiers/entities/Course::setSchool → KILLED |
course.setSchool(school); |
| 542 | ||
| 543 | Course savedCourse = courseRepository.save(course); | |
| 544 | ||
| 545 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::updateCourse → KILLED |
return new InstructorCourseView(savedCourse); |
| 546 | } | |
| 547 | ||
| 548 | /** | |
| 549 | * This method updates an existing course. | |
| 550 | * | |
| 551 | * @param courseId the id of the course to update | |
| 552 | * @param courseName the new name of the course | |
| 553 | * @param term the new term of the course | |
| 554 | * @param school the new school of the course | |
| 555 | * @param canvasApiToken the new Canvas API token for the course | |
| 556 | * @param canvasCourseId the new Canvas course ID | |
| 557 | * @return the updated course | |
| 558 | */ | |
| 559 | @Operation(summary = "Update an existing course with Canvas token and course ID") | |
| 560 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 561 | @PutMapping("/updateCourseCanvasToken") | |
| 562 | public InstructorCourseView updateCourseWithCanvasToken( | |
| 563 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 564 | @Parameter(name = "canvasApiToken") @RequestParam(required = false) String canvasApiToken, | |
| 565 | @Parameter(name = "canvasCourseId") @RequestParam(required = false) String canvasCourseId) { | |
| 566 | Course course = | |
| 567 | courseRepository | |
| 568 | .findById(courseId) | |
| 569 |
1
1. lambda$updateCourseWithCanvasToken$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$updateCourseWithCanvasToken$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 570 | ||
| 571 | String existingCanvasToken = canvasApiTokenSecurityService.decrypt(course.getCanvasApiToken()); | |
| 572 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
if (canvasApiToken != null |
| 573 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
&& !canvasApiToken.isEmpty() |
| 574 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
&& !canvasApiToken.equals(existingCanvasToken)) { |
| 575 |
1
1. updateCourseWithCanvasToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setCanvasApiToken → KILLED |
course.setCanvasApiToken(canvasApiTokenSecurityService.encrypt(canvasApiToken)); |
| 576 | } | |
| 577 | ||
| 578 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
if (canvasCourseId != null |
| 579 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
&& !canvasCourseId.isEmpty() |
| 580 |
1
1. updateCourseWithCanvasToken : negated conditional → KILLED |
&& !canvasCourseId.equals(course.getCanvasCourseId())) { |
| 581 |
1
1. updateCourseWithCanvasToken : removed call to edu/ucsb/cs156/frontiers/entities/Course::setCanvasCourseId → KILLED |
course.setCanvasCourseId(canvasCourseId); |
| 582 | } | |
| 583 | ||
| 584 | Course savedCourse = courseRepository.save(course); | |
| 585 | ||
| 586 |
1
1. updateCourseWithCanvasToken : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::updateCourseWithCanvasToken → KILLED |
return new InstructorCourseView(savedCourse); |
| 587 | } | |
| 588 | ||
| 589 | @Operation(summary = "Get course warnings") | |
| 590 | @GetMapping("/warnings/{courseId}") | |
| 591 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 592 | public CourseWarning warnings(@PathVariable @Parameter Long courseId) throws Exception { | |
| 593 | Course course = | |
| 594 | courseRepository | |
| 595 | .findById(courseId) | |
| 596 |
1
1. lambda$warnings$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$warnings$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 597 |
1
1. warnings : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::warnings → KILLED |
return linkerService.checkCourseWarnings(course); |
| 598 | } | |
| 599 | ||
| 600 | @Operation(summary = "Hide base permission warning for a course") | |
| 601 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 602 | @PostMapping("/warnings/hideBasePermissionWarning/{courseId}") | |
| 603 | public Object hideBasePermissionWarning(@PathVariable @Parameter Long courseId) { | |
| 604 | Course course = | |
| 605 | courseRepository | |
| 606 | .findById(courseId) | |
| 607 |
1
1. lambda$hideBasePermissionWarning$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$hideBasePermissionWarning$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 608 | ||
| 609 |
1
1. hideBasePermissionWarning : removed call to edu/ucsb/cs156/frontiers/entities/Course::setHideBasePermissionWarning → KILLED |
course.setHideBasePermissionWarning(true); |
| 610 | courseRepository.save(course); | |
| 611 | ||
| 612 |
1
1. hideBasePermissionWarning : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::hideBasePermissionWarning → KILLED |
return genericMessage( |
| 613 | "hideBasePermissionWarning set to true for course with id %s".formatted(courseId)); | |
| 614 | } | |
| 615 | ||
| 616 | @Operation(summary = "Hide free plan warning for a course") | |
| 617 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 618 | @PostMapping("/warnings/hideFreePlanWarning/{courseId}") | |
| 619 | public Object hideFreePlanWarning(@PathVariable @Parameter Long courseId) { | |
| 620 | Course course = | |
| 621 | courseRepository | |
| 622 | .findById(courseId) | |
| 623 |
1
1. lambda$hideFreePlanWarning$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::lambda$hideFreePlanWarning$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 624 | ||
| 625 |
1
1. hideFreePlanWarning : removed call to edu/ucsb/cs156/frontiers/entities/Course::setHideFreePlanWarning → KILLED |
course.setHideFreePlanWarning(true); |
| 626 | courseRepository.save(course); | |
| 627 | ||
| 628 |
1
1. hideFreePlanWarning : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CoursesController::hideFreePlanWarning → KILLED |
return genericMessage( |
| 629 | "hideFreePlanWarning set to true for course with id %s".formatted(courseId)); | |
| 630 | } | |
| 631 | } | |
Mutations | ||
| 113 |
1.1 |
|
| 142 |
1.1 |
|
| 143 |
1.1 |
|
| 162 |
1.1 |
|
| 178 |
1.1 |
|
| 193 |
1.1 |
|
| 196 |
1.1 |
|
| 216 |
1.1 |
|
| 220 |
1.1 |
|
| 222 |
1.1 2.2 |
|
| 225 |
1.1 |
|
| 226 |
1.1 |
|
| 229 |
1.1 |
|
| 231 |
1.1 |
|
| 232 |
1.1 |
|
| 254 |
1.1 |
|
| 279 |
1.1 |
|
| 280 |
1.1 |
|
| 287 |
1.1 |
|
| 288 |
1.1 |
|
| 289 |
1.1 |
|
| 290 |
1.1 |
|
| 293 |
1.1 |
|
| 294 |
1.1 |
|
| 297 |
1.1 |
|
| 299 |
1.1 |
|
| 303 |
1.1 |
|
| 305 |
1.1 |
|
| 308 |
1.1 |
|
| 324 |
1.1 |
|
| 351 |
1.1 |
|
| 352 |
1.1 |
|
| 366 |
1.1 |
|
| 408 |
1.1 |
|
| 422 |
1.1 |
|
| 439 |
1.1 |
|
| 445 |
1.1 2.2 |
|
| 449 |
1.1 |
|
| 452 |
1.1 |
|
| 476 |
1.1 2.2 |
|
| 477 |
1.1 2.2 3.3 4.4 |
|
| 484 |
1.1 |
|
| 486 |
1.1 |
|
| 491 |
1.1 |
|
| 492 |
1.1 |
|
| 504 |
1.1 |
|
| 507 |
1.1 2.2 |
|
| 511 |
1.1 |
|
| 512 |
1.1 |
|
| 513 |
1.1 |
|
| 514 |
1.1 |
|
| 537 |
1.1 |
|
| 539 |
1.1 |
|
| 540 |
1.1 |
|
| 541 |
1.1 |
|
| 545 |
1.1 |
|
| 569 |
1.1 |
|
| 572 |
1.1 |
|
| 573 |
1.1 |
|
| 574 |
1.1 |
|
| 575 |
1.1 |
|
| 578 |
1.1 |
|
| 579 |
1.1 |
|
| 580 |
1.1 |
|
| 581 |
1.1 |
|
| 586 |
1.1 |
|
| 596 |
1.1 |
|
| 597 |
1.1 |
|
| 607 |
1.1 |
|
| 609 |
1.1 |
|
| 612 |
1.1 |
|
| 623 |
1.1 |
|
| 625 |
1.1 |
|
| 628 |
1.1 |