| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.Course; | |
| 5 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 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 jakarta.validation.Valid; | |
| 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 | @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 | * | |
| 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> allOrganisations() { | |
| 33 | Iterable<Course> courses = courseRepository.findAll(); | |
| 34 |
1
1. allOrganisations : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/CourseController::allOrganisations → KILLED |
return courses; |
| 35 | } | |
| 36 | ||
| 37 | @Operation(summary = "Get a single course") | |
| 38 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 39 | @GetMapping("") | |
| 40 | ||
| 41 | public Course getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 42 | Course course = | |
| 43 | courseRepository | |
| 44 | .findById(id) | |
| 45 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, id)); |
| 46 | ||
| 47 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::getById → KILLED |
return course; |
| 48 | } | |
| 49 | ||
| 50 | @Operation(summary = "Create a new course") | |
| 51 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 52 | @PostMapping("") | |
| 53 | ||
| 54 | public Course postCourse( | |
| 55 | @Parameter( | |
| 56 | name = "code", | |
| 57 | description = "e.g. CMPSC 156" | |
| 58 | ) | |
| 59 | @RequestParam String code, | |
| 60 | @Parameter( | |
| 61 | name = "name", | |
| 62 | description = "e.g. Advanced Applications Programming" | |
| 63 | ) | |
| 64 | @RequestParam String name, | |
| 65 | @Parameter( | |
| 66 | name = "term", | |
| 67 | description = "e.g. F25" | |
| 68 | ) | |
| 69 | @RequestParam String term | |
| 70 | ) | |
| 71 | ||
| 72 | throws JsonProcessingException { | |
| 73 | ||
| 74 | log.info("code={}, name={}, term={}", code, name, term); | |
| 75 | ||
| 76 | Course course = new Course(); | |
| 77 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(code); |
| 78 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(name); |
| 79 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(term); |
| 80 | ||
| 81 | Course savedCourse = courseRepository.save(course); | |
| 82 | ||
| 83 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::postCourse → KILLED |
return savedCourse; |
| 84 | } | |
| 85 | ||
| 86 | @Operation(summary = "Update a single course") | |
| 87 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 88 | @PutMapping("") | |
| 89 | public Course updateCourse(@Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Course incoming) { | |
| 90 | Course course = courseRepository | |
| 91 | .findById(id) | |
| 92 |
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)); |
| 93 | ||
| 94 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(incoming.getCode()); |
| 95 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(incoming.getName()); |
| 96 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(incoming.getTerm()); |
| 97 | ||
| 98 | courseRepository.save(course); | |
| 99 | ||
| 100 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::updateCourse → KILLED |
return course; |
| 101 | } | |
| 102 | ||
| 103 | @Operation(summary = "Delete a single course") | |
| 104 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 105 | @DeleteMapping("") | |
| 106 | public Object deleteCourse(@Parameter(name = "id") @RequestParam Long id) { | |
| 107 | Course course = | |
| 108 | courseRepository | |
| 109 | .findById(id) | |
| 110 |
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)); |
| 111 | ||
| 112 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/happiercows/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
| 113 |
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)); |
| 114 | } | |
| 115 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 45 |
1.1 |
|
| 47 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 83 |
1.1 |
|
| 92 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 100 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 |