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