| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.*; | |
| 5 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 6 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 9 | import edu.ucsb.cs156.frontiers.services.*; | |
| 10 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 11 | import io.swagger.v3.oas.annotations.Operation; | |
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 14 | import java.security.NoSuchAlgorithmException; | |
| 15 | import java.security.spec.InvalidKeySpecException; | |
| 16 | import lombok.extern.slf4j.Slf4j; | |
| 17 | import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | import org.springframework.http.ResponseEntity; | |
| 19 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 20 | import org.springframework.transaction.annotation.Transactional; | |
| 21 | import org.springframework.web.bind.annotation.*; | |
| 22 | ||
| 23 | @Tag(name = "CourseStaff") | |
| 24 | @RequestMapping("/api/coursestaff") | |
| 25 | @RestController | |
| 26 | @Slf4j | |
| 27 | public class CourseStaffController extends ApiController { | |
| 28 | ||
| 29 | @Autowired private OrganizationMemberService organizationMemberService; | |
| 30 | ||
| 31 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 32 | ||
| 33 | @Autowired private CourseRepository courseRepository; | |
| 34 | ||
| 35 | @Autowired private UpdateUserService updateUserService; | |
| 36 | ||
| 37 | @Autowired private CurrentUserService currentUserService; | |
| 38 | ||
| 39 | /** | |
| 40 | * This method creates a new CourseStaff. | |
| 41 | * | |
| 42 | * @return the created CourseStaff | |
| 43 | */ | |
| 44 | @Operation(summary = "Add a staff member to a course") | |
| 45 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 46 | @PostMapping("/post") | |
| 47 | public CourseStaff postCourseStaff( | |
| 48 | @Parameter(name = "firstName") @RequestParam String firstName, | |
| 49 | @Parameter(name = "lastName") @RequestParam String lastName, | |
| 50 | @Parameter(name = "email") @RequestParam String email, | |
| 51 | @Parameter(name = "courseId") @RequestParam Long courseId) | |
| 52 | throws EntityNotFoundException { | |
| 53 | ||
| 54 | // Get Course or else throw an error | |
| 55 | ||
| 56 | Course course = | |
| 57 | courseRepository | |
| 58 | .findById(courseId) | |
| 59 |
1
1. lambda$postCourseStaff$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::lambda$postCourseStaff$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 60 | ||
| 61 | String sanitizedEmail = CanonicalFormConverter.convertToValidEmail(email); | |
| 62 | ||
| 63 | CourseStaff courseStaff = | |
| 64 | CourseStaff.builder() | |
| 65 | .firstName(firstName) | |
| 66 | .lastName(lastName) | |
| 67 | .email(sanitizedEmail) | |
| 68 | .course(course) | |
| 69 | .build(); | |
| 70 | ||
| 71 |
1
1. postCourseStaff : negated conditional → KILLED |
if (course.getInstallationId() != null) { |
| 72 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.JOINCOURSE); |
| 73 | } else { | |
| 74 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.PENDING); |
| 75 | } | |
| 76 | ||
| 77 | CourseStaff savedCourseStaff = courseStaffRepository.save(courseStaff); | |
| 78 | ||
| 79 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToCourseStaff → KILLED |
updateUserService.attachUserToCourseStaff(savedCourseStaff); |
| 80 | ||
| 81 |
1
1. postCourseStaff : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::postCourseStaff → KILLED |
return savedCourseStaff; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * This method returns a list of course staff for a given course. | |
| 86 | * | |
| 87 | * @return a list of all courses. | |
| 88 | */ | |
| 89 | @Operation(summary = "List all course staff members for a course") | |
| 90 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 91 | @GetMapping("/course") | |
| 92 | public Iterable<CourseStaff> courseStaffForCourse( | |
| 93 | @Parameter(name = "courseId") @RequestParam Long courseId) throws EntityNotFoundException { | |
| 94 | courseRepository | |
| 95 | .findById(courseId) | |
| 96 |
1
1. lambda$courseStaffForCourse$1 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::lambda$courseStaffForCourse$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 97 | Iterable<CourseStaff> courseStaffs = courseStaffRepository.findByCourseId(courseId); | |
| 98 |
1
1. courseStaffForCourse : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::courseStaffForCourse → KILLED |
return courseStaffs; |
| 99 | } | |
| 100 | ||
| 101 | @Operation( | |
| 102 | summary = | |
| 103 | "Allow staff member to join a course by generating an invitation to the linked Github Org") | |
| 104 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 105 | @PutMapping("/joinCourse") | |
| 106 | public ResponseEntity<String> joinCourseOnGitHub( | |
| 107 | @Parameter(name = "courseStaffId", description = "Staff Member joining a course on GitHub") | |
| 108 | @RequestParam | |
| 109 | Long courseStaffId) | |
| 110 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 111 | ||
| 112 | User currentUser = currentUserService.getUser(); | |
| 113 | CourseStaff courseStaff = | |
| 114 | courseStaffRepository | |
| 115 | .findById(courseStaffId) | |
| 116 |
1
1. lambda$joinCourseOnGitHub$2 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::lambda$joinCourseOnGitHub$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(CourseStaff.class, courseStaffId)); |
| 117 | ||
| 118 |
2
1. joinCourseOnGitHub : negated conditional → KILLED 2. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getUser() == null || currentUser.getId() != courseStaff.getUser().getId()) { |
| 119 | throw new IllegalArgumentException( | |
| 120 | String.format( | |
| 121 | "This operation is restricted to the user associated with staff member with id %d", | |
| 122 | courseStaff.getId())); | |
| 123 | } | |
| 124 | ||
| 125 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getGithubId() != null |
| 126 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
&& courseStaff.getGithubLogin() != null |
| 127 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
&& (courseStaff.getOrgStatus() == OrgStatus.MEMBER |
| 128 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
|| courseStaff.getOrgStatus() == OrgStatus.OWNER)) { |
| 129 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.badRequest() |
| 130 | .body("You have already linked a Github account to this course."); | |
| 131 | } | |
| 132 | ||
| 133 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getCourse().getOrgName() == null |
| 134 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
|| courseStaff.getCourse().getInstallationId() == null) { |
| 135 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.badRequest() |
| 136 | .body("Course has not been set up. Please ask your instructor for help."); | |
| 137 | } | |
| 138 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubId → KILLED |
courseStaff.setGithubId(currentUser.getGithubId()); |
| 139 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubLogin → KILLED |
courseStaff.setGithubLogin(currentUser.getGithubLogin()); |
| 140 | OrgStatus status = organizationMemberService.inviteOrganizationOwner(courseStaff); | |
| 141 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(status); |
| 142 | courseStaffRepository.save(courseStaff); | |
| 143 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (status == OrgStatus.INVITED) { |
| 144 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.accepted().body("Successfully invited staff member to Organization"); |
| 145 |
2
1. joinCourseOnGitHub : negated conditional → KILLED 2. joinCourseOnGitHub : negated conditional → KILLED |
} else if (status == OrgStatus.MEMBER || status == OrgStatus.OWNER) { |
| 146 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.accepted() |
| 147 | .body("Already in organization - set status to %s".formatted(status.toString())); | |
| 148 | } else { | |
| 149 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.internalServerError() |
| 150 | .body("Could not invite staff member to Organization"); | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | @Operation(summary = "Update a staff member") | |
| 155 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 156 | @PutMapping("") | |
| 157 | public CourseStaff updateStaffMember( | |
| 158 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 159 | @Parameter(name = "id") @RequestParam Long id, | |
| 160 | @Parameter(name = "firstName") @RequestParam String firstName, | |
| 161 | @Parameter(name = "lastName") @RequestParam String lastName) | |
| 162 | throws EntityNotFoundException { | |
| 163 | ||
| 164 | CourseStaff staffMember = | |
| 165 | courseStaffRepository | |
| 166 | .findById(id) | |
| 167 |
1
1. lambda$updateStaffMember$3 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::lambda$updateStaffMember$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(CourseStaff.class, id)); |
| 168 | ||
| 169 |
1
1. updateStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setFirstName → KILLED |
staffMember.setFirstName(firstName.trim()); |
| 170 |
1
1. updateStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setLastName → KILLED |
staffMember.setLastName(lastName.trim()); |
| 171 |
1
1. updateStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::updateStaffMember → KILLED |
return courseStaffRepository.save(staffMember); |
| 172 | } | |
| 173 | ||
| 174 | @Operation(summary = "Delete a staff member") | |
| 175 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 176 | @DeleteMapping("/delete") | |
| 177 | @Transactional | |
| 178 | public ResponseEntity<String> deleteStaffMember( | |
| 179 | @Parameter(name = "id") @RequestParam Long id, | |
| 180 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 181 | @Parameter( | |
| 182 | name = "removeFromOrg", | |
| 183 | description = "Whether to remove staff member from GitHub organization") | |
| 184 | @RequestParam(defaultValue = "false") | |
| 185 | boolean removeFromOrg) | |
| 186 | throws EntityNotFoundException { | |
| 187 | CourseStaff staffMember = | |
| 188 | courseStaffRepository | |
| 189 | .findById(id) | |
| 190 |
1
1. lambda$deleteStaffMember$4 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::lambda$deleteStaffMember$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(CourseStaff.class, id)); |
| 191 | ||
| 192 | Course course = staffMember.getCourse(); | |
| 193 | ||
| 194 | boolean orgRemovalAttempted = false; | |
| 195 | boolean orgRemovalSuccessful = false; | |
| 196 | String orgRemovalErrorMessage = null; | |
| 197 | ||
| 198 | // Try to remove the staff member from the organization if they have a GitHub login | |
| 199 | // and removeFromOrg parameter is true | |
| 200 |
1
1. deleteStaffMember : negated conditional → KILLED |
if (removeFromOrg |
| 201 |
1
1. deleteStaffMember : negated conditional → KILLED |
&& staffMember.getGithubLogin() != null |
| 202 |
1
1. deleteStaffMember : negated conditional → KILLED |
&& course.getOrgName() != null |
| 203 |
1
1. deleteStaffMember : negated conditional → KILLED |
&& course.getInstallationId() != null) { |
| 204 | orgRemovalAttempted = true; | |
| 205 | try { | |
| 206 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/services/OrganizationMemberService::removeOrganizationMember → KILLED |
organizationMemberService.removeOrganizationMember(staffMember); |
| 207 | orgRemovalSuccessful = true; | |
| 208 | } catch (Exception e) { | |
| 209 | log.error("Error removing staff member from organization: {}", e.getMessage()); | |
| 210 | orgRemovalErrorMessage = e.getMessage(); | |
| 211 | // Continue with deletion even if organization removal fails | |
| 212 | } | |
| 213 | } | |
| 214 | ||
| 215 | course.getCourseStaff().remove(staffMember); | |
| 216 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setCourse → KILLED |
staffMember.setCourse(null); |
| 217 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/repositories/CourseStaffRepository::delete → KILLED |
courseStaffRepository.delete(staffMember); |
| 218 | ||
| 219 |
1
1. deleteStaffMember : negated conditional → KILLED |
if (!orgRemovalAttempted) { |
| 220 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 221 | "Successfully deleted staff member and removed them from the staff roster."); | |
| 222 |
1
1. deleteStaffMember : negated conditional → KILLED |
} else if (orgRemovalSuccessful) { |
| 223 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 224 | "Successfully deleted staff member and removed them from the staff roster and organization."); | |
| 225 | } else { | |
| 226 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 227 | "Successfully deleted staff member but there was an error removing them from the course organization: " | |
| 228 | + orgRemovalErrorMessage); | |
| 229 | } | |
| 230 | } | |
| 231 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 81 |
1.1 |
|
| 96 |
1.1 |
|
| 98 |
1.1 |
|
| 116 |
1.1 |
|
| 118 |
1.1 2.2 |
|
| 125 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 |
|
| 141 |
1.1 |
|
| 143 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 2.2 |
|
| 146 |
1.1 |
|
| 149 |
1.1 |
|
| 167 |
1.1 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 190 |
1.1 |
|
| 200 |
1.1 |
|
| 201 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 206 |
1.1 |
|
| 216 |
1.1 |
|
| 217 |
1.1 |
|
| 219 |
1.1 |
|
| 220 |
1.1 |
|
| 222 |
1.1 |
|
| 223 |
1.1 |
|
| 226 |
1.1 |