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