| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import static edu.ucsb.cs156.frontiers.controllers.RosterStudentsController.upsertStudent; | |
| 4 | ||
| 5 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 7 | import edu.ucsb.cs156.frontiers.enums.InsertStatus; | |
| 8 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 9 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 10 | import edu.ucsb.cs156.frontiers.jobs.PullTeamsFromCanvasJob; | |
| 11 | import edu.ucsb.cs156.frontiers.jobs.PushTeamsToCanvasJob; | |
| 12 | import edu.ucsb.cs156.frontiers.jobs.RemoveStudentsJob; | |
| 13 | import edu.ucsb.cs156.frontiers.models.CanvasGroupSet; | |
| 14 | import edu.ucsb.cs156.frontiers.models.LoadResult; | |
| 15 | import edu.ucsb.cs156.frontiers.models.UpsertResponse; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 18 | import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository; | |
| 19 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 20 | import edu.ucsb.cs156.frontiers.services.CanvasService; | |
| 21 | import edu.ucsb.cs156.frontiers.services.OrganizationMemberService; | |
| 22 | import edu.ucsb.cs156.frontiers.services.StudentDropService; | |
| 23 | import edu.ucsb.cs156.frontiers.services.UpdateUserService; | |
| 24 | import edu.ucsb.cs156.jobs.entities.Job; | |
| 25 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 26 | import io.swagger.v3.oas.annotations.Operation; | |
| 27 | import io.swagger.v3.oas.annotations.Parameter; | |
| 28 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 29 | import java.util.ArrayList; | |
| 30 | import java.util.List; | |
| 31 | import lombok.extern.slf4j.Slf4j; | |
| 32 | import org.springframework.http.HttpStatus; | |
| 33 | import org.springframework.http.ResponseEntity; | |
| 34 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 35 | import org.springframework.web.bind.annotation.GetMapping; | |
| 36 | import org.springframework.web.bind.annotation.PostMapping; | |
| 37 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 38 | import org.springframework.web.bind.annotation.RequestParam; | |
| 39 | import org.springframework.web.bind.annotation.RestController; | |
| 40 | ||
| 41 | @RestController | |
| 42 | @RequestMapping("/api/courses/canvas") | |
| 43 | @Tag(name = "Canvas") | |
| 44 | @Slf4j | |
| 45 | public class CanvasController extends ApiController { | |
| 46 | ||
| 47 | private final CourseRepository courseRepository; | |
| 48 | private final CanvasService canvasService; | |
| 49 | private final StudentDropService studentDropService; | |
| 50 | private final TeamRepository teamRepository; | |
| 51 | private final JobService jobService; | |
| 52 | private final RosterStudentRepository rosterStudentRepository; | |
| 53 | private final UpdateUserService updateUserService; | |
| 54 | private final OrganizationMemberService organizationMemberService; | |
| 55 | private final TeamMemberRepository teamMemberRepository; | |
| 56 | ||
| 57 | public CanvasController( | |
| 58 | CourseRepository courseRepository, | |
| 59 | CanvasService canvasService, | |
| 60 | TeamRepository teamRepository, | |
| 61 | JobService jobService, | |
| 62 | RosterStudentRepository rosterStudentRepository, | |
| 63 | UpdateUserService updateUserService, | |
| 64 | OrganizationMemberService organizationMemberService, | |
| 65 | TeamMemberRepository teamMemberRepository, | |
| 66 | StudentDropService studentDropService) { | |
| 67 | this.courseRepository = courseRepository; | |
| 68 | this.canvasService = canvasService; | |
| 69 | this.studentDropService = studentDropService; | |
| 70 | this.teamRepository = teamRepository; | |
| 71 | this.jobService = jobService; | |
| 72 | this.rosterStudentRepository = rosterStudentRepository; | |
| 73 | this.updateUserService = updateUserService; | |
| 74 | this.organizationMemberService = organizationMemberService; | |
| 75 | this.teamMemberRepository = teamMemberRepository; | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Upload Roster students for Course from Canvas. It is important to keep the code in this method | |
| 80 | * consistent with the code in uploadRosterStudentsCSV. | |
| 81 | * | |
| 82 | * @param courseId the internal course ID in Frontiers | |
| 83 | * @return LoadResult with counts of inserted, updated, dropped students and any rejected students | |
| 84 | */ | |
| 85 | @Operation(summary = "Upload Roster students for Course from Canvas") | |
| 86 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 87 | @PostMapping("/sync/students") | |
| 88 | public ResponseEntity<LoadResult> uploadRosterFromCanvas( | |
| 89 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 90 | ||
| 91 | Course course = | |
| 92 | courseRepository | |
| 93 | .findById(courseId) | |
| 94 |
1
1. lambda$uploadRosterFromCanvas$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$uploadRosterFromCanvas$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 95 | ||
| 96 | course.getRosterStudents().stream() | |
| 97 |
2
1. lambda$uploadRosterFromCanvas$1 : negated conditional → KILLED 2. lambda$uploadRosterFromCanvas$1 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$uploadRosterFromCanvas$1 → KILLED |
.filter(filteredStudent -> filteredStudent.getRosterStatus() == RosterStatus.ROSTER) |
| 98 |
2
1. uploadRosterFromCanvas : removed call to java/util/stream/Stream::forEach → KILLED 2. lambda$uploadRosterFromCanvas$2 : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setRosterStatus → KILLED |
.forEach(student -> student.setRosterStatus(RosterStatus.DROPPED)); |
| 99 | ||
| 100 | int counts[] = {0, 0}; | |
| 101 | List<RosterStudent> rejectedStudents = new ArrayList<>(); | |
| 102 | ||
| 103 | List<RosterStudent> canvasStudents = canvasService.getCanvasRoster(course); | |
| 104 | for (RosterStudent rosterStudent : canvasStudents) { | |
| 105 | UpsertResponse upsertResponse = upsertStudent(rosterStudent, course, RosterStatus.ROSTER); | |
| 106 |
1
1. uploadRosterFromCanvas : negated conditional → KILLED |
if (upsertResponse.getInsertStatus() == InsertStatus.REJECTED) { |
| 107 | rejectedStudents.add(upsertResponse.rosterStudent()); | |
| 108 | } else { | |
| 109 | InsertStatus s = upsertResponse.getInsertStatus(); | |
| 110 |
1
1. uploadRosterFromCanvas : negated conditional → KILLED |
if (s == InsertStatus.INSERTED) { |
| 111 | course.getRosterStudents().add(upsertResponse.rosterStudent()); | |
| 112 | } | |
| 113 |
1
1. uploadRosterFromCanvas : Replaced integer addition with subtraction → KILLED |
counts[s.ordinal()]++; |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 |
1
1. uploadRosterFromCanvas : negated conditional → KILLED |
if (rejectedStudents.isEmpty()) { |
| 118 | List<RosterStudent> droppedStudents = | |
| 119 | course.getRosterStudents().stream() | |
| 120 |
2
1. lambda$uploadRosterFromCanvas$3 : negated conditional → KILLED 2. lambda$uploadRosterFromCanvas$3 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$uploadRosterFromCanvas$3 → KILLED |
.filter(student -> student.getRosterStatus() == RosterStatus.DROPPED) |
| 121 | .toList(); | |
| 122 | LoadResult successfulResult = | |
| 123 | new LoadResult( | |
| 124 | counts[InsertStatus.INSERTED.ordinal()], | |
| 125 | counts[InsertStatus.UPDATED.ordinal()], | |
| 126 | droppedStudents.size(), | |
| 127 | List.of()); | |
| 128 |
1
1. uploadRosterFromCanvas : removed call to java/util/List::forEach → KILLED |
droppedStudents.forEach(studentDropService::dropStudent); |
| 129 | rosterStudentRepository.saveAll(course.getRosterStudents()); | |
| 130 |
1
1. uploadRosterFromCanvas : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUsersToRosterStudents → KILLED |
updateUserService.attachUsersToRosterStudents(course.getRosterStudents()); |
| 131 | RemoveStudentsJob job = | |
| 132 | RemoveStudentsJob.builder() | |
| 133 | .students(droppedStudents) | |
| 134 | .organizationMemberService(organizationMemberService) | |
| 135 | .rosterStudentRepository(rosterStudentRepository) | |
| 136 | .build(); | |
| 137 | jobService.runAsJob(job); | |
| 138 |
1
1. uploadRosterFromCanvas : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::uploadRosterFromCanvas → KILLED |
return ResponseEntity.ok(successfulResult); |
| 139 | } else { | |
| 140 | LoadResult conflictResult = new LoadResult(0, 0, 0, rejectedStudents); | |
| 141 |
1
1. uploadRosterFromCanvas : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::uploadRosterFromCanvas → KILLED |
return ResponseEntity.status(HttpStatus.CONFLICT).body(conflictResult); |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | @Operation(summary = "See available Canvas GroupSets") | |
| 146 | @GetMapping("/groupsets") | |
| 147 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 148 | public List<CanvasGroupSet> getCanvasGroupSets( | |
| 149 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 150 | Course course = | |
| 151 | courseRepository | |
| 152 | .findById(courseId) | |
| 153 |
1
1. lambda$getCanvasGroupSets$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$getCanvasGroupSets$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 154 |
1
1. getCanvasGroupSets : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CanvasController::getCanvasGroupSets → KILLED |
return canvasService.getCanvasGroupSets(course); |
| 155 | } | |
| 156 | ||
| 157 | @Operation( | |
| 158 | summary = "Pull teams from Canvas", | |
| 159 | description = | |
| 160 | "Launches a job that creates or updates the teams in Frontiers to match the groups in" | |
| 161 | + " the given Canvas group set.") | |
| 162 | @PostMapping("/teams/pull") | |
| 163 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 164 | public Job pullCanvasTeams( | |
| 165 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 166 | @Parameter(name = "groupSetId") @RequestParam String groupSetId) { | |
| 167 | Course course = | |
| 168 | courseRepository | |
| 169 | .findById(courseId) | |
| 170 |
1
1. lambda$pullCanvasTeams$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$pullCanvasTeams$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 171 | PullTeamsFromCanvasJob job = | |
| 172 | PullTeamsFromCanvasJob.builder() | |
| 173 | .course(course) | |
| 174 | .canvasService(canvasService) | |
| 175 | .teamRepository(teamRepository) | |
| 176 | .teamMemberRepository(teamMemberRepository) | |
| 177 | .courseRepository(courseRepository) | |
| 178 | .groupsetId(groupSetId) | |
| 179 | .build(); | |
| 180 |
1
1. pullCanvasTeams : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::pullCanvasTeams → KILLED |
return jobService.runAsJob(job); |
| 181 | } | |
| 182 | ||
| 183 | @Operation( | |
| 184 | summary = "Push teams to Canvas", | |
| 185 | description = | |
| 186 | "Launches a job that updates the groups in the given Canvas group set to match the teams" | |
| 187 | + " in Frontiers: groups are created, members added and removed, and groups with no" | |
| 188 | + " matching team deleted. Problems with individual students or groups are logged as" | |
| 189 | + " warnings in the job log and the job proceeds.") | |
| 190 | @PostMapping("/teams/push") | |
| 191 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 192 | public Job pushCanvasTeams( | |
| 193 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 194 | @Parameter(name = "groupSetId") @RequestParam String groupSetId) { | |
| 195 | Course course = | |
| 196 | courseRepository | |
| 197 | .findById(courseId) | |
| 198 |
1
1. lambda$pushCanvasTeams$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::lambda$pushCanvasTeams$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 199 | PushTeamsToCanvasJob job = | |
| 200 | PushTeamsToCanvasJob.builder() | |
| 201 | .course(course) | |
| 202 | .groupSetId(groupSetId) | |
| 203 | .canvasService(canvasService) | |
| 204 | .courseRepository(courseRepository) | |
| 205 | .teamRepository(teamRepository) | |
| 206 | .build(); | |
| 207 |
1
1. pushCanvasTeams : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CanvasController::pushCanvasTeams → KILLED |
return jobService.runAsJob(job); |
| 208 | } | |
| 209 | } | |
Mutations | ||
| 94 |
1.1 |
|
| 97 |
1.1 2.2 |
|
| 98 |
1.1 2.2 |
|
| 106 |
1.1 |
|
| 110 |
1.1 |
|
| 113 |
1.1 |
|
| 117 |
1.1 |
|
| 120 |
1.1 2.2 |
|
| 128 |
1.1 |
|
| 130 |
1.1 |
|
| 138 |
1.1 |
|
| 141 |
1.1 |
|
| 153 |
1.1 |
|
| 154 |
1.1 |
|
| 170 |
1.1 |
|
| 180 |
1.1 |
|
| 198 |
1.1 |
|
| 207 |
1.1 |