| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | import java.io.BufferedInputStream; | |
| 3 | import java.io.IOException; | |
| 4 | import java.io.InputStream; | |
| 5 | import java.io.InputStreamReader; | |
| 6 | import java.security.NoSuchAlgorithmException; | |
| 7 | import java.security.spec.InvalidKeySpecException; | |
| 8 | import java.util.List; | |
| 9 | import java.util.Map; | |
| 10 | import java.util.Optional; | |
| 11 | ||
| 12 | import edu.ucsb.cs156.frontiers.entities.Job; | |
| 13 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 14 | import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException; | |
| 15 | import edu.ucsb.cs156.frontiers.jobs.UpdateOrgMembershipJob; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.services.*; | |
| 18 | import edu.ucsb.cs156.frontiers.services.jobs.JobService; | |
| 19 | import org.apache.coyote.BadRequestException; | |
| 20 | import org.springframework.beans.factory.annotation.Autowired; | |
| 21 | import org.springframework.http.ResponseEntity; | |
| 22 | import org.springframework.security.access.AccessDeniedException; | |
| 23 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 24 | import org.springframework.web.bind.annotation.*; | |
| 25 | import org.springframework.web.multipart.MultipartFile; | |
| 26 | ||
| 27 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 28 | ||
| 29 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 30 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 31 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 32 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 33 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 34 | import edu.ucsb.cs156.frontiers.errors.InvalidRequestException; | |
| 35 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 36 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 37 | import edu.ucsb.cs156.frontiers.dto.RosterStudentUpdateDTO; | |
| 38 | import io.swagger.v3.oas.annotations.Operation; | |
| 39 | import io.swagger.v3.oas.annotations.Parameter; | |
| 40 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 41 | import lombok.extern.slf4j.Slf4j; | |
| 42 | ||
| 43 | import com.opencsv.CSVReader; | |
| 44 | import com.opencsv.exceptions.CsvException; | |
| 45 | ||
| 46 | import jakarta.validation.Valid; | |
| 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 | /** | |
| 127 | * This method updates a student from a list of roster students. | |
| 128 | * | |
| 129 | * @param id id of the student object | |
| 130 | * @param incoming the new student | |
| 131 | * @return the updated student object. | |
| 132 | */ | |
| 133 | @Operation(summary= "Update a roster student") | |
| 134 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 135 | @PutMapping("/updateStudent") | |
| 136 | public RosterStudent updateRosterStudent( | |
| 137 | @Parameter(name="id") @RequestParam Long id, | |
| 138 | @RequestBody @Valid RosterStudentUpdateDTO incoming) { | |
| 139 | ||
| 140 | RosterStudent rosterStudent = rosterStudentRepository.findById(id) | |
| 141 |
1
1. lambda$updateRosterStudent$2 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$updateRosterStudent$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
| 142 | ||
| 143 | Optional<RosterStudent> duplicate = rosterStudentRepository.findByStudentId(incoming.getStudentId()); | |
| 144 |
2
1. updateRosterStudent : negated conditional → KILLED 2. updateRosterStudent : negated conditional → KILLED |
if (duplicate.isPresent() && !duplicate.get().getId().equals(id)) { |
| 145 | throw new InvalidRequestException("studentId " + incoming.getStudentId() + " already exists"); | |
| 146 | } | |
| 147 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setFirstName → KILLED |
rosterStudent.setFirstName(incoming.getFirstName()); |
| 148 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setLastName → KILLED |
rosterStudent.setLastName(incoming.getLastName()); |
| 149 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setStudentId → KILLED |
rosterStudent.setStudentId(incoming.getStudentId()); |
| 150 | ||
| 151 | rosterStudentRepository.save(rosterStudent); | |
| 152 | ||
| 153 |
1
1. updateRosterStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::updateRosterStudent → KILLED |
return rosterStudent; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * This method deletes a RosterStudent | |
| 158 | * | |
| 159 | * @param id id of the student object | |
| 160 | * @return a message indicating the student was deleted | |
| 161 | */ | |
| 162 | @Operation(summary= "Delete a RosterStudent") | |
| 163 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 164 | @DeleteMapping("") | |
| 165 | public Object deleteRosterStudent( | |
| 166 | @Parameter(name="id") @RequestParam Long id) { | |
| 167 | ||
| 168 | RosterStudent rosterStudent = rosterStudentRepository.findById(id) | |
| 169 |
1
1. lambda$deleteRosterStudent$3 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::lambda$deleteRosterStudent$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
| 170 | ||
| 171 |
1
1. deleteRosterStudent : removed call to edu/ucsb/cs156/frontiers/repositories/RosterStudentRepository::delete → KILLED |
rosterStudentRepository.delete(rosterStudent); |
| 172 | ||
| 173 | Course course = rosterStudent.getCourse(); | |
| 174 | course.getRosterStudents().remove(rosterStudent); // remove from list | |
| 175 | courseRepository.save(course); | |
| 176 |
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)); |
| 177 | } | |
| 178 | ||
| 179 | @Operation(summary = "Upload Roster students for Course in UCSB Egrades Format") | |
| 180 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 181 | @PostMapping(value = "/upload/egrades", consumes = { "multipart/form-data" }) | |
| 182 | public Map<String, String> uploadRosterStudents( | |
| 183 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 184 | @Parameter(name = "file") @RequestParam("file") MultipartFile file) | |
| 185 | throws JsonProcessingException, IOException, CsvException { | |
| 186 | ||
| 187 | Course course = courseRepository.findById(courseId) | |
| 188 |
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())); |
| 189 | ||
| 190 | int counts[] = { 0, 0 }; | |
| 191 | ||
| 192 | try (InputStream inputStream = new BufferedInputStream(file.getInputStream()); | |
| 193 | InputStreamReader reader = new InputStreamReader(inputStream); | |
| 194 | CSVReader csvReader = new CSVReader(reader);) { | |
| 195 |
1
1. uploadRosterStudents : removed call to com/opencsv/CSVReader::skip → KILLED |
csvReader.skip(2); |
| 196 | List<String[]> myEntries = csvReader.readAll(); | |
| 197 | for (String[] row : myEntries) { | |
| 198 | RosterStudent rosterStudent = fromEgradesCSVRow(row); | |
| 199 | InsertStatus s = upsertStudent(rosterStudent, course); | |
| 200 |
1
1. uploadRosterStudents : Replaced integer addition with subtraction → KILLED |
counts[s.ordinal()]++; |
| 201 | } | |
| 202 | } | |
| 203 |
1
1. uploadRosterStudents : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::uploadRosterStudents → KILLED |
return Map.of( |
| 204 | "filename", file.getOriginalFilename(), | |
| 205 | "message", String.format("Inserted %d new students, Updated %d students", | |
| 206 | counts[InsertStatus.INSERTED.ordinal()], counts[InsertStatus.UPDATED.ordinal()])); | |
| 207 | ||
| 208 | } | |
| 209 | ||
| 210 | public RosterStudent fromEgradesCSVRow(String[] row) { | |
| 211 |
1
1. fromEgradesCSVRow : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::fromEgradesCSVRow → KILLED |
return RosterStudent.builder() |
| 212 | .firstName(row[5]) | |
| 213 | .lastName(row[4]) | |
| 214 | .studentId(row[1]) | |
| 215 | .email(row[10]) | |
| 216 | .build(); | |
| 217 | } | |
| 218 | ||
| 219 | public InsertStatus upsertStudent(RosterStudent student, Course course) { | |
| 220 | Optional<RosterStudent> existingStudent = rosterStudentRepository.findByCourseIdAndStudentId(course.getId(), | |
| 221 | student.getStudentId()); | |
| 222 | String convertedEmail = student.getEmail().replace("@umail.ucsb.edu","@ucsb.edu"); | |
| 223 |
1
1. upsertStudent : negated conditional → KILLED |
if (existingStudent.isPresent()) { |
| 224 | RosterStudent existingStudentObj = existingStudent.get(); | |
| 225 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setRosterStatus → KILLED |
existingStudentObj.setRosterStatus(RosterStatus.ROSTER); |
| 226 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setFirstName → KILLED |
existingStudentObj.setFirstName(student.getFirstName()); |
| 227 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setLastName → KILLED |
existingStudentObj.setLastName(student.getLastName()); |
| 228 |
1
1. upsertStudent : negated conditional → KILLED |
if (!existingStudentObj.getEmail().equals(convertedEmail)) { |
| 229 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setEmail → KILLED |
existingStudentObj.setEmail(convertedEmail); |
| 230 | } | |
| 231 | existingStudentObj = rosterStudentRepository.save(existingStudentObj); | |
| 232 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToRosterStudent → KILLED |
updateUserService.attachUserToRosterStudent(existingStudentObj); |
| 233 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::upsertStudent → KILLED |
return InsertStatus.UPDATED; |
| 234 | } else { | |
| 235 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setCourse → KILLED |
student.setCourse(course); |
| 236 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setEmail → KILLED |
student.setEmail(convertedEmail); |
| 237 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setRosterStatus → KILLED |
student.setRosterStatus(RosterStatus.ROSTER); |
| 238 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED |
student.setOrgStatus(OrgStatus.NONE); |
| 239 | student = rosterStudentRepository.save(student); | |
| 240 |
1
1. upsertStudent : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToRosterStudent → KILLED |
updateUserService.attachUserToRosterStudent(student); |
| 241 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::upsertStudent → KILLED |
return InsertStatus.INSERTED; |
| 242 | } | |
| 243 | } | |
| 244 | ||
| 245 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
| 246 | @PostMapping("/updateCourseMembership") | |
| 247 | public Job updateCourseMembership(@Parameter(name = "courseId", description = "Course ID") @RequestParam Long courseId) throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 248 |
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)); |
| 249 |
2
1. updateCourseMembership : negated conditional → KILLED 2. updateCourseMembership : negated conditional → KILLED |
if(course.getInstallationId() == null || course.getOrgName() == null){ |
| 250 | throw new NoLinkedOrganizationException(course.getCourseName()); | |
| 251 | }else{ | |
| 252 | UpdateOrgMembershipJob job = UpdateOrgMembershipJob.builder() | |
| 253 | .rosterStudentRepository(rosterStudentRepository) | |
| 254 | .organizationMemberService(organizationMemberService) | |
| 255 | .course(course) | |
| 256 | .build(); | |
| 257 | ||
| 258 |
1
1. updateCourseMembership : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::updateCourseMembership → KILLED |
return jobService.runAsJob(job); |
| 259 | } | |
| 260 | } | |
| 261 | | |
| 262 | @Operation(summary = "Link a Roster Student to a Github Account") | |
| 263 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 264 | @PutMapping("/linkGitHub") | |
| 265 | public ResponseEntity<String> linkGitHub(@Parameter(name = "rosterStudentId", description = "Roster Student to be linked to") @RequestParam Long rosterStudentId){ | |
| 266 | User currentUser = currentUserService.getUser(); | |
| 267 | RosterStudent rosterStudent = rosterStudentRepository.findById(rosterStudentId) | |
| 268 |
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)); |
| 269 | ||
| 270 |
1
1. linkGitHub : negated conditional → KILLED |
if (currentUser.getId() != rosterStudent.getUser().getId()) { |
| 271 | throw new AccessDeniedException("User not authorized to link this roster student"); | |
| 272 | } | |
| 273 | ||
| 274 |
2
1. linkGitHub : negated conditional → KILLED 2. linkGitHub : negated conditional → KILLED |
if (rosterStudent.getGithubId() != 0 && rosterStudent.getGithubLogin() != null ) { |
| 275 |
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"); |
| 276 | } | |
| 277 | ||
| 278 |
1
1. linkGitHub : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubId → KILLED |
rosterStudent.setGithubId(currentUser.getGithubId()); |
| 279 |
1
1. linkGitHub : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubLogin → KILLED |
rosterStudent.setGithubLogin(currentUser.getGithubLogin()); |
| 280 | rosterStudentRepository.save(rosterStudent); | |
| 281 |
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"); |
| 282 | } | |
| 283 | ||
| 284 | @Operation(summary = "Get Associated Roster Students with a User") | |
| 285 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 286 | @GetMapping("/associatedRosterStudents") | |
| 287 | public Iterable<RosterStudent> getAssociatedRosterStudents(){ | |
| 288 | User currentUser = currentUserService.getUser(); | |
| 289 | Iterable<RosterStudent> rosterStudents = rosterStudentRepository.findAllByUser((currentUser)); | |
| 290 |
1
1. getAssociatedRosterStudents : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/RosterStudentsController::getAssociatedRosterStudents → KILLED |
return rosterStudents; |
| 291 | } | |
| 292 | } | |
Mutations | ||
| 95 |
1.1 |
|
| 108 |
1.1 |
|
| 121 |
1.1 |
|
| 123 |
1.1 |
|
| 141 |
1.1 |
|
| 144 |
1.1 2.2 |
|
| 147 |
1.1 |
|
| 148 |
1.1 |
|
| 149 |
1.1 |
|
| 153 |
1.1 |
|
| 169 |
1.1 |
|
| 171 |
1.1 |
|
| 176 |
1.1 |
|
| 188 |
1.1 |
|
| 195 |
1.1 |
|
| 200 |
1.1 |
|
| 203 |
1.1 |
|
| 211 |
1.1 |
|
| 223 |
1.1 |
|
| 225 |
1.1 |
|
| 226 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 232 |
1.1 |
|
| 233 |
1.1 |
|
| 235 |
1.1 |
|
| 236 |
1.1 |
|
| 237 |
1.1 |
|
| 238 |
1.1 |
|
| 240 |
1.1 |
|
| 241 |
1.1 |
|
| 248 |
1.1 |
|
| 249 |
1.1 2.2 |
|
| 258 |
1.1 |
|
| 268 |
1.1 |
|
| 270 |
1.1 |
|
| 274 |
1.1 2.2 |
|
| 275 |
1.1 |
|
| 278 |
1.1 |
|
| 279 |
1.1 |
|
| 281 |
1.1 |
|
| 290 |
1.1 |