| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Course; | |
| 4 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.happiercows.models.CourseDTO; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CourseRepository; | |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | import jakarta.validation.Valid; | |
| 9 | import io.swagger.v3.oas.annotations.Operation; | |
| 10 | import io.swagger.v3.oas.annotations.Parameter; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.*; | |
| 15 | ||
| 16 | @Slf4j | |
| 17 | @Tag(name = "Course") | |
| 18 | @RequestMapping("/api/course") | |
| 19 | @RestController | |
| 20 | public class CourseController extends ApiController { | |
| 21 | @Autowired | |
| 22 | private CourseRepository courseRepository; | |
| 23 | ||
| 24 | /** | |
| 25 | * This method returns a list of all courses. | |
| 26 | * | |
| 27 | * @return a list of all courses | |
| 28 | */ | |
| 29 | @Operation(summary = "List all courses") | |
| 30 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 31 | @GetMapping("/all") | |
| 32 | public Iterable<Course> allCourses() { | |
| 33 | Iterable<Course> courses = courseRepository.findAll(); | |
| 34 |
1
1. allCourses : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/CourseController::allCourses → KILLED |
return courses; |
| 35 | } | |
| 36 | ||
| 37 | @Operation(summary = "Create a course") | |
| 38 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 39 | @PostMapping("") | |
| 40 | public Course postCourse( | |
| 41 | @Parameter(name = "code") @RequestParam String code, | |
| 42 | @Parameter(name = "name") @RequestParam String name, | |
| 43 | @Parameter(name = "term") @RequestParam String term) { | |
| 44 | Course course = new Course(); | |
| 45 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(code); |
| 46 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(name); |
| 47 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(term); |
| 48 | Course savedCourse = courseRepository.save(course); | |
| 49 | ||
| 50 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::postCourse → KILLED |
return savedCourse; |
| 51 | } | |
| 52 | ||
| 53 | @Operation(summary = "Get a single course by id") | |
| 54 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 55 | @GetMapping("/{id}") | |
| 56 | public Course getCourseById( | |
| 57 | @Parameter(name = "id") @PathVariable Long id) { | |
| 58 | Course course = courseRepository.findById(id) | |
| 59 |
1
1. lambda$getCourseById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$getCourseById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 60 |
1
1. getCourseById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::getCourseById → KILLED |
return course; |
| 61 | } | |
| 62 | ||
| 63 | @Operation(summary = "Update a single course") | |
| 64 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 65 | @PutMapping("/{id}") | |
| 66 | public Course updateCourse( | |
| 67 | @Parameter(name = "id") @PathVariable Long id, | |
| 68 | @RequestBody @Valid Course incoming) { | |
| 69 | ||
| 70 | Course course = | |
| 71 | courseRepository | |
| 72 | .findById(id) | |
| 73 |
1
1. lambda$updateCourse$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$updateCourse$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 74 | ||
| 75 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(incoming.getCode()); |
| 76 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(incoming.getName()); |
| 77 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(incoming.getTerm()); |
| 78 | ||
| 79 | courseRepository.save(course); | |
| 80 | ||
| 81 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::updateCourse → KILLED |
return course; |
| 82 | } | |
| 83 | ||
| 84 | @Operation(summary = "Delete a course") | |
| 85 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 86 | @DeleteMapping("/{id}") | |
| 87 | public Object deleteCourse(@Parameter(name = "id") @PathVariable Long id) { | |
| 88 | Course course = | |
| 89 | courseRepository | |
| 90 | .findById(id) | |
| 91 |
1
1. lambda$deleteCourse$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$deleteCourse$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 92 | ||
| 93 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/happiercows/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
| 94 |
1
1. deleteCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::deleteCourse → KILLED |
return genericMessage("Course with id %s deleted".formatted(id)); |
| 95 | } | |
| 96 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 59 |
1.1 |
|
| 60 |
1.1 |
|
| 73 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 81 |
1.1 |
|
| 91 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |