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 | @Tag(name = "Courses") | |
39 | @RequestMapping("/api/course") | |
40 | @RestController | |
41 | @Slf4j | |
42 | public class CoursesController extends ApiController { | |
43 | ||
44 | @Autowired | |
45 | CourseRepository courseRepository; | |
46 | ||
47 | @Autowired | |
48 | StaffRepository courseStaffRepository; | |
49 | ||
50 | @Autowired | |
51 | UserRepository userRepository; | |
52 | ||
53 | @Operation(summary = "List all courses") | |
54 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
55 | @GetMapping("/getAll") | |
56 | public Iterable<Course> allCourses() { | |
57 | User u = getCurrentUser().getUser(); | |
58 | log.info("u={}", u); | |
59 |
1
1. allCourses : negated conditional → KILLED |
if (u.isAdmin()) { |
60 |
1
1. allCourses : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::allCourses → KILLED |
return courseRepository.findAll(); |
61 | } else { | |
62 |
1
1. allCourses : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::allCourses → KILLED |
return courseRepository.findCoursesStaffedByUser(u.getGithubId()); |
63 | } | |
64 | } | |
65 | ||
66 | @Operation(summary= "Get a single course by id") | |
67 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
68 | @GetMapping("/get") | |
69 | public Course getById( | |
70 | @Parameter(name="id") @RequestParam Long id) { | |
71 | User u = getCurrentUser().getUser(); | |
72 | ||
73 | Course course = courseRepository.findById(id) | |
74 |
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)); |
75 | | |
76 |
1
1. getById : negated conditional → KILLED |
if(!u.isAdmin()){ |
77 | courseStaffRepository.findByCourseIdAndGithubId(id, u.getGithubId()) | |
78 |
1
1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$getById$1 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
79 | String.format("User %s is not authorized to get course %d", u.getGithubLogin(), id))); | |
80 | } | |
81 | ||
82 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getById → KILLED |
return course; |
83 | } | |
84 | ||
85 | @Operation(summary = "Create a new course") | |
86 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
87 | @PostMapping("/create") | |
88 | public Course postCourse( | |
89 | @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name, | |
90 | @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school, | |
91 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
92 | @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, | |
93 | @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, | |
94 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23") @RequestParam String githubOrg) | |
95 | throws JsonProcessingException { | |
96 | ||
97 | ||
98 | // Check if endDate is after startDate | |
99 |
1
1. postCourse : negated conditional → KILLED |
if (endDate.isBefore(startDate)) { |
100 | throw new IllegalArgumentException("End date must be after start date."); | |
101 | } | |
102 | ||
103 | Course course = Course.builder() | |
104 | .name(name) | |
105 | .school(school) | |
106 | .term(term) | |
107 | .startDate(startDate) | |
108 | .endDate(endDate) | |
109 | .githubOrg(githubOrg) | |
110 | .build(); | |
111 | ||
112 | Course savedCourse = courseRepository.save(course); | |
113 | User u = getCurrentUser().getUser(); | |
114 | ||
115 | Staff courseStaff = Staff.builder() | |
116 | .courseId(savedCourse.getId()) | |
117 | .githubId(u.getGithubId()) | |
118 | .build(); | |
119 | ||
120 | log.info("courseStaff={}", courseStaff); | |
121 | courseStaffRepository.save(courseStaff); | |
122 | ||
123 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::postCourse → KILLED |
return savedCourse; |
124 | } | |
125 | ||
126 | @Operation(summary = "Add a staff member to a course") | |
127 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
128 | @PostMapping("/staff/create") | |
129 | public Staff addStaff( | |
130 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
131 | @Parameter(name = "githubLogin") @RequestParam String githubLogin) | |
132 | throws JsonProcessingException { | |
133 | ||
134 | Course course = courseRepository.findById(courseId) | |
135 |
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())); |
136 | ||
137 | User user = userRepository.findByGithubLogin(githubLogin) | |
138 |
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())); |
139 | ||
140 | Staff courseStaff = Staff.builder() | |
141 | .courseId(course.getId()) | |
142 | .githubId(user.getGithubId()) | |
143 | .user(user) | |
144 | .build(); | |
145 | ||
146 | courseStaff = courseStaffRepository.save(courseStaff); | |
147 | log.info("courseStaff={}", courseStaff); | |
148 | ||
149 |
1
1. addStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::addStaff → KILLED |
return courseStaff; |
150 | } | |
151 | ||
152 | @Operation(summary = "Get Staffs for the course") | |
153 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
154 | @GetMapping("/staff/all") | |
155 | public Iterable<Staff> getStaff( | |
156 | @Parameter(name = "courseId") @RequestParam Long courseId) | |
157 | throws JsonProcessingException { | |
158 | ||
159 | Course course = courseRepository.findById(courseId) | |
160 |
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())); |
161 | ||
162 | Iterable<Staff> courseStaff = courseStaffRepository.findByCourseId(course.getId()); | |
163 |
1
1. getStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getStaff → KILLED |
return courseStaff; |
164 | } | |
165 | ||
166 | @Operation(summary = "Delete a Course Staff by id") | |
167 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
168 | @DeleteMapping("/staff") | |
169 | public Object deleteStaff( | |
170 | @Parameter(name = "id") @RequestParam Long id) { | |
171 | Staff staff = courseStaffRepository.findById(id) | |
172 |
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())); |
173 | ||
174 |
1
1. deleteStaff : removed call to edu/ucsb/cs156/organic/repositories/StaffRepository::delete → KILLED |
courseStaffRepository.delete(staff); |
175 |
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)); |
176 | } | |
177 | ||
178 | @Operation(summary = "Update information for a course") | |
179 | // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member | |
180 | // for the course | |
181 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
182 | @PutMapping("/update") | |
183 | public Course updateCourse( | |
184 | @Parameter(name = "id") @RequestParam Long id, | |
185 | @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name, | |
186 | @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school, | |
187 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
188 | @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, | |
189 | @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, | |
190 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23") @RequestParam String githubOrg) | |
191 | throws JsonProcessingException { | |
192 | ||
193 | Course course = courseRepository.findById(id) | |
194 |
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())); |
195 | ||
196 | // Check if the current user is a staff member for this course or an admin. If | |
197 | // not, throw AccessDeniedException | |
198 | ||
199 | User u = getCurrentUser().getUser(); | |
200 |
1
1. updateCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
201 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
202 |
1
1. lambda$updateCourse$7 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$7 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
203 | "User is not a staff member for this course")); | |
204 | } | |
205 | ||
206 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setName → KILLED |
course.setName(name); |
207 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setSchool → KILLED |
course.setSchool(school); |
208 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setTerm → KILLED |
course.setTerm(term); |
209 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setStartDate → KILLED |
course.setStartDate(startDate); |
210 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setEndDate → KILLED |
course.setEndDate(endDate); |
211 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setGithubOrg → KILLED |
course.setGithubOrg(githubOrg); |
212 | ||
213 | course = courseRepository.save(course); | |
214 | log.info("course={}", course); | |
215 | ||
216 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::updateCourse → KILLED |
return course; |
217 | } | |
218 | ||
219 | // delete a course if the user is an admin or instructor for the course | |
220 | @Operation(summary = "Delete a course") | |
221 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
222 | @DeleteMapping("/delete") | |
223 | public Course deleteCourse( | |
224 | @Parameter(name = "id") @RequestParam Long id) | |
225 | throws JsonProcessingException { | |
226 | ||
227 | Course course = courseRepository.findById(id) | |
228 |
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())); |
229 | ||
230 | // Check if the current user is a staff member for this course or an admin. If | |
231 | // not, throw AccessDeniedException | |
232 | ||
233 | User u = getCurrentUser().getUser(); | |
234 |
1
1. deleteCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
235 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
236 |
1
1. lambda$deleteCourse$9 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$9 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
237 | "User is not a staff member for this course")); | |
238 | } | |
239 | ||
240 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/organic/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
241 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteCourse → KILLED |
return course; |
242 | } | |
243 | ||
244 | } | |
Mutations | ||
59 |
1.1 |
|
60 |
1.1 |
|
62 |
1.1 |
|
74 |
1.1 |
|
76 |
1.1 |
|
78 |
1.1 |
|
82 |
1.1 |
|
99 |
1.1 |
|
123 |
1.1 |
|
135 |
1.1 |
|
138 |
1.1 |
|
149 |
1.1 |
|
160 |
1.1 |
|
163 |
1.1 |
|
172 |
1.1 |
|
174 |
1.1 |
|
175 |
1.1 |
|
194 |
1.1 |
|
200 |
1.1 |
|
202 |
1.1 |
|
206 |
1.1 |
|
207 |
1.1 |
|
208 |
1.1 |
|
209 |
1.1 |
|
210 |
1.1 |
|
211 |
1.1 |
|
216 |
1.1 |
|
228 |
1.1 |
|
234 |
1.1 |
|
236 |
1.1 |
|
240 |
1.1 |
|
241 |
1.1 |