| 1 | package edu.ucsb.cs156.organic.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.organic.entities.Course; | |
| 4 | import edu.ucsb.cs156.organic.entities.Staff; | |
| 5 | import edu.ucsb.cs156.organic.entities.User; | |
| 6 | import edu.ucsb.cs156.organic.repositories.CourseRepository; | |
| 7 | import edu.ucsb.cs156.organic.repositories.StaffRepository; | |
| 8 | import edu.ucsb.cs156.organic.repositories.UserRepository; | |
| 9 | import io.swagger.v3.oas.annotations.Operation; | |
| 10 | import io.swagger.v3.oas.annotations.Parameter; | |
| 11 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | ||
| 14 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 15 | ||
| 16 | ||
| 17 | import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | import org.springframework.format.annotation.DateTimeFormat; | |
| 19 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 20 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 21 | import org.springframework.web.bind.annotation.GetMapping; | |
| 22 | import org.springframework.web.bind.annotation.PostMapping; | |
| 23 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 24 | import org.springframework.web.bind.annotation.PutMapping; | |
| 25 | import org.springframework.web.bind.annotation.RequestBody; | |
| 26 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 27 | import org.springframework.web.bind.annotation.RequestParam; | |
| 28 | import org.springframework.web.bind.annotation.RestController; | |
| 29 | ||
| 30 | import edu.ucsb.cs156.organic.errors.EntityNotFoundException; | |
| 31 | ||
| 32 | import org.springframework.security.access.AccessDeniedException; | |
| 33 | import java.time.LocalDateTime; | |
| 34 | ||
| 35 | import javax.transaction.Transactional; | |
| 36 | import javax.validation.Valid; | |
| 37 | ||
| 38 | import java.util.Optional; | |
| 39 | ||
| 40 | ||
| 41 | @Tag(name = "Courses") | |
| 42 | @RequestMapping("/api/courses") | |
| 43 | @RestController | |
| 44 | @Slf4j | |
| 45 | public class CoursesController extends ApiController { | |
| 46 | ||
| 47 | @Autowired | |
| 48 | CourseRepository courseRepository; | |
| 49 | ||
| 50 | @Autowired | |
| 51 | StaffRepository courseStaffRepository; | |
| 52 | ||
| 53 | @Autowired | |
| 54 | UserRepository userRepository; | |
| 55 | ||
| 56 | @Operation(summary = "List all courses") | |
| 57 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 58 | @GetMapping("/all") | |
| 59 | public Iterable<Course> allCourses() { | |
| 60 | User u = getCurrentUser().getUser(); | |
| 61 | log.info("u={}", u); | |
| 62 |
1
1. allCourses : negated conditional → KILLED |
if (u.isAdmin()) { |
| 63 |
1
1. allCourses : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::allCourses → KILLED |
return courseRepository.findAll(); |
| 64 | } else { | |
| 65 |
1
1. allCourses : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::allCourses → KILLED |
return courseRepository.findCoursesStaffedByUser(u.getGithubId()); |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | @Operation(summary= "Get a single course") | |
| 70 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 71 | @GetMapping("") | |
| 72 | public Course getById( | |
| 73 | @Parameter(name="id") @RequestParam Long id) { | |
| 74 | User u = getCurrentUser().getUser(); | |
| 75 | ||
| 76 | Course course = courseRepository.findById(id) | |
| 77 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 78 | | |
| 79 |
1
1. getById : negated conditional → KILLED |
if(!u.isAdmin()){ |
| 80 | courseStaffRepository.findByCourseIdAndGithubId(id, u.getGithubId()) | |
| 81 |
1
1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$getById$1 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
| 82 | String.format("User %s is not authorized to get course %d", u.getGithubLogin(), id))); | |
| 83 | } | |
| 84 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getById → KILLED |
return course; |
| 85 | } | |
| 86 | ||
| 87 | @Operation(summary = "Create a new course") | |
| 88 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 89 | @PostMapping("/post") | |
| 90 | public Course postCourse( | |
| 91 | @Parameter(name = "name", description ="course name, e.g. CMPSC 156" ) @RequestParam String name, | |
| 92 | @Parameter(name = "school", description ="school abbreviation e.g. UCSB" ) @RequestParam String school, | |
| 93 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
| 94 | @Parameter(name = "startDate", description = "in iso format, i.e. YYYY-mm-ddTHH:MM:SS; e.g. 2023-10-01T00:00:00 see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate, | |
| 95 | @Parameter(name = "endDate", description = "in iso format, i.e. YYYY-mm-ddTHH:MM:SS; e.g. 2023-12-31T11:59:59 see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate, | |
| 96 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23" ) @RequestParam String githubOrg) | |
| 97 | throws JsonProcessingException { | |
| 98 | ||
| 99 | Course course = Course.builder() | |
| 100 | .name(name) | |
| 101 | .school(school) | |
| 102 | .term(term) | |
| 103 | .startDate(startDate) | |
| 104 | .endDate(endDate) | |
| 105 | .githubOrg(githubOrg) | |
| 106 | .build(); | |
| 107 | ||
| 108 | Course savedCourse = courseRepository.save(course); | |
| 109 | User u = getCurrentUser().getUser(); | |
| 110 | ||
| 111 | Staff courseStaff = Staff.builder() | |
| 112 | .courseId(savedCourse.getId()) | |
| 113 | .githubId(u.getGithubId()) | |
| 114 | .build(); | |
| 115 | ||
| 116 | log.info("courseStaff={}", courseStaff); | |
| 117 | courseStaffRepository.save(courseStaff); | |
| 118 | ||
| 119 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::postCourse → KILLED |
return savedCourse; |
| 120 | } | |
| 121 | ||
| 122 | @Operation(summary = "Add a staff member to a course") | |
| 123 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 124 | @PostMapping("/addStaff") | |
| 125 | public Staff addStaff( | |
| 126 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 127 | @Parameter(name = "githubLogin") @RequestParam String githubLogin) | |
| 128 | throws JsonProcessingException { | |
| 129 | ||
| 130 | Course course = courseRepository.findById(courseId) | |
| 131 |
1
1. lambda$addStaff$2 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$addStaff$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 132 | ||
| 133 | User user = userRepository.findByGithubLogin(githubLogin) | |
| 134 |
1
1. lambda$addStaff$3 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$addStaff$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, githubLogin.toString())); |
| 135 | ||
| 136 | Staff courseStaff = Staff.builder() | |
| 137 | .courseId(course.getId()) | |
| 138 | .githubId(user.getGithubId()) | |
| 139 | .user(user) | |
| 140 | .build(); | |
| 141 | ||
| 142 | courseStaff = courseStaffRepository.save(courseStaff); | |
| 143 | log.info("courseStaff={}", courseStaff); | |
| 144 | ||
| 145 |
1
1. addStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::addStaff → KILLED |
return courseStaff; |
| 146 | } | |
| 147 | ||
| 148 | @Operation(summary = "Remove staff from a course") | |
| 149 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
| 150 | @DeleteMapping("/staff") | |
| 151 | public Object deleteStaff( | |
| 152 | @Parameter(name = "id") @RequestParam Long id) throws JsonProcessingException { | |
| 153 | // Find staff member from id (also includes associated data like which course(s) they're staff in) | |
| 154 | Staff staff = courseStaffRepository.findById(id) | |
| 155 |
1
1. lambda$deleteStaff$4 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteStaff$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Staff.class, id.toString())); |
| 156 | | |
| 157 | // If instructor and not admin, check if user is staff in the same course of the staff member they're trying to remove | |
| 158 | User u = getCurrentUser().getUser(); | |
| 159 |
1
1. deleteStaff : negated conditional → KILLED |
if (!u.isAdmin()) { |
| 160 | Long courseId = staff.getCourseId(); | |
| 161 | log.info("staff={}\nthe parameter id={}", staff, id); | |
| 162 | courseStaffRepository.findByCourseIdAndGithubId(courseId, u.getGithubId()) // This find will succeed if the condition mentioned above is met | |
| 163 |
1
1. lambda$deleteStaff$5 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteStaff$5 → KILLED |
.orElseThrow(() -> new AccessDeniedException( // Throw an error if find fails (so, if they aren't in the same course) |
| 164 | String.format("User %s is not authorized to delete staff of id %d", u.getGithubLogin(), id))); | |
| 165 | } | |
| 166 | ||
| 167 |
1
1. deleteStaff : removed call to edu/ucsb/cs156/organic/repositories/StaffRepository::delete → KILLED |
courseStaffRepository.delete(staff); |
| 168 |
1
1. deleteStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteStaff → KILLED |
return genericMessage("Staff with id %s deleted".formatted(id)); |
| 169 | } | |
| 170 | ||
| 171 | @Operation(summary = "Get Staff for course") | |
| 172 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 173 | @GetMapping("/getStaff") | |
| 174 | public Iterable<Staff> getStaff( | |
| 175 | @Parameter(name = "courseId") @RequestParam Long courseId | |
| 176 | ) | |
| 177 | throws JsonProcessingException { | |
| 178 | ||
| 179 | Course course = courseRepository.findById(courseId) | |
| 180 |
1
1. lambda$getStaff$6 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$getStaff$6 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 181 | ||
| 182 | Iterable<Staff> courseStaff = courseStaffRepository.findByCourseId(course.getId()); | |
| 183 |
1
1. getStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getStaff → KILLED |
return courseStaff; |
| 184 | } | |
| 185 | ||
| 186 | @Operation(summary = "Update a course") | |
| 187 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
| 188 | @PutMapping("") | |
| 189 | public Course updateCourse( | |
| 190 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 191 | @RequestBody @Valid Course incoming) throws JsonProcessingException { | |
| 192 | | |
| 193 | User u = getCurrentUser().getUser(); | |
| 194 | Course course = courseRepository.findById(courseId) | |
| 195 |
1
1. lambda$updateCourse$7 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$7 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 196 |
1
1. updateCourse : negated conditional → KILLED |
if(!u.isAdmin()) |
| 197 | courseStaffRepository.findByCourseIdAndGithubId(courseId, u.getGithubId()) | |
| 198 |
1
1. lambda$updateCourse$8 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$8 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
| 199 | String.format("User %s is not authorized to update course %d", u.getGithubLogin(), courseId))); | |
| 200 | ||
| 201 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setName → KILLED |
course.setName(incoming.getName()); |
| 202 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setSchool → KILLED |
course.setSchool(incoming.getSchool()); |
| 203 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setTerm → KILLED |
course.setTerm(incoming.getTerm()); |
| 204 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setStartDate → KILLED |
course.setStartDate(incoming.getStartDate()); |
| 205 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setEndDate → KILLED |
course.setEndDate(incoming.getEndDate()); |
| 206 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setGithubOrg → KILLED |
course.setGithubOrg(incoming.getGithubOrg()); |
| 207 | ||
| 208 | courseRepository.save(course); | |
| 209 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::updateCourse → KILLED |
return course; |
| 210 | } | |
| 211 | ||
| 212 | @Transactional | |
| 213 | @Operation(summary = "Delete a course") | |
| 214 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
| 215 | @DeleteMapping("") | |
| 216 | public Object deleteCourse( | |
| 217 | @Parameter(name = "id") @RequestParam Long id) throws JsonProcessingException { | |
| 218 | | |
| 219 | User u = getCurrentUser().getUser(); | |
| 220 | Course course = courseRepository.findById(id) | |
| 221 |
1
1. lambda$deleteCourse$9 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$9 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id.toString())); |
| 222 |
1
1. deleteCourse : negated conditional → KILLED |
if(!u.isAdmin()) |
| 223 | courseStaffRepository.findByCourseIdAndGithubId(id, u.getGithubId()) | |
| 224 |
1
1. lambda$deleteCourse$10 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$10 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
| 225 | String.format("User %s is not authorized to delete course %d", u.getGithubLogin(), id))); | |
| 226 | ||
| 227 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/organic/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
| 228 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/organic/repositories/StaffRepository::deleteByCourseId → KILLED |
courseStaffRepository.deleteByCourseId(id); |
| 229 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteCourse → KILLED |
return genericMessage("Course with id %s deleted".formatted(id)); |
| 230 | } | |
| 231 | } | |
Mutations | ||
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 65 |
1.1 |
|
| 77 |
1.1 |
|
| 79 |
1.1 |
|
| 81 |
1.1 |
|
| 84 |
1.1 |
|
| 119 |
1.1 |
|
| 131 |
1.1 |
|
| 134 |
1.1 |
|
| 145 |
1.1 |
|
| 155 |
1.1 |
|
| 159 |
1.1 |
|
| 163 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 180 |
1.1 |
|
| 183 |
1.1 |
|
| 195 |
1.1 |
|
| 196 |
1.1 |
|
| 198 |
1.1 |
|
| 201 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 204 |
1.1 |
|
| 205 |
1.1 |
|
| 206 |
1.1 |
|
| 209 |
1.1 |
|
| 221 |
1.1 |
|
| 222 |
1.1 |
|
| 224 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |