| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.courses.entities.PSCourse; | |
| 4 | import edu.ucsb.cs156.courses.entities.User; | |
| 5 | import edu.ucsb.cs156.courses.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.courses.models.CurrentUser; | |
| 7 | import edu.ucsb.cs156.courses.repositories.PSCourseRepository; | |
| 8 | import io.swagger.annotations.Api; | |
| 9 | import io.swagger.annotations.ApiOperation; | |
| 10 | import io.swagger.annotations.ApiParam; | |
| 11 | import io.swagger.annotations.ApiResponse; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | ||
| 14 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 15 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 16 | import com.fasterxml.jackson.databind.JsonNode; | |
| 17 | ||
| 18 | import org.springframework.beans.factory.annotation.Autowired; | |
| 19 | import org.springframework.http.ResponseEntity; | |
| 20 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 21 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 22 | import org.springframework.web.bind.annotation.GetMapping; | |
| 23 | import org.springframework.web.bind.annotation.PostMapping; | |
| 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 javax.validation.Valid; | |
| 31 | import java.util.Iterator; | |
| 32 | import java.util.Optional; | |
| 33 | import java.util.ArrayList; | |
| 34 | ||
| 35 | import edu.ucsb.cs156.courses.entities.PersonalSchedule; | |
| 36 | import edu.ucsb.cs156.courses.repositories.PersonalScheduleRepository; | |
| 37 | import edu.ucsb.cs156.courses.services.UCSBCurriculumService; | |
| 38 | import edu.ucsb.cs156.courses.errors.BadEnrollCdException; | |
| 39 | ||
| 40 | @Api(description = "PSCourse") | |
| 41 | @RequestMapping("/api/courses") | |
| 42 | @RestController | |
| 43 | @Slf4j | |
| 44 | public class PSCourseController extends ApiController { | |
| 45 | ||
| 46 | @Autowired | |
| 47 | PSCourseRepository coursesRepository; | |
| 48 | @Autowired | |
| 49 | PersonalScheduleRepository personalScheduleRepository; | |
| 50 | @Autowired | |
| 51 | UCSBCurriculumService ucsbCurriculumService; | |
| 52 | @Autowired | |
| 53 | ObjectMapper mapper; | |
| 54 | ||
| 55 | @ApiOperation(value = "List all courses (admin)") | |
| 56 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 57 | @GetMapping("/admin/all") | |
| 58 | public Iterable<PSCourse> allUsersCourses() { | |
| 59 | Iterable<PSCourse> courses = coursesRepository.findAll(); | |
| 60 |
1
1. allUsersCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::allUsersCourses → KILLED |
return courses; |
| 61 | } | |
| 62 | ||
| 63 | @ApiOperation(value = "List all courses (user)") | |
| 64 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 65 | @GetMapping("/user/all") | |
| 66 | public Iterable<PSCourse> thisUsersCourses() { | |
| 67 | CurrentUser currentUser = getCurrentUser(); | |
| 68 | Iterable<PSCourse> courses = coursesRepository.findAllByUserId(currentUser.getUser().getId()); | |
| 69 |
1
1. thisUsersCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::thisUsersCourses → KILLED |
return courses; |
| 70 | } | |
| 71 | ||
| 72 | @ApiOperation(value = "List all courses for a specified psId (admin)") | |
| 73 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 74 | @GetMapping("/admin/psid/all") | |
| 75 | public Iterable<PSCourse> allCoursesForPsId( | |
| 76 | @ApiParam("psId") @RequestParam Long psId) { | |
| 77 | Iterable<PSCourse> courses = coursesRepository.findAllByPsId(psId); | |
| 78 |
1
1. allCoursesForPsId : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::allCoursesForPsId → KILLED |
return courses; |
| 79 | } | |
| 80 | ||
| 81 | @ApiOperation(value = "List all courses for a specified psId (user)") | |
| 82 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 83 | @GetMapping("/user/psid/all") | |
| 84 | public Iterable<PSCourse> thisUsersCoursesForPsId( | |
| 85 | @ApiParam("psId") @RequestParam Long psId) { | |
| 86 | User currentUser = getCurrentUser().getUser(); | |
| 87 | Iterable<PSCourse> courses = coursesRepository.findAllByPsIdAndUser(psId, currentUser); | |
| 88 |
1
1. thisUsersCoursesForPsId : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::thisUsersCoursesForPsId → KILLED |
return courses; |
| 89 | } | |
| 90 | ||
| 91 | @ApiOperation(value = "Get a single course (admin)") | |
| 92 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 93 | @GetMapping("/admin") | |
| 94 | public PSCourse getCourseById_admin( | |
| 95 | @ApiParam("id") @RequestParam Long id) { | |
| 96 | PSCourse courses = coursesRepository.findById(id) | |
| 97 |
1
1. lambda$getCourseById_admin$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$getCourseById_admin$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 98 | ||
| 99 |
1
1. getCourseById_admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::getCourseById_admin → KILLED |
return courses; |
| 100 | } | |
| 101 | ||
| 102 | @ApiOperation(value = "Get a single course (user)") | |
| 103 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 104 | @GetMapping("/user") | |
| 105 | public PSCourse getCourseById( | |
| 106 | @ApiParam("id") @RequestParam Long id) { | |
| 107 | User currentUser = getCurrentUser().getUser(); | |
| 108 | PSCourse courses = coursesRepository.findByIdAndUser(id, currentUser) | |
| 109 |
1
1. lambda$getCourseById$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$getCourseById$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 110 | ||
| 111 |
1
1. getCourseById : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::getCourseById → KILLED |
return courses; |
| 112 | } | |
| 113 | ||
| 114 | @ApiOperation(value = "Create a new course") | |
| 115 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 116 | @PostMapping("/post") | |
| 117 | public ArrayList<PSCourse> postCourses( | |
| 118 | @ApiParam("enrollCd") @RequestParam String enrollCd, | |
| 119 | @ApiParam("psId") @RequestParam Long psId) throws JsonProcessingException { | |
| 120 | CurrentUser currentUser = getCurrentUser(); | |
| 121 | log.info("currentUser={}", currentUser); | |
| 122 | ||
| 123 | PersonalSchedule checkPsId = personalScheduleRepository.findByIdAndUser(psId, currentUser.getUser()) | |
| 124 |
1
1. lambda$postCourses$2 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$postCourses$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId)); |
| 125 | ||
| 126 | String body = ucsbCurriculumService.getAllSections(enrollCd, checkPsId.getQuarter()); | |
| 127 |
2
1. postCourses : negated conditional → KILLED 2. postCourses : negated conditional → KILLED |
if(body.equals("{\"error\": \"401: Unauthorized\"}") || body.equals("{\"error\": \"Enroll code doesn't exist in that quarter.\"}")){ |
| 128 | throw new BadEnrollCdException(enrollCd); | |
| 129 | } | |
| 130 | ||
| 131 | String enrollCdPrimary = null; | |
| 132 | boolean hasSecondary = false; | |
| 133 | Iterator<JsonNode> it = mapper.readTree(body).path("classSections").elements(); | |
| 134 |
1
1. postCourses : negated conditional → KILLED |
while (it.hasNext()) { |
| 135 | JsonNode classSection = it.next(); | |
| 136 | String section = classSection.path("section").asText(); | |
| 137 |
1
1. postCourses : negated conditional → KILLED |
if (section.endsWith("00")) { |
| 138 | String currentEnrollCd = classSection.path("enrollCode").asText(); | |
| 139 | enrollCdPrimary = currentEnrollCd; | |
| 140 |
1
1. postCourses : negated conditional → KILLED |
if (hasSecondary) |
| 141 | break; | |
| 142 | } else { | |
| 143 | hasSecondary = true; | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 |
1
1. postCourses : negated conditional → KILLED |
if (enrollCdPrimary == null) { |
| 148 | enrollCdPrimary = enrollCd; | |
| 149 | hasSecondary = false; | |
| 150 | } | |
| 151 | ||
| 152 |
1
1. postCourses : negated conditional → KILLED |
if (coursesRepository.findByPsIdAndEnrollCd(psId, enrollCdPrimary).isPresent()) { |
| 153 | throw new IllegalArgumentException("class exists in schedule"); | |
| 154 | } | |
| 155 | | |
| 156 | ArrayList<PSCourse> savedCourses = new ArrayList<>(); | |
| 157 | ||
| 158 |
1
1. postCourses : negated conditional → KILLED |
if (!enrollCdPrimary.equals(enrollCd)) { |
| 159 | String enrollCdSecondary = enrollCd; | |
| 160 | PSCourse secondary = new PSCourse(); | |
| 161 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setUser → KILLED |
secondary.setUser(currentUser.getUser()); |
| 162 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setEnrollCd → KILLED |
secondary.setEnrollCd(enrollCdSecondary); |
| 163 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setPsId → KILLED |
secondary.setPsId(psId); |
| 164 | PSCourse savedSecondary = coursesRepository.save(secondary); | |
| 165 | savedCourses.add(savedSecondary); | |
| 166 |
1
1. postCourses : negated conditional → KILLED |
} else if (hasSecondary) { |
| 167 | throw new IllegalArgumentException(enrollCd + " is for a course with sections; please add a specific section and the lecture will be automatically added"); | |
| 168 | } | |
| 169 | ||
| 170 | PSCourse primary = new PSCourse(); | |
| 171 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setUser → KILLED |
primary.setUser(currentUser.getUser()); |
| 172 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setEnrollCd → KILLED |
primary.setEnrollCd(enrollCdPrimary); |
| 173 |
1
1. postCourses : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setPsId → KILLED |
primary.setPsId(psId); |
| 174 | PSCourse savedPrimary = coursesRepository.save(primary); | |
| 175 | savedCourses.add(savedPrimary); | |
| 176 |
1
1. postCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::postCourses → KILLED |
return savedCourses; |
| 177 | } | |
| 178 | ||
| 179 | @ApiOperation(value = "Delete a course (admin)") | |
| 180 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 181 | @DeleteMapping("/admin") | |
| 182 | public Object deleteCourses_Admin( | |
| 183 | @ApiParam("id") @RequestParam Long id) { | |
| 184 | PSCourse courses = coursesRepository.findById(id) | |
| 185 |
1
1. lambda$deleteCourses_Admin$3 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$deleteCourses_Admin$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 186 | ||
| 187 |
1
1. deleteCourses_Admin : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED |
coursesRepository.delete(courses); |
| 188 | ||
| 189 |
1
1. deleteCourses_Admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::deleteCourses_Admin → KILLED |
return genericMessage("PSCourse with id %s deleted".formatted(id)); |
| 190 | } | |
| 191 | ||
| 192 | @ApiOperation(value = "Delete a course (user)") | |
| 193 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 194 | @DeleteMapping("/user") | |
| 195 | public Object deleteCourses( | |
| 196 | @ApiParam("id") @RequestParam Long id) throws JsonProcessingException { | |
| 197 | User currentUser = getCurrentUser().getUser(); | |
| 198 | PSCourse psCourse = coursesRepository.findByIdAndUser(id, currentUser) | |
| 199 |
1
1. lambda$deleteCourses$4 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$deleteCourses$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 200 | long psId = psCourse.getPsId(); | |
| 201 | PersonalSchedule checkPsId = personalScheduleRepository.findByIdAndUser(psId, currentUser) | |
| 202 |
1
1. lambda$deleteCourses$5 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$deleteCourses$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId)); |
| 203 | | |
| 204 | String body = ucsbCurriculumService.getAllSections(psCourse.getEnrollCd(), checkPsId.getQuarter()); | |
| 205 |
2
1. deleteCourses : negated conditional → KILLED 2. deleteCourses : negated conditional → KILLED |
if (body.equals("{\"error\": \"401: Unauthorized\"}") || body.equals("{\"error\": \"Enroll code doesn't exist in that quarter.\"}")) { |
| 206 |
1
1. deleteCourses : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED |
coursesRepository.delete(psCourse); |
| 207 |
1
1. deleteCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::deleteCourses → KILLED |
return genericMessage("PSCourse with id %s deleted".formatted(id)); |
| 208 | } | |
| 209 | | |
| 210 | Iterator<JsonNode> it = mapper.readTree(body).path("classSections").elements(); | |
| 211 | Optional<Long> primaryId = Optional.empty(); | |
| 212 | Optional<Long> secondaryId = Optional.empty(); | |
| 213 |
1
1. deleteCourses : negated conditional → KILLED |
while (it.hasNext()) { |
| 214 | JsonNode classSection = it.next(); | |
| 215 | String section = classSection.path("section").asText(); | |
| 216 | String currentEnrollCd = classSection.path("enrollCode").asText(); | |
| 217 | Optional<PSCourse> currentPsCourse = coursesRepository.findByPsIdAndEnrollCd(psId, currentEnrollCd); | |
| 218 |
1
1. deleteCourses : negated conditional → KILLED |
if (!currentPsCourse.isPresent()) |
| 219 | continue; | |
| 220 | Optional<Long> idOpt = Optional.of(currentPsCourse.get().getId()); | |
| 221 |
1
1. deleteCourses : negated conditional → KILLED |
if (section.endsWith("00")) |
| 222 | primaryId = idOpt; | |
| 223 | else | |
| 224 | secondaryId = idOpt; | |
| 225 |
1
1. deleteCourses : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED |
coursesRepository.delete(currentPsCourse.get()); |
| 226 | } | |
| 227 | | |
| 228 |
2
1. deleteCourses : negated conditional → KILLED 2. deleteCourses : negated conditional → KILLED |
if (primaryId.isPresent() && secondaryId.isPresent()) { |
| 229 |
1
1. deleteCourses : negated conditional → KILLED |
if (primaryId.get() == id) |
| 230 |
1
1. deleteCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::deleteCourses → KILLED |
return genericMessage("PSCourse with id %s and matching secondary with id %s deleted".formatted(id, secondaryId.get())); |
| 231 | else | |
| 232 |
1
1. deleteCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::deleteCourses → KILLED |
return genericMessage("PSCourse with id %s and matching primary with id %s deleted".formatted(id, primaryId.get())); |
| 233 | } | |
| 234 | | |
| 235 |
1
1. deleteCourses : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::deleteCourses → KILLED |
return genericMessage("PSCourse with id %s deleted".formatted(id)); |
| 236 | } | |
| 237 | ||
| 238 | @ApiOperation(value = "Update a single Course (admin)") | |
| 239 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 240 | @PutMapping("/admin") | |
| 241 | public PSCourse putCourseById_admin( | |
| 242 | @ApiParam("id") @RequestParam Long id, | |
| 243 | @RequestBody @Valid PSCourse incomingCourses) { | |
| 244 | PSCourse courses = coursesRepository.findById(id) | |
| 245 |
1
1. lambda$putCourseById_admin$6 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$putCourseById_admin$6 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 246 | ||
| 247 |
1
1. putCourseById_admin : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setEnrollCd → KILLED |
courses.setEnrollCd(incomingCourses.getEnrollCd()); |
| 248 |
1
1. putCourseById_admin : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setPsId → KILLED |
courses.setPsId(incomingCourses.getPsId()); |
| 249 | ||
| 250 | coursesRepository.save(courses); | |
| 251 | ||
| 252 |
1
1. putCourseById_admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::putCourseById_admin → KILLED |
return courses; |
| 253 | } | |
| 254 | ||
| 255 | @ApiOperation(value = "Update a single course (user)") | |
| 256 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 257 | @PutMapping("/user") | |
| 258 | public PSCourse putCoursesById( | |
| 259 | @ApiParam("id") @RequestParam Long id, | |
| 260 | @RequestBody @Valid PSCourse incomingCourses) { | |
| 261 | User currentUser = getCurrentUser().getUser(); | |
| 262 | PSCourse courses = coursesRepository.findByIdAndUser(id, currentUser) | |
| 263 |
1
1. lambda$putCoursesById$7 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::lambda$putCoursesById$7 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PSCourse.class, id)); |
| 264 | ||
| 265 |
1
1. putCoursesById : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setEnrollCd → KILLED |
courses.setEnrollCd(incomingCourses.getEnrollCd()); |
| 266 |
1
1. putCoursesById : removed call to edu/ucsb/cs156/courses/entities/PSCourse::setPsId → KILLED |
courses.setPsId(incomingCourses.getPsId()); |
| 267 | ||
| 268 | coursesRepository.save(courses); | |
| 269 | ||
| 270 |
1
1. putCoursesById : replaced return value with null for edu/ucsb/cs156/courses/controllers/PSCourseController::putCoursesById → KILLED |
return courses; |
| 271 | } | |
| 272 | } | |
Mutations | ||
| 60 |
1.1 |
|
| 69 |
1.1 |
|
| 78 |
1.1 |
|
| 88 |
1.1 |
|
| 97 |
1.1 |
|
| 99 |
1.1 |
|
| 109 |
1.1 |
|
| 111 |
1.1 |
|
| 124 |
1.1 |
|
| 127 |
1.1 2.2 |
|
| 134 |
1.1 |
|
| 137 |
1.1 |
|
| 140 |
1.1 |
|
| 147 |
1.1 |
|
| 152 |
1.1 |
|
| 158 |
1.1 |
|
| 161 |
1.1 |
|
| 162 |
1.1 |
|
| 163 |
1.1 |
|
| 166 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 |
|
| 173 |
1.1 |
|
| 176 |
1.1 |
|
| 185 |
1.1 |
|
| 187 |
1.1 |
|
| 189 |
1.1 |
|
| 199 |
1.1 |
|
| 202 |
1.1 |
|
| 205 |
1.1 2.2 |
|
| 206 |
1.1 |
|
| 207 |
1.1 |
|
| 213 |
1.1 |
|
| 218 |
1.1 |
|
| 221 |
1.1 |
|
| 225 |
1.1 |
|
| 228 |
1.1 2.2 |
|
| 229 |
1.1 |
|
| 230 |
1.1 |
|
| 232 |
1.1 |
|
| 235 |
1.1 |
|
| 245 |
1.1 |
|
| 247 |
1.1 |
|
| 248 |
1.1 |
|
| 252 |
1.1 |
|
| 263 |
1.1 |
|
| 265 |
1.1 |
|
| 266 |
1.1 |
|
| 270 |
1.1 |