| 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.http.HttpStatus; | |
| 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 | import org.springframework.web.server.ResponseStatusException; | |
| 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 CourseStaff 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 normalizedEmail = email.strip(); | |
| 63 | ||
| 64 |
1
1. postCourseStaff : negated conditional → KILLED |
if (courseStaffRepository.existsByCourseAndEmailIgnoreCase(course, normalizedEmail)) { |
| 65 | throw new ResponseStatusException( | |
| 66 | HttpStatus.CONFLICT, | |
| 67 | "A staff member with email %s already exists in this course".formatted(normalizedEmail)); | |
| 68 | } | |
| 69 | ||
| 70 | CourseStaff courseStaff = | |
| 71 | CourseStaff.builder() | |
| 72 | .firstName(firstName) | |
| 73 | .lastName(lastName) | |
| 74 | .email(normalizedEmail) | |
| 75 | .course(course) | |
| 76 | .build(); | |
| 77 | ||
| 78 |
1
1. postCourseStaff : negated conditional → KILLED |
if (course.getInstallationId() != null) { |
| 79 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.JOINCOURSE); |
| 80 | } else { | |
| 81 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.PENDING); |
| 82 | } | |
| 83 | ||
| 84 | CourseStaff savedCourseStaff = courseStaffRepository.save(courseStaff); | |
| 85 | ||
| 86 |
1
1. postCourseStaff : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToCourseStaff → KILLED |
updateUserService.attachUserToCourseStaff(savedCourseStaff); |
| 87 | ||
| 88 |
1
1. postCourseStaff : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::postCourseStaff → KILLED |
return savedCourseStaff; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * This method returns a list of course staff for a given course. | |
| 93 | * | |
| 94 | * @return a list of all courses. | |
| 95 | */ | |
| 96 | @Operation(summary = "List all course staff members for a course") | |
| 97 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 98 | @GetMapping("/course") | |
| 99 | public Iterable<CourseStaff> courseStaffForCourse( | |
| 100 | @Parameter(name = "courseId") @RequestParam Long courseId) throws EntityNotFoundException { | |
| 101 | courseRepository | |
| 102 | .findById(courseId) | |
| 103 |
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)); |
| 104 | Iterable<CourseStaff> courseStaffs = courseStaffRepository.findByCourseId(courseId); | |
| 105 |
1
1. courseStaffForCourse : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::courseStaffForCourse → KILLED |
return courseStaffs; |
| 106 | } | |
| 107 | ||
| 108 | @Operation( | |
| 109 | summary = | |
| 110 | "Allow staff member to join a course by generating an invitation to the linked Github Org") | |
| 111 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 112 | @PutMapping("/joinCourse") | |
| 113 | public ResponseEntity<String> joinCourseOnGitHub( | |
| 114 | @Parameter(name = "courseStaffId", description = "Staff Member joining a course on GitHub") | |
| 115 | @RequestParam | |
| 116 | Long courseStaffId) | |
| 117 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 118 | ||
| 119 | User currentUser = currentUserService.getUser(); | |
| 120 | CourseStaff courseStaff = | |
| 121 | courseStaffRepository | |
| 122 | .findById(courseStaffId) | |
| 123 |
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)); |
| 124 | ||
| 125 |
2
1. joinCourseOnGitHub : negated conditional → KILLED 2. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getUser() == null || currentUser.getId() != courseStaff.getUser().getId()) { |
| 126 | throw new IllegalArgumentException( | |
| 127 | String.format( | |
| 128 | "This operation is restricted to the user associated with staff member with id %d", | |
| 129 | courseStaff.getId())); | |
| 130 | } | |
| 131 | ||
| 132 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getGithubId() != null |
| 133 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
&& courseStaff.getGithubLogin() != null |
| 134 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
&& (courseStaff.getOrgStatus() == OrgStatus.MEMBER |
| 135 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
|| courseStaff.getOrgStatus() == OrgStatus.OWNER)) { |
| 136 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.badRequest() |
| 137 | .body("You have already linked a Github account to this course."); | |
| 138 | } | |
| 139 | ||
| 140 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (courseStaff.getCourse().getOrgName() == null |
| 141 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
|| courseStaff.getCourse().getInstallationId() == null) { |
| 142 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.badRequest() |
| 143 | .body("Course has not been set up. Please ask your instructor for help."); | |
| 144 | } | |
| 145 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubId → KILLED |
courseStaff.setGithubId(currentUser.getGithubId()); |
| 146 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubLogin → KILLED |
courseStaff.setGithubLogin(currentUser.getGithubLogin()); |
| 147 | OrgStatus status = organizationMemberService.inviteOrganizationOwner(courseStaff); | |
| 148 |
1
1. joinCourseOnGitHub : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(status); |
| 149 | courseStaffRepository.save(courseStaff); | |
| 150 |
1
1. joinCourseOnGitHub : negated conditional → KILLED |
if (status == OrgStatus.INVITED) { |
| 151 |
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"); |
| 152 |
2
1. joinCourseOnGitHub : negated conditional → KILLED 2. joinCourseOnGitHub : negated conditional → KILLED |
} else if (status == OrgStatus.MEMBER || status == OrgStatus.OWNER) { |
| 153 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.accepted() |
| 154 | .body("Already in organization - set status to %s".formatted(status.toString())); | |
| 155 | } else { | |
| 156 |
1
1. joinCourseOnGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::joinCourseOnGitHub → KILLED |
return ResponseEntity.internalServerError() |
| 157 | .body("Could not invite staff member to Organization"); | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | @Operation(summary = "Update a staff member") | |
| 162 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 163 | @PutMapping("") | |
| 164 | public CourseStaff updateStaffMember( | |
| 165 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 166 | @Parameter(name = "id") @RequestParam Long id, | |
| 167 | @Parameter(name = "firstName") @RequestParam String firstName, | |
| 168 | @Parameter(name = "lastName") @RequestParam String lastName) | |
| 169 | throws EntityNotFoundException { | |
| 170 | ||
| 171 | CourseStaff staffMember = | |
| 172 | courseStaffRepository | |
| 173 | .findById(id) | |
| 174 |
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)); |
| 175 | ||
| 176 |
1
1. updateStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setFirstName → KILLED |
staffMember.setFirstName(firstName.trim()); |
| 177 |
1
1. updateStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setLastName → KILLED |
staffMember.setLastName(lastName.trim()); |
| 178 |
1
1. updateStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::updateStaffMember → KILLED |
return courseStaffRepository.save(staffMember); |
| 179 | } | |
| 180 | ||
| 181 | @Operation(summary = "Delete a staff member") | |
| 182 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 183 | @DeleteMapping("/delete") | |
| 184 | @Transactional | |
| 185 | public ResponseEntity<String> deleteStaffMember( | |
| 186 | @Parameter(name = "id") @RequestParam Long id, | |
| 187 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 188 | @Parameter( | |
| 189 | name = "removeFromOrg", | |
| 190 | description = "Whether to remove course staff from GitHub organization") | |
| 191 | @RequestParam(defaultValue = "false") | |
| 192 | boolean removeFromOrg) | |
| 193 | throws EntityNotFoundException { | |
| 194 | CourseStaff staffMember = | |
| 195 | courseStaffRepository | |
| 196 | .findById(id) | |
| 197 |
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)); |
| 198 | Course course = staffMember.getCourse(); | |
| 199 | ||
| 200 | boolean orgRemovalAttempted = false; | |
| 201 | boolean orgRemovalSuccessful = false; | |
| 202 | String orgRemovalErrorMessage = null; | |
| 203 | ||
| 204 | // Try to remove the course staff from the organization if they have a GitHub login | |
| 205 |
1
1. deleteStaffMember : negated conditional → KILLED |
if (staffMember.getGithubLogin() != null |
| 206 |
1
1. deleteStaffMember : negated conditional → KILLED |
&& course.getOrgName() != null |
| 207 |
2
1. deleteStaffMember : negated conditional → KILLED 2. deleteStaffMember : negated conditional → KILLED |
&& course.getInstallationId() != null |
| 208 | && removeFromOrg) { | |
| 209 | orgRemovalAttempted = true; | |
| 210 | try { | |
| 211 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/services/OrganizationMemberService::removeOrganizationMember → KILLED |
organizationMemberService.removeOrganizationMember(staffMember); |
| 212 | orgRemovalSuccessful = true; | |
| 213 | } catch (Exception e) { | |
| 214 | log.error("Error removing course staff from organization: {}", e.getMessage()); | |
| 215 | orgRemovalErrorMessage = e.getMessage(); | |
| 216 | // Continue with deletion even if organization removal fails | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | course.getCourseStaff().remove(staffMember); | |
| 221 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setCourse → KILLED |
staffMember.setCourse(null); |
| 222 |
1
1. deleteStaffMember : removed call to edu/ucsb/cs156/frontiers/repositories/CourseStaffRepository::delete → KILLED |
courseStaffRepository.delete(staffMember); |
| 223 | ||
| 224 |
1
1. deleteStaffMember : negated conditional → KILLED |
if (!orgRemovalAttempted) { |
| 225 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 226 | "Successfully deleted staff member and removed them from the staff roster."); | |
| 227 |
1
1. deleteStaffMember : negated conditional → KILLED |
} else if (orgRemovalSuccessful) { |
| 228 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 229 | "Successfully deleted staff member and removed them from the staff roster and organization."); | |
| 230 | } else { | |
| 231 |
1
1. deleteStaffMember : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffController::deleteStaffMember → KILLED |
return ResponseEntity.ok( |
| 232 | "Successfully deleted staff member but there was an error removing them from the course organization: " | |
| 233 | + orgRemovalErrorMessage); | |
| 234 | } | |
| 235 | } | |
| 236 | } | |
Mutations | ||
| 60 |
1.1 |
|
| 64 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 81 |
1.1 |
|
| 86 |
1.1 |
|
| 88 |
1.1 |
|
| 103 |
1.1 |
|
| 105 |
1.1 |
|
| 123 |
1.1 |
|
| 125 |
1.1 2.2 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |
|
| 136 |
1.1 |
|
| 140 |
1.1 |
|
| 141 |
1.1 |
|
| 142 |
1.1 |
|
| 145 |
1.1 |
|
| 146 |
1.1 |
|
| 148 |
1.1 |
|
| 150 |
1.1 |
|
| 151 |
1.1 |
|
| 152 |
1.1 2.2 |
|
| 153 |
1.1 |
|
| 156 |
1.1 |
|
| 174 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 197 |
1.1 |
|
| 205 |
1.1 |
|
| 206 |
1.1 |
|
| 207 |
1.1 2.2 |
|
| 211 |
1.1 |
|
| 221 |
1.1 |
|
| 222 |
1.1 |
|
| 224 |
1.1 |
|
| 225 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 231 |
1.1 |