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