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