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 jakarta.transaction.Transactional; | |
34 | import javax.validation.Valid; | |
35 | ||
36 | import java.util.Optional; | |
37 | ||
38 | @Tag(name = "Courses") | |
39 | @RequestMapping("/api/courses") | |
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("/all") | |
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("hasAnyRole('ROLE_ADMIN','ROLE_INSTRUCTOR')") | |
87 | @PostMapping("/post") | |
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 | Course course = Course.builder() | |
98 | .name(name) | |
99 | .school(school) | |
100 | .term(term) | |
101 | .startDate(startDate) | |
102 | .endDate(endDate) | |
103 | .githubOrg(githubOrg) | |
104 | .build(); | |
105 | ||
106 | Course savedCourse = courseRepository.save(course); | |
107 | User u = getCurrentUser().getUser(); | |
108 | ||
109 | Staff courseStaff = Staff.builder() | |
110 | .courseId(savedCourse.getId()) | |
111 | .githubId(u.getGithubId()) | |
112 | .build(); | |
113 | ||
114 | log.info("courseStaff={}", courseStaff); | |
115 | courseStaffRepository.save(courseStaff); | |
116 | ||
117 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::postCourse → KILLED |
return savedCourse; |
118 | } | |
119 | ||
120 | @Operation(summary = "Add a staff member to a course") | |
121 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
122 | @PostMapping("/addStaff") | |
123 | public Staff addStaff( | |
124 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
125 | @Parameter(name = "githubLogin") @RequestParam String githubLogin) | |
126 | throws JsonProcessingException { | |
127 | ||
128 | Course course = courseRepository.findById(courseId) | |
129 |
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())); |
130 | ||
131 | User user = userRepository.findByGithubLogin(githubLogin) | |
132 |
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())); |
133 | ||
134 | Staff courseStaff = Staff.builder() | |
135 | .courseId(course.getId()) | |
136 | .githubId(user.getGithubId()) | |
137 | .user(user) | |
138 | .build(); | |
139 | ||
140 | courseStaff = courseStaffRepository.save(courseStaff); | |
141 | log.info("courseStaff={}", courseStaff); | |
142 | ||
143 |
1
1. addStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::addStaff → KILLED |
return courseStaff; |
144 | } | |
145 | ||
146 | @Operation(summary = "Get Staff for course") | |
147 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
148 | @GetMapping("/getStaff") | |
149 | public Iterable<Staff> getStaff( | |
150 | @Parameter(name = "courseId") @RequestParam Long courseId) | |
151 | throws JsonProcessingException { | |
152 | ||
153 | Course course = courseRepository.findById(courseId) | |
154 |
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())); |
155 | ||
156 | Iterable<Staff> courseStaff = courseStaffRepository.findByCourseId(course.getId()); | |
157 |
1
1. getStaff : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::getStaff → KILLED |
return courseStaff; |
158 | } | |
159 | ||
160 | @Operation(summary = "Delete a Course Staff by id") | |
161 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
162 | @DeleteMapping("/staff") | |
163 | public Object deleteStaff( | |
164 | @Parameter(name = "id") @RequestParam Long id) { | |
165 | Staff staff = courseStaffRepository.findById(id) | |
166 |
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())); |
167 | ||
168 |
1
1. deleteStaff : removed call to edu/ucsb/cs156/organic/repositories/StaffRepository::delete → KILLED |
courseStaffRepository.delete(staff); |
169 |
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)); |
170 | } | |
171 | ||
172 | @Operation(summary = "Update information for a course") | |
173 | // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member | |
174 | // for the course | |
175 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
176 | @PutMapping("/update") | |
177 | public Course updateCourse( | |
178 | @Parameter(name = "id") @RequestParam Long id, | |
179 | @Parameter(name = "name", description = "course name, e.g. CMPSC 156") @RequestParam String name, | |
180 | @Parameter(name = "school", description = "school abbreviation e.g. UCSB") @RequestParam String school, | |
181 | @Parameter(name = "term", description = "quarter or semester, e.g. F23") @RequestParam String term, | |
182 | @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, | |
183 | @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, | |
184 | @Parameter(name = "githubOrg", description = "for example ucsb-cs156-f23") @RequestParam String githubOrg) | |
185 | throws JsonProcessingException { | |
186 | ||
187 | Course course = courseRepository.findById(id) | |
188 |
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())); |
189 | ||
190 | // Check if the current user is a staff member for this course or an admin. If | |
191 | // not, throw AccessDeniedException | |
192 | ||
193 | User u = getCurrentUser().getUser(); | |
194 |
1
1. updateCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
195 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
196 |
1
1. lambda$updateCourse$7 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$updateCourse$7 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
197 | "User is not a staff member for this course")); | |
198 | } | |
199 | ||
200 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setName → KILLED |
course.setName(name); |
201 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setSchool → KILLED |
course.setSchool(school); |
202 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setTerm → KILLED |
course.setTerm(term); |
203 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setStartDate → KILLED |
course.setStartDate(startDate); |
204 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setEndDate → KILLED |
course.setEndDate(endDate); |
205 |
1
1. updateCourse : removed call to edu/ucsb/cs156/organic/entities/Course::setGithubOrg → KILLED |
course.setGithubOrg(githubOrg); |
206 | ||
207 | course = courseRepository.save(course); | |
208 | log.info("course={}", course); | |
209 | ||
210 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::updateCourse → KILLED |
return course; |
211 | } | |
212 | ||
213 | // delete a course if the user is an admin or instructor for the course | |
214 | @Operation(summary = "Delete a course") | |
215 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
216 | @DeleteMapping("/delete") | |
217 | public Course deleteCourse( | |
218 | @Parameter(name = "id") @RequestParam Long id) | |
219 | throws JsonProcessingException { | |
220 | ||
221 | Course course = courseRepository.findById(id) | |
222 |
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())); |
223 | ||
224 | // Check if the current user is a staff member for this course or an admin. If | |
225 | // not, throw AccessDeniedException | |
226 | ||
227 | User u = getCurrentUser().getUser(); | |
228 |
1
1. deleteCourse : negated conditional → KILLED |
if (!u.isAdmin()) { |
229 | courseStaffRepository.findByCourseIdAndGithubId(course.getId(), u.getGithubId()) | |
230 |
1
1. lambda$deleteCourse$9 : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::lambda$deleteCourse$9 → KILLED |
.orElseThrow(() -> new AccessDeniedException( |
231 | "User is not a staff member for this course")); | |
232 | } | |
233 | ||
234 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/organic/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
235 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/organic/controllers/CoursesController::deleteCourse → KILLED |
return course; |
236 | } | |
237 | ||
238 | } | |
Mutations | ||
59 |
1.1 |
|
60 |
1.1 |
|
62 |
1.1 |
|
74 |
1.1 |
|
76 |
1.1 |
|
78 |
1.1 |
|
82 |
1.1 |
|
117 |
1.1 |
|
129 |
1.1 |
|
132 |
1.1 |
|
143 |
1.1 |
|
154 |
1.1 |
|
157 |
1.1 |
|
166 |
1.1 |
|
168 |
1.1 |
|
169 |
1.1 |
|
188 |
1.1 |
|
194 |
1.1 |
|
196 |
1.1 |
|
200 |
1.1 |
|
201 |
1.1 |
|
202 |
1.1 |
|
203 |
1.1 |
|
204 |
1.1 |
|
205 |
1.1 |
|
210 |
1.1 |
|
222 |
1.1 |
|
228 |
1.1 |
|
230 |
1.1 |
|
234 |
1.1 |
|
235 |
1.1 |