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