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 | import org.springframework.beans.factory.annotation.Autowired; | |
17 | import org.springframework.format.annotation.DateTimeFormat; | |
18 | import org.springframework.security.access.prepost.PreAuthorize; | |
19 | import org.springframework.web.bind.annotation.DeleteMapping; | |
20 | import org.springframework.web.bind.annotation.GetMapping; | |
21 | import org.springframework.web.bind.annotation.DeleteMapping; | |
22 | import org.springframework.web.bind.annotation.PostMapping; | |
23 | import org.springframework.web.bind.annotation.PutMapping; | |
24 | import org.springframework.web.bind.annotation.RequestMapping; | |
25 | import org.springframework.web.bind.annotation.RequestParam; | |
26 | import org.springframework.web.bind.annotation.RestController; | |
27 | ||
28 | import edu.ucsb.cs156.organic.errors.EntityNotFoundException; | |
29 | import org.springframework.security.access.AccessDeniedException; | |
30 | ||
31 | import java.time.LocalDateTime; | |
32 | ||
33 | import javax.transaction.Transactional; | |
34 | import javax.validation.Valid; | |
35 | ||
36 | import java.util.Optional; | |
37 | ||
38 | // added this for date comparison | |
39 | import org.springframework.http.ResponseEntity; | |
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 by id") | |
70 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
71 | @GetMapping("/get") | |
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 | ||
85 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getById → KILLED |
return course; |
86 | } | |
87 | ||
88 | @Operation(summary = "Create a new course") | |
89 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
90 | @PostMapping("/post") | |
91 | public Object postCourse( | |
92 | @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name, | |
93 | @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school, | |
94 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
95 | @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, | |
96 | @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, | |
97 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23") @RequestParam String githubOrg) | |
98 | throws JsonProcessingException { | |
99 | ||
100 | Course course; | |
101 | ||
102 | course = Course.builder() | |
103 | .name(name) | |
104 | .school(school) | |
105 | .term(term) | |
106 | .startDate(startDate) | |
107 | .endDate(endDate) | |
108 | .githubOrg(githubOrg) | |
109 | .build(); | |
110 | ||
111 |
2
1. postCourse : negated conditional → KILLED 2. postCourse : negated conditional → KILLED |
if (startDate.isAfter(endDate) || startDate.isEqual(endDate)){ |
112 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::postCourse → KILLED |
return ResponseEntity.badRequest().body("The value of Start Date should be lower than the value of End Date."); |
113 | } | |
114 | ||
115 | Course savedCourse = courseRepository.save(course); | |
116 | User u = getCurrentUser().getUser(); | |
117 | ||
118 | Staff courseStaff = Staff.builder() | |
119 | .courseId(savedCourse.getId()) | |
120 | .githubId(u.getGithubId()) | |
121 | .build(); | |
122 | ||
123 | log.info("courseStaff={}", courseStaff); | |
124 | courseStaffRepository.save(courseStaff); | |
125 | ||
126 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::postCourse → KILLED |
return savedCourse; |
127 | } | |
128 | ||
129 | @Operation(summary = "Add a staff member to a course") | |
130 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
131 | @PostMapping("/addStaff") | |
132 | public Staff addStaff( | |
133 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
134 | @Parameter(name = "githubLogin") @RequestParam String githubLogin) | |
135 | throws JsonProcessingException { | |
136 | ||
137 | Course course = courseRepository.findById(courseId) | |
138 |
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())); |
139 | ||
140 | User user = userRepository.findByGithubLogin(githubLogin) | |
141 |
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())); |
142 | ||
143 | Staff courseStaff = Staff.builder() | |
144 | .courseId(course.getId()) | |
145 | .githubId(user.getGithubId()) | |
146 | .user(user) | |
147 | .build(); | |
148 | ||
149 | courseStaff = courseStaffRepository.save(courseStaff); | |
150 | log.info("courseStaff={}", courseStaff); | |
151 | ||
152 |
1
1. addStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::addStaff → KILLED |
return courseStaff; |
153 | } | |
154 | ||
155 | @Operation(summary = "Get Staff for course") | |
156 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
157 | @GetMapping("/getStaff") | |
158 | public Iterable<Staff> getStaff( | |
159 | @Parameter(name = "courseId") @RequestParam Long courseId) | |
160 | throws JsonProcessingException { | |
161 | ||
162 | Course course = courseRepository.findById(courseId) | |
163 |
1
1. lambda$getStaff$4 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$getStaff$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
164 | ||
165 | Iterable<Staff> courseStaff = courseStaffRepository.findByCourseId(course.getId()); | |
166 |
1
1. getStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getStaff → KILLED |
return courseStaff; |
167 | } | |
168 | ||
169 | @Operation(summary = "Delete a Course Staff by id") | |
170 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
171 | @DeleteMapping("/staff") | |
172 | public Object deleteStaff( | |
173 | @Parameter(name = "id") @RequestParam Long id) { | |
174 | Staff staff = courseStaffRepository.findById(id) | |
175 |
1
1. lambda$deleteStaff$5 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteStaff$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Staff.class, id.toString())); |
176 | ||
177 |
1
1. deleteStaff : removed call to edu/ucsb/cs156/organic/repositories/StaffRepository::delete → KILLED |
courseStaffRepository.delete(staff); |
178 |
1
1. deleteStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteStaff → KILLED |
return genericMessage("Staff with id %s is deleted".formatted(id)); |
179 | } | |
180 | ||
181 | @Operation(summary = "Update information for a course") | |
182 | // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member | |
183 | // for the course | |
184 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
185 | @PutMapping("/update") | |
186 | public Object updateCourse( | |
187 | @Parameter(name = "id") @RequestParam Long id, | |
188 | @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name, | |
189 | @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school, | |
190 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
191 | @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, | |
192 | @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, | |
193 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23") @RequestParam String githubOrg) | |
194 | throws JsonProcessingException { | |
195 | ||
196 |
2
1. updateCourse : negated conditional → KILLED 2. updateCourse : negated conditional → KILLED |
if (startDate.isAfter(endDate) || startDate.isEqual(endDate)){ |
197 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::updateCourse → KILLED |
return ResponseEntity.badRequest().body("The value of Start Date should be lower than the value of End Date."); |
198 | } | |
199 | ||
200 | Course course = courseRepository.findById(id) | |
201 |
1
1. lambda$updateCourse$6 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$6 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id.toString())); |
202 | ||
203 | // Check if the current user is a staff member for this course or an admin. If | |
204 | // not, throw AccessDeniedException | |
205 | ||
206 | User u = getCurrentUser().getUser(); | |
207 |
1
1. updateCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
208 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
209 |
1
1. lambda$updateCourse$7 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$7 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
210 | "User is not a staff member for this course")); | |
211 | } | |
212 | ||
213 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setName → KILLED |
course.setName(name); |
214 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setSchool → KILLED |
course.setSchool(school); |
215 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setTerm → KILLED |
course.setTerm(term); |
216 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setStartDate → KILLED |
course.setStartDate(startDate); |
217 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setEndDate → KILLED |
course.setEndDate(endDate); |
218 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setGithubOrg → KILLED |
course.setGithubOrg(githubOrg); |
219 | ||
220 | course = courseRepository.save(course); | |
221 | log.info("course={}", course); | |
222 | ||
223 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::updateCourse → KILLED |
return course; |
224 | } | |
225 | ||
226 | // delete a course if the user is an admin or instructor for the course | |
227 | @Operation(summary = "Delete a course") | |
228 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
229 | @DeleteMapping("/delete") | |
230 | public Course deleteCourse( | |
231 | @Parameter(name = "id") @RequestParam Long id) | |
232 | throws JsonProcessingException { | |
233 | ||
234 | Course course = courseRepository.findById(id) | |
235 |
1
1. lambda$deleteCourse$8 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$8 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id.toString())); |
236 | ||
237 | // Check if the current user is a staff member for this course or an admin. If | |
238 | // not, throw AccessDeniedException | |
239 | ||
240 | User u = getCurrentUser().getUser(); | |
241 |
1
1. deleteCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
242 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
243 |
1
1. lambda$deleteCourse$9 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$9 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
244 | "User is not a staff member for this course")); | |
245 | } | |
246 | ||
247 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/organic/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
248 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteCourse → KILLED |
return course; |
249 | } | |
250 | ||
251 | } | |
Mutations | ||
62 |
1.1 |
|
63 |
1.1 |
|
65 |
1.1 |
|
77 |
1.1 |
|
79 |
1.1 |
|
81 |
1.1 |
|
85 |
1.1 |
|
111 |
1.1 2.2 |
|
112 |
1.1 |
|
126 |
1.1 |
|
138 |
1.1 |
|
141 |
1.1 |
|
152 |
1.1 |
|
163 |
1.1 |
|
166 |
1.1 |
|
175 |
1.1 |
|
177 |
1.1 |
|
178 |
1.1 |
|
196 |
1.1 2.2 |
|
197 |
1.1 |
|
201 |
1.1 |
|
207 |
1.1 |
|
209 |
1.1 |
|
213 |
1.1 |
|
214 |
1.1 |
|
215 |
1.1 |
|
216 |
1.1 |
|
217 |
1.1 |
|
218 |
1.1 |
|
223 |
1.1 |
|
235 |
1.1 |
|
241 |
1.1 |
|
243 |
1.1 |
|
247 |
1.1 |
|
248 |
1.1 |