| 1 | package edu.ucsb.cs.scaffold.controller; | |
| 2 | ||
| 3 | import edu.ucsb.cs.scaffold.entity.Course; | |
| 4 | import edu.ucsb.cs.scaffold.entity.RosterStudent; | |
| 5 | import edu.ucsb.cs.scaffold.entity.User; | |
| 6 | import edu.ucsb.cs.scaffold.enums.InsertStatus; | |
| 7 | import edu.ucsb.cs.scaffold.enums.RosterStatus; | |
| 8 | import edu.ucsb.cs.scaffold.errors.EntityNotFoundException; | |
| 9 | import edu.ucsb.cs.scaffold.model.RosterStudentDTO; | |
| 10 | import edu.ucsb.cs.scaffold.model.UpsertResponse; | |
| 11 | import edu.ucsb.cs.scaffold.repository.CourseRepository; | |
| 12 | import edu.ucsb.cs.scaffold.repository.RosterStudentRepository; | |
| 13 | import edu.ucsb.cs.scaffold.services.CurrentUserService; | |
| 14 | import edu.ucsb.cs.scaffold.services.UpdateUserService; | |
| 15 | import edu.ucsb.cs.scaffold.utilities.CanonicalFormConverter; | |
| 16 | import io.swagger.v3.oas.annotations.Operation; | |
| 17 | import io.swagger.v3.oas.annotations.Parameter; | |
| 18 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 19 | import java.util.Optional; | |
| 20 | import lombok.extern.slf4j.Slf4j; | |
| 21 | import org.springframework.beans.factory.annotation.Autowired; | |
| 22 | import org.springframework.http.HttpStatus; | |
| 23 | import org.springframework.http.ResponseEntity; | |
| 24 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 25 | import org.springframework.transaction.annotation.Transactional; | |
| 26 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 27 | import org.springframework.web.bind.annotation.GetMapping; | |
| 28 | import org.springframework.web.bind.annotation.PathVariable; | |
| 29 | import org.springframework.web.bind.annotation.PostMapping; | |
| 30 | import org.springframework.web.bind.annotation.PutMapping; | |
| 31 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 32 | import org.springframework.web.bind.annotation.RequestParam; | |
| 33 | import org.springframework.web.bind.annotation.RestController; | |
| 34 | import org.springframework.web.server.ResponseStatusException; | |
| 35 | ||
| 36 | @Tag(name = "RosterStudents") | |
| 37 | @RequestMapping("/api/rosterstudents") | |
| 38 | @RestController | |
| 39 | @Slf4j | |
| 40 | public class RosterStudentsController extends ApiController { | |
| 41 | ||
| 42 | @Autowired private RosterStudentRepository rosterStudentRepository; | |
| 43 | ||
| 44 | @Autowired private CourseRepository courseRepository; | |
| 45 | ||
| 46 | @Autowired private UpdateUserService updateUserService; | |
| 47 | ||
| 48 | @Autowired private CurrentUserService currentUserService; | |
| 49 | ||
| 50 | /** | |
| 51 | * This method creates a new RosterStudent. It is important to keep the code in this method | |
| 52 | * consistent with the code for adding multiple roster students from a CSV | |
| 53 | * | |
| 54 | * @return the created RosterStudent | |
| 55 | */ | |
| 56 | @Operation(summary = "Create a new roster student") | |
| 57 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 58 | @PostMapping("/post") | |
| 59 | public ResponseEntity<UpsertResponse> postRosterStudent( | |
| 60 | @Parameter(name = "studentId") @RequestParam String studentId, | |
| 61 | @Parameter(name = "firstName") @RequestParam String firstName, | |
| 62 | @Parameter(name = "lastName") @RequestParam String lastName, | |
| 63 | @Parameter(name = "email") @RequestParam String email, | |
| 64 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 65 | @Parameter(name = "section") @RequestParam(required = false) String section) | |
| 66 | throws EntityNotFoundException { | |
| 67 | ||
| 68 | // Get Course or else throw an error | |
| 69 | ||
| 70 | Course course = | |
| 71 | courseRepository | |
| 72 | .findById(courseId) | |
| 73 |
1
1. lambda$postRosterStudent$0 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$postRosterStudent$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 74 | ||
| 75 | RosterStudent rosterStudent = | |
| 76 | RosterStudent.builder() | |
| 77 | .studentId(studentId) | |
| 78 | .firstName(firstName) | |
| 79 | .lastName(lastName) | |
| 80 | .email(email.strip()) | |
| 81 |
1
1. postRosterStudent : negated conditional → KILLED |
.section(section != null ? section : "") |
| 82 | .build(); | |
| 83 | ||
| 84 | UpsertResponse upsertResponse = upsertStudent(rosterStudent, course, RosterStatus.MANUAL); | |
| 85 |
1
1. postRosterStudent : negated conditional → KILLED |
if (upsertResponse.getInsertStatus() == InsertStatus.REJECTED) { |
| 86 |
1
1. postRosterStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::postRosterStudent → KILLED |
return ResponseEntity.status(HttpStatus.CONFLICT).body(upsertResponse); |
| 87 | } else { | |
| 88 | rosterStudent = rosterStudentRepository.save(upsertResponse.rosterStudent()); | |
| 89 |
1
1. postRosterStudent : removed call to edu/ucsb/cs/scaffold/services/UpdateUserService::attachUserToRosterStudent → KILLED |
updateUserService.attachUserToRosterStudent(rosterStudent); |
| 90 |
1
1. postRosterStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::postRosterStudent → KILLED |
return ResponseEntity.ok(upsertResponse); |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * This method returns a list of roster students for a given course. | |
| 96 | * | |
| 97 | * @return a list of all courses. | |
| 98 | */ | |
| 99 | @Operation(summary = "List all roster students for a course") | |
| 100 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 101 | @GetMapping("/course/{courseId}") | |
| 102 | public Iterable<RosterStudentDTO> rosterStudentForCourse( | |
| 103 | @Parameter(name = "courseId") @PathVariable Long courseId) throws EntityNotFoundException { | |
| 104 | courseRepository | |
| 105 | .findById(courseId) | |
| 106 |
1
1. lambda$rosterStudentForCourse$1 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$rosterStudentForCourse$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 107 | Iterable<RosterStudent> rosterStudents = | |
| 108 | rosterStudentRepository.findByCourseIdOrderByFirstNameAscLastNameAscIgnoreCase(courseId); | |
| 109 | Iterable<RosterStudentDTO> rosterStudentDTOs = | |
| 110 | () -> | |
| 111 |
1
1. lambda$rosterStudentForCourse$2 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$rosterStudentForCourse$2 → KILLED |
java.util.stream.StreamSupport.stream(rosterStudents.spliterator(), false) |
| 112 | .map(RosterStudentDTO::new) | |
| 113 | .iterator(); | |
| 114 |
1
1. rosterStudentForCourse : replaced return value with Collections.emptyList for edu/ucsb/cs/scaffold/controller/RosterStudentsController::rosterStudentForCourse → KILLED |
return rosterStudentDTOs; |
| 115 | } | |
| 116 | ||
| 117 | public static UpsertResponse upsertStudent( | |
| 118 | RosterStudent student, Course course, RosterStatus rosterStatus) { | |
| 119 | String convertedEmail = CanonicalFormConverter.convertToValidEmail(student.getEmail()).strip(); | |
| 120 | Optional<RosterStudent> existingStudent = | |
| 121 | course.getRosterStudents().stream() | |
| 122 | .filter( | |
| 123 |
2
1. lambda$upsertStudent$3 : replaced boolean return with false for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$upsertStudent$3 → KILLED 2. lambda$upsertStudent$3 : replaced boolean return with true for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$upsertStudent$3 → KILLED |
filteringStudent -> student.getStudentId().equals(filteringStudent.getStudentId())) |
| 124 | .findFirst(); | |
| 125 | Optional<RosterStudent> existingStudentByEmail = | |
| 126 | course.getRosterStudents().stream() | |
| 127 |
2
1. lambda$upsertStudent$4 : replaced boolean return with true for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$upsertStudent$4 → KILLED 2. lambda$upsertStudent$4 : replaced boolean return with false for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$upsertStudent$4 → KILLED |
.filter(filteringStudent -> convertedEmail.equals(filteringStudent.getEmail())) |
| 128 | .findFirst(); | |
| 129 |
2
1. upsertStudent : negated conditional → KILLED 2. upsertStudent : negated conditional → KILLED |
if (existingStudent.isPresent() && existingStudentByEmail.isPresent()) { |
| 130 |
1
1. upsertStudent : negated conditional → KILLED |
if (existingStudent.get().getId().equals(existingStudentByEmail.get().getId())) { |
| 131 | RosterStudent existingStudentObj = existingStudent.get(); | |
| 132 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setRosterStatus → KILLED |
existingStudentObj.setRosterStatus(rosterStatus); |
| 133 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setFirstName → KILLED |
existingStudentObj.setFirstName(student.getFirstName()); |
| 134 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setLastName → KILLED |
existingStudentObj.setLastName(student.getLastName()); |
| 135 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setSection → KILLED |
existingStudentObj.setSection(student.getSection()); |
| 136 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::upsertStudent → KILLED |
return new UpsertResponse(InsertStatus.UPDATED, existingStudentObj); |
| 137 | } else { | |
| 138 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::upsertStudent → KILLED |
return new UpsertResponse(InsertStatus.REJECTED, student); |
| 139 | } | |
| 140 |
2
1. upsertStudent : negated conditional → KILLED 2. upsertStudent : negated conditional → KILLED |
} else if (existingStudent.isPresent() || existingStudentByEmail.isPresent()) { |
| 141 | RosterStudent existingStudentObj = | |
| 142 |
1
1. upsertStudent : negated conditional → KILLED |
existingStudent.isPresent() ? existingStudent.get() : existingStudentByEmail.get(); |
| 143 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setRosterStatus → KILLED |
existingStudentObj.setRosterStatus(rosterStatus); |
| 144 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setFirstName → KILLED |
existingStudentObj.setFirstName(student.getFirstName()); |
| 145 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setLastName → KILLED |
existingStudentObj.setLastName(student.getLastName()); |
| 146 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setSection → KILLED |
existingStudentObj.setSection(student.getSection()); |
| 147 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setEmail → KILLED |
existingStudentObj.setEmail(convertedEmail); |
| 148 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setStudentId → KILLED |
existingStudentObj.setStudentId(student.getStudentId()); |
| 149 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::upsertStudent → KILLED |
return new UpsertResponse(InsertStatus.UPDATED, existingStudentObj); |
| 150 | } else { | |
| 151 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setCourse → KILLED |
student.setCourse(course); |
| 152 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setEmail → KILLED |
student.setEmail(convertedEmail); |
| 153 |
1
1. upsertStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setRosterStatus → KILLED |
student.setRosterStatus(rosterStatus); |
| 154 |
1
1. upsertStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::upsertStudent → KILLED |
return new UpsertResponse(InsertStatus.INSERTED, student); |
| 155 | } | |
| 156 | } | |
| 157 | ||
| 158 | @Operation(summary = "Get Associated Roster Students with a User") | |
| 159 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 160 | @GetMapping("/associatedRosterStudents") | |
| 161 | public Iterable<RosterStudent> getAssociatedRosterStudents() { | |
| 162 | User currentUser = currentUserService.getUser(); | |
| 163 | Iterable<RosterStudent> rosterStudents = rosterStudentRepository.findAllByUser((currentUser)); | |
| 164 |
1
1. getAssociatedRosterStudents : replaced return value with Collections.emptyList for edu/ucsb/cs/scaffold/controller/RosterStudentsController::getAssociatedRosterStudents → KILLED |
return rosterStudents; |
| 165 | } | |
| 166 | ||
| 167 | @Operation(summary = "Update a roster student") | |
| 168 | @PreAuthorize("@CourseSecurity.hasRosterStudentManagementPermissions(#root, #id)") | |
| 169 | @PutMapping("/update") | |
| 170 | public RosterStudent updateRosterStudent( | |
| 171 | @Parameter(name = "id") @RequestParam Long id, | |
| 172 | @Parameter(name = "firstName") @RequestParam(required = false) String firstName, | |
| 173 | @Parameter(name = "lastName") @RequestParam(required = false) String lastName, | |
| 174 | @Parameter(name = "studentId") @RequestParam(required = false) String studentId, | |
| 175 | @Parameter(name = "section") @RequestParam(required = false) String section) | |
| 176 | throws EntityNotFoundException { | |
| 177 | ||
| 178 |
3
1. updateRosterStudent : negated conditional → KILLED 2. updateRosterStudent : negated conditional → KILLED 3. updateRosterStudent : negated conditional → KILLED |
if (firstName == null |
| 179 | || lastName == null | |
| 180 | || studentId == null | |
| 181 |
1
1. updateRosterStudent : negated conditional → KILLED |
|| firstName.trim().isEmpty() |
| 182 |
1
1. updateRosterStudent : negated conditional → KILLED |
|| lastName.trim().isEmpty() |
| 183 |
1
1. updateRosterStudent : negated conditional → KILLED |
|| studentId.trim().isEmpty()) { |
| 184 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Required fields cannot be empty"); | |
| 185 | } | |
| 186 | ||
| 187 | RosterStudent rosterStudent = | |
| 188 | rosterStudentRepository | |
| 189 | .findById(id) | |
| 190 |
1
1. lambda$updateRosterStudent$5 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$updateRosterStudent$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
| 191 | ||
| 192 |
1
1. updateRosterStudent : negated conditional → KILLED |
if (!rosterStudent.getStudentId().trim().equals(studentId.trim())) { |
| 193 | Optional<RosterStudent> existingStudent = | |
| 194 | rosterStudentRepository.findByCourseIdAndStudentId( | |
| 195 | rosterStudent.getCourse().getId(), studentId.trim()); | |
| 196 |
1
1. updateRosterStudent : negated conditional → KILLED |
if (existingStudent.isPresent()) { |
| 197 | throw new ResponseStatusException( | |
| 198 | HttpStatus.BAD_REQUEST, "Student ID already exists in this course"); | |
| 199 | } | |
| 200 | } | |
| 201 | ||
| 202 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setFirstName → KILLED |
rosterStudent.setFirstName(firstName.trim()); |
| 203 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setLastName → KILLED |
rosterStudent.setLastName(lastName.trim()); |
| 204 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setStudentId → KILLED |
rosterStudent.setStudentId(studentId.trim()); |
| 205 | ||
| 206 |
1
1. updateRosterStudent : negated conditional → KILLED |
if (section != null) { |
| 207 |
1
1. updateRosterStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setSection → KILLED |
rosterStudent.setSection(section.trim()); |
| 208 | } | |
| 209 | ||
| 210 |
1
1. updateRosterStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::updateRosterStudent → KILLED |
return rosterStudentRepository.save(rosterStudent); |
| 211 | } | |
| 212 | ||
| 213 | @Operation( | |
| 214 | summary = "Restore a roster student", | |
| 215 | description = "Restores a student who previously was dropped from the course") | |
| 216 | @PreAuthorize("@CourseSecurity.hasRosterStudentManagementPermissions(#root, #id)") | |
| 217 | @PutMapping("/restore") | |
| 218 | public RosterStudent restoreRosterStudent(@Parameter(name = "id") @RequestParam Long id) | |
| 219 | throws EntityNotFoundException { | |
| 220 | RosterStudent rosterStudent = | |
| 221 | rosterStudentRepository | |
| 222 | .findById(id) | |
| 223 |
1
1. lambda$restoreRosterStudent$6 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$restoreRosterStudent$6 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
| 224 |
1
1. restoreRosterStudent : removed call to edu/ucsb/cs/scaffold/entity/RosterStudent::setRosterStatus → KILLED |
rosterStudent.setRosterStatus(RosterStatus.MANUAL); |
| 225 |
1
1. restoreRosterStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::restoreRosterStudent → KILLED |
return rosterStudentRepository.save(rosterStudent); |
| 226 | } | |
| 227 | ||
| 228 | @Operation(summary = "Delete a roster student") | |
| 229 | @PreAuthorize("@CourseSecurity.hasRosterStudentManagementPermissions(#root, #id)") | |
| 230 | @DeleteMapping("/delete") | |
| 231 | @Transactional | |
| 232 | public ResponseEntity<String> deleteRosterStudent(@Parameter(name = "id") @RequestParam Long id) | |
| 233 | throws EntityNotFoundException { | |
| 234 | RosterStudent rosterStudent = | |
| 235 | rosterStudentRepository | |
| 236 | .findById(id) | |
| 237 |
1
1. lambda$deleteRosterStudent$7 : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::lambda$deleteRosterStudent$7 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RosterStudent.class, id)); |
| 238 | ||
| 239 | rosterStudent.getCourse().getRosterStudents().remove(rosterStudent); | |
| 240 |
1
1. deleteRosterStudent : removed call to edu/ucsb/cs/scaffold/repository/RosterStudentRepository::delete → KILLED |
rosterStudentRepository.delete(rosterStudent); |
| 241 | ||
| 242 |
1
1. deleteRosterStudent : replaced return value with null for edu/ucsb/cs/scaffold/controller/RosterStudentsController::deleteRosterStudent → KILLED |
return ResponseEntity.ok( |
| 243 | "Successfully deleted roster student and removed him/her from the course list"); | |
| 244 | } | |
| 245 | } | |
Mutations | ||
| 73 |
1.1 |
|
| 81 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 89 |
1.1 |
|
| 90 |
1.1 |
|
| 106 |
1.1 |
|
| 111 |
1.1 |
|
| 114 |
1.1 |
|
| 123 |
1.1 2.2 |
|
| 127 |
1.1 2.2 |
|
| 129 |
1.1 2.2 |
|
| 130 |
1.1 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |
|
| 136 |
1.1 |
|
| 138 |
1.1 |
|
| 140 |
1.1 2.2 |
|
| 142 |
1.1 |
|
| 143 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |
|
| 146 |
1.1 |
|
| 147 |
1.1 |
|
| 148 |
1.1 |
|
| 149 |
1.1 |
|
| 151 |
1.1 |
|
| 152 |
1.1 |
|
| 153 |
1.1 |
|
| 154 |
1.1 |
|
| 164 |
1.1 |
|
| 178 |
1.1 2.2 3.3 |
|
| 181 |
1.1 |
|
| 182 |
1.1 |
|
| 183 |
1.1 |
|
| 190 |
1.1 |
|
| 192 |
1.1 |
|
| 196 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 204 |
1.1 |
|
| 206 |
1.1 |
|
| 207 |
1.1 |
|
| 210 |
1.1 |
|
| 223 |
1.1 |
|
| 224 |
1.1 |
|
| 225 |
1.1 |
|
| 237 |
1.1 |
|
| 240 |
1.1 |
|
| 242 |
1.1 |