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