1 | package edu.ucsb.cs156.frontiers.controllers; | |
2 | ||
3 | import java.io.BufferedInputStream; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.io.InputStreamReader; | |
7 | import java.security.NoSuchAlgorithmException; | |
8 | import java.security.spec.InvalidKeySpecException; | |
9 | import java.util.List; | |
10 | import java.util.Map; | |
11 | import java.util.Optional; | |
12 | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | |
14 | import org.springframework.http.HttpStatus; | |
15 | import org.springframework.http.ResponseEntity; | |
16 | import org.springframework.security.access.AccessDeniedException; | |
17 | import org.springframework.security.access.prepost.PreAuthorize; | |
18 | import org.springframework.web.bind.annotation.DeleteMapping; | |
19 | import org.springframework.web.bind.annotation.GetMapping; | |
20 | import org.springframework.web.bind.annotation.PostMapping; | |
21 | import org.springframework.web.bind.annotation.PutMapping; | |
22 | import org.springframework.web.bind.annotation.RequestMapping; | |
23 | import org.springframework.web.bind.annotation.RequestParam; | |
24 | import org.springframework.web.bind.annotation.RestController; | |
25 | import org.springframework.web.multipart.MultipartFile; | |
26 | import org.springframework.web.server.ResponseStatusException; | |
27 | ||
28 | import com.fasterxml.jackson.core.JsonProcessingException; | |
29 | import com.opencsv.CSVReader; | |
30 | import com.opencsv.exceptions.CsvException; | |
31 | ||
32 | import edu.ucsb.cs156.frontiers.entities.Course; | |
33 | import edu.ucsb.cs156.frontiers.entities.Job; | |
34 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
35 | import edu.ucsb.cs156.frontiers.entities.User; | |
36 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
37 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
38 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
39 | import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException; | |
40 | import edu.ucsb.cs156.frontiers.jobs.UpdateOrgMembershipJob; | |
41 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
42 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
43 | import edu.ucsb.cs156.frontiers.services.CurrentUserService; | |
44 | import edu.ucsb.cs156.frontiers.services.OrganizationMemberService; | |
45 | import edu.ucsb.cs156.frontiers.services.UpdateUserService; | |
46 | import edu.ucsb.cs156.frontiers.services.jobs.JobService; | |
47 | import io.swagger.v3.oas.annotations.Operation; | |
48 | import io.swagger.v3.oas.annotations.Parameter; | |
49 | import io.swagger.v3.oas.annotations.tags.Tag; | |
50 | import lombok.extern.slf4j.Slf4j; | |
51 | ||
52 | @Tag(name = "RosterStudents") | |
53 | @RequestMapping("/api/rosterstudents") | |
54 | @RestController | |
55 | @Slf4j | |
56 | public class RosterStudentsController extends ApiController { | |
57 | ||
58 | @Autowired | |
59 | private JobService jobService; | |
60 | @Autowired | |
61 | private OrganizationMemberService organizationMemberService; | |
62 | ||
63 | public enum InsertStatus { | |
64 | INSERTED, UPDATED | |
65 | }; | |
66 | ||
67 | @Autowired | |
68 | private RosterStudentRepository rosterStudentRepository; | |
69 | ||
70 | @Autowired | |
71 | private CourseRepository courseRepository; | |
72 | ||
73 | @Autowired | |
74 | private UpdateUserService updateUserService; | |
75 | ||
76 | @Autowired | |
77 | private CurrentUserService currentUserService; | |
78 | ||
79 | /** | |
80 | * This method creates a new RosterStudent. | |
81 | * | |
82 | * | |
83 | * @return the created RosterStudent | |
84 | */ | |
85 | ||
86 | @Operation(summary = "Create a new roster student") | |
87 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
88 | @PostMapping("/post") | |
89 | public RosterStudent postRosterStudent( | |
90 | @Parameter(name = "studentId") @RequestParam String studentId, | |
91 | @Parameter(name = "firstName") @RequestParam String firstName, | |
92 | @Parameter(name = "lastName") @RequestParam String lastName, | |
93 | @Parameter(name = "email") @RequestParam String email, | |
94 | @Parameter(name = "courseId") @RequestParam Long courseId) throws EntityNotFoundException, ResponseStatusException { | |
95 | ||
96 | // Get Course or else throw an error | |
97 | ||
98 | Course course = courseRepository.findById(courseId) | |
99 |
1
1. lambda$postRosterStudent$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$postRosterStudent$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
100 | ||
101 | Optional<RosterStudent> existing = rosterStudentRepository.findByCourseIdAndStudentId(courseId, studentId); | |
102 |
1
1. postRosterStudent : negated conditional → KILLED |
if (existing.isPresent()) { |
103 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Another student in this course already has student ID " + studentId + "."); | |
104 | } | |
105 | ||
106 | RosterStudent rosterStudent = RosterStudent.builder() | |
107 | .studentId(studentId) | |
108 | .firstName(firstName) | |
109 | .lastName(lastName) | |
110 | .email(email) | |
111 | .course(course) | |
112 | .rosterStatus(RosterStatus.MANUAL) | |
113 | .orgStatus(OrgStatus.NONE) | |
114 | .build(); | |
115 | RosterStudent savedRosterStudent = rosterStudentRepository.save(rosterStudent); | |
116 | ||
117 |
1
1. postRosterStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::postRosterStudent → KILLED |
return savedRosterStudent; |
118 | } | |
119 | ||
120 | /** | |
121 | * This method updates a RosterStudent. | |
122 | * | |
123 | * | |
124 | * @return the updated RosterStudent | |
125 | */ | |
126 | @Operation(summary = "Update a Roster Student") | |
127 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
128 | @PutMapping("/update") | |
129 | public RosterStudent updateRosterStudent( | |
130 | @Parameter(name = "id") @RequestParam Long id, | |
131 | @Parameter(name = "firstName") @RequestParam String firstName, | |
132 | @Parameter(name = "lastName") @RequestParam String lastName, | |
133 | @Parameter(name = "studentId") @RequestParam String studentId) throws EntityNotFoundException, ResponseStatusException { | |
134 | ||
135 | RosterStudent rosterStudent = rosterStudentRepository.findById(id) | |
136 |
1
1. lambda$updateRosterStudent$1 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$updateRosterStudent$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
137 | ||
138 | Long courseId = rosterStudent.getCourse().getId(); | |
139 | ||
140 | Optional<RosterStudent> existing = rosterStudentRepository.findByCourseIdAndStudentId(courseId, studentId); | |
141 |
1
1. updateRosterStudent : negated conditional → KILLED |
if (existing.isPresent()) { |
142 |
1
1. updateRosterStudent : negated conditional → KILLED |
if (!existing.get().getId().equals(id)) { |
143 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Another student in this course already has student ID " + studentId + "."); | |
144 | } | |
145 | } | |
146 | ||
147 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setFirstName → KILLED |
rosterStudent.setFirstName(firstName); |
148 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setLastName → KILLED |
rosterStudent.setLastName(lastName); |
149 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setStudentId → KILLED |
rosterStudent.setStudentId(studentId); |
150 | ||
151 |
1
1. updateRosterStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::updateRosterStudent → KILLED |
return rosterStudentRepository.save(rosterStudent); |
152 | } | |
153 | ||
154 | /* | |
155 | * This method deletes an existing RosterStudent. | |
156 | * | |
157 | * @param id the id of the roster student to delete | |
158 | * @return a message indicating the RosterStudent was deleted | |
159 | */ | |
160 | ||
161 | @Operation(summary = "Delete a roster student") | |
162 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
163 | @DeleteMapping("") | |
164 | public Object deleteRosterStudent( | |
165 | @Parameter(name="id") @RequestParam Long id) { | |
166 | RosterStudent rosterStudent = rosterStudentRepository.findById(id) | |
167 |
1
1. lambda$deleteRosterStudent$2 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$deleteRosterStudent$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
168 | ||
169 |
1
1. deleteRosterStudent : removed call to edu/ucsb/cs156/frontiers/repositories/RosterStudentRepository::delete → KILLED |
rosterStudentRepository.delete(rosterStudent); |
170 |
1
1. deleteRosterStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::deleteRosterStudent → KILLED |
return genericMessage("RosterStudent with id %s deleted".formatted(id)); |
171 | } | |
172 | ||
173 | /** | |
174 | * This method returns a list of roster students for a given course. | |
175 | * | |
176 | * @return a list of all courses. | |
177 | */ | |
178 | @Operation(summary = "List all roster students for a course") | |
179 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
180 | @GetMapping("/course") | |
181 | public Iterable<RosterStudent> rosterStudentForCourse( | |
182 | @Parameter(name = "courseId") @RequestParam Long courseId) throws EntityNotFoundException { | |
183 |
1
1. lambda$rosterStudentForCourse$3 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$rosterStudentForCourse$3 → KILLED |
courseRepository.findById(courseId).orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
184 | Iterable<RosterStudent> rosterStudents = rosterStudentRepository.findByCourseId(courseId); | |
185 |
1
1. rosterStudentForCourse : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::rosterStudentForCourse → KILLED |
return rosterStudents; |
186 | } | |
187 | ||
188 | @Operation(summary = "Upload Roster students for Course in UCSB Egrades Format") | |
189 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
190 | @PostMapping(value = "/upload/egrades", consumes = { "multipart/form-data" }) | |
191 | public Map<String, String> uploadRosterStudents( | |
192 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
193 | @Parameter(name = "file") @RequestParam("file") MultipartFile file) | |
194 | throws JsonProcessingException, IOException, CsvException { | |
195 | ||
196 | Course course = courseRepository.findById(courseId) | |
197 |
1
1. lambda$uploadRosterStudents$4 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$uploadRosterStudents$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
198 | ||
199 | int counts[] = { 0, 0 }; | |
200 | ||
201 | try (InputStream inputStream = new BufferedInputStream(file.getInputStream()); | |
202 | InputStreamReader reader = new InputStreamReader(inputStream); | |
203 | CSVReader csvReader = new CSVReader(reader);) { | |
204 |
1
1. uploadRosterStudents : removed call to com/opencsv/CSVReader::skip → KILLED |
csvReader.skip(2); |
205 | List<String[]> myEntries = csvReader.readAll(); | |
206 | for (String[] row : myEntries) { | |
207 | RosterStudent rosterStudent = fromEgradesCSVRow(row); | |
208 | InsertStatus s = upsertStudent(rosterStudent, course); | |
209 |
1
1. uploadRosterStudents : Replaced integer addition with subtraction → KILLED |
counts[s.ordinal()]++; |
210 | } | |
211 | } | |
212 |
1
1. uploadRosterStudents : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::uploadRosterStudents → KILLED |
return Map.of( |
213 | "filename", file.getOriginalFilename(), | |
214 | "message", String.format("Inserted %d new students, Updated %d students", | |
215 | counts[InsertStatus.INSERTED.ordinal()], counts[InsertStatus.UPDATED.ordinal()])); | |
216 | ||
217 | } | |
218 | ||
219 | public RosterStudent fromEgradesCSVRow(String[] row) { | |
220 |
1
1. fromEgradesCSVRow : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::fromEgradesCSVRow → KILLED |
return RosterStudent.builder() |
221 | .firstName(row[5]) | |
222 | .lastName(row[4]) | |
223 | .studentId(row[1]) | |
224 | .email(row[10]) | |
225 | .build(); | |
226 | } | |
227 | ||
228 | public InsertStatus upsertStudent(RosterStudent student, Course course) { | |
229 | Optional<RosterStudent> existingStudent = rosterStudentRepository.findByCourseIdAndStudentId(course.getId(), | |
230 | student.getStudentId()); | |
231 | String convertedEmail = student.getEmail().replace("@umail.ucsb.edu","@ucsb.edu"); | |
232 |
1
1. upsertStudent : negated conditional → KILLED |
if (existingStudent.isPresent()) { |
233 | RosterStudent existingStudentObj = existingStudent.get(); | |
234 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setRosterStatus → KILLED |
existingStudentObj.setRosterStatus(RosterStatus.ROSTER); |
235 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setFirstName → KILLED |
existingStudentObj.setFirstName(student.getFirstName()); |
236 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setLastName → KILLED |
existingStudentObj.setLastName(student.getLastName()); |
237 |
1
1. upsertStudent : negated conditional → KILLED |
if (!existingStudentObj.getEmail().equals(convertedEmail)) { |
238 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setEmail → KILLED |
existingStudentObj.setEmail(convertedEmail); |
239 | } | |
240 | existingStudentObj = rosterStudentRepository.save(existingStudentObj); | |
241 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToRosterStudent → KILLED |
updateUserService.attachUserToRosterStudent(existingStudentObj); |
242 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::upsertStudent → KILLED |
return InsertStatus.UPDATED; |
243 | } else { | |
244 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setCourse → KILLED |
student.setCourse(course); |
245 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setEmail → KILLED |
student.setEmail(convertedEmail); |
246 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setRosterStatus → KILLED |
student.setRosterStatus(RosterStatus.ROSTER); |
247 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED |
student.setOrgStatus(OrgStatus.NONE); |
248 | student = rosterStudentRepository.save(student); | |
249 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToRosterStudent → KILLED |
updateUserService.attachUserToRosterStudent(student); |
250 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::upsertStudent → KILLED |
return InsertStatus.INSERTED; |
251 | } | |
252 | } | |
253 | ||
254 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
255 | @PostMapping("/updateCourseMembership") | |
256 | public Job updateCourseMembership(@Parameter(name = "courseId", description = "Course ID") @RequestParam Long courseId) throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
257 |
1
1. lambda$updateCourseMembership$5 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$updateCourseMembership$5 → KILLED |
Course course = courseRepository.findById(courseId).orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
258 |
2
1. updateCourseMembership : negated conditional → KILLED 2. updateCourseMembership : negated conditional → KILLED |
if(course.getInstallationId() == null || course.getOrgName() == null){ |
259 | throw new NoLinkedOrganizationException(course.getCourseName()); | |
260 | }else{ | |
261 | UpdateOrgMembershipJob job = UpdateOrgMembershipJob.builder() | |
262 | .rosterStudentRepository(rosterStudentRepository) | |
263 | .organizationMemberService(organizationMemberService) | |
264 | .course(course) | |
265 | .build(); | |
266 | ||
267 |
1
1. updateCourseMembership : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::updateCourseMembership → KILLED |
return jobService.runAsJob(job); |
268 | } | |
269 | } | |
270 | | |
271 | @Operation(summary = "Link a Roster Student to a Github Account") | |
272 | @PreAuthorize("hasRole('ROLE_USER')") | |
273 | @PutMapping("/linkGitHub") | |
274 | public ResponseEntity<String> linkGitHub(@Parameter(name = "rosterStudentId", description = "Roster Student to be linked to") @RequestParam Long rosterStudentId){ | |
275 | User currentUser = currentUserService.getUser(); | |
276 | RosterStudent rosterStudent = rosterStudentRepository.findById(rosterStudentId) | |
277 |
1
1. lambda$linkGitHub$6 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$linkGitHub$6 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, rosterStudentId)); |
278 | ||
279 |
1
1. linkGitHub : negated conditional → KILLED |
if (currentUser.getId() != rosterStudent.getUser().getId()) { |
280 | throw new AccessDeniedException("User not authorized to link this roster student"); | |
281 | } | |
282 | ||
283 |
2
1. linkGitHub : negated conditional → KILLED 2. linkGitHub : negated conditional → KILLED |
if (rosterStudent.getGithubId() != 0 && rosterStudent.getGithubLogin() != null ) { |
284 |
1
1. linkGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::linkGitHub → KILLED |
return ResponseEntity.badRequest().body("This roster student is already linked to a GitHub account"); |
285 | } | |
286 | ||
287 |
1
1. linkGitHub : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubId → KILLED |
rosterStudent.setGithubId(currentUser.getGithubId()); |
288 |
1
1. linkGitHub : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubLogin → KILLED |
rosterStudent.setGithubLogin(currentUser.getGithubLogin()); |
289 | rosterStudentRepository.save(rosterStudent); | |
290 |
1
1. linkGitHub : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::linkGitHub → KILLED |
return ResponseEntity.ok("Successfully linked GitHub account to roster student"); |
291 | } | |
292 | ||
293 | @Operation(summary = "Get Associated Roster Students with a User") | |
294 | @PreAuthorize("hasRole('ROLE_USER')") | |
295 | @GetMapping("/associatedRosterStudents") | |
296 | public Iterable<RosterStudent> getAssociatedRosterStudents(){ | |
297 | User currentUser = currentUserService.getUser(); | |
298 | Iterable<RosterStudent> rosterStudents = rosterStudentRepository.findAllByUser((currentUser)); | |
299 |
1
1. getAssociatedRosterStudents : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::getAssociatedRosterStudents → KILLED |
return rosterStudents; |
300 | } | |
301 | } | |
Mutations | ||
99 |
1.1 |
|
102 |
1.1 |
|
117 |
1.1 |
|
136 |
1.1 |
|
141 |
1.1 |
|
142 |
1.1 |
|
147 |
1.1 |
|
148 |
1.1 |
|
149 |
1.1 |
|
151 |
1.1 |
|
167 |
1.1 |
|
169 |
1.1 |
|
170 |
1.1 |
|
183 |
1.1 |
|
185 |
1.1 |
|
197 |
1.1 |
|
204 |
1.1 |
|
209 |
1.1 |
|
212 |
1.1 |
|
220 |
1.1 |
|
232 |
1.1 |
|
234 |
1.1 |
|
235 |
1.1 |
|
236 |
1.1 |
|
237 |
1.1 |
|
238 |
1.1 |
|
241 |
1.1 |
|
242 |
1.1 |
|
244 |
1.1 |
|
245 |
1.1 |
|
246 |
1.1 |
|
247 |
1.1 |
|
249 |
1.1 |
|
250 |
1.1 |
|
257 |
1.1 |
|
258 |
1.1 2.2 |
|
267 |
1.1 |
|
277 |
1.1 |
|
279 |
1.1 |
|
283 |
1.1 2.2 |
|
284 |
1.1 |
|
287 |
1.1 |
|
288 |
1.1 |
|
290 |
1.1 |
|
299 |
1.1 |