| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Course; | |
| 4 | import edu.ucsb.cs156.happiercows.repositories.CourseRepository; | |
| 5 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import lombok.extern.slf4j.Slf4j; | |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 10 | import org.springframework.web.bind.annotation.*; | |
| 11 | ||
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | import jakarta.validation.Valid; | |
| 14 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 15 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 16 | ||
| 17 | ||
| 18 | @Slf4j | |
| 19 | @Tag(name = "Course") | |
| 20 | @RequestMapping("/api/course") | |
| 21 | @RestController | |
| 22 | public class CourseController extends ApiController { | |
| 23 | @Autowired | |
| 24 | private CourseRepository courseRepository; | |
| 25 | ||
| 26 | /** | |
| 27 | * This method returns a list of all courses. | |
| 28 | * | |
| 29 | * @return a list of all courses | |
| 30 | */ | |
| 31 | @Operation(summary = "List all courses") | |
| 32 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 33 | @GetMapping("/all") | |
| 34 | public Iterable<Course> allOrganisations() { | |
| 35 | Iterable<Course> courses = courseRepository.findAll(); | |
| 36 |
1
1. allOrganisations : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/CourseController::allOrganisations → KILLED |
return courses; |
| 37 | } | |
| 38 | /** | |
| 39 | * Create a new Course | |
| 40 | * | |
| 41 | * @param code the course code | |
| 42 | * @param name the course name | |
| 43 | * @param term the course term | |
| 44 | * @return the saved Course | |
| 45 | */ | |
| 46 | @Operation(summary = "Create a new course") | |
| 47 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 48 | @PostMapping("") | |
| 49 | public Course postCourse( | |
| 50 | @Parameter(name = "code") @RequestParam String code, | |
| 51 | @Parameter(name = "name") @RequestParam String name, | |
| 52 | @Parameter(name = "term") @RequestParam String term | |
| 53 | ) | |
| 54 | throws JsonProcessingException { | |
| 55 | ||
| 56 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 57 | // See: https://www.baeldung.com/spring-date-parameters | |
| 58 | ||
| 59 | Course course = new Course(); | |
| 60 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(code); |
| 61 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(name); |
| 62 |
1
1. postCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(term); |
| 63 | ||
| 64 | ||
| 65 | Course savedCourse = courseRepository.save(course); | |
| 66 | ||
| 67 |
1
1. postCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::postCourse → KILLED |
return savedCourse; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Get a single course by id | |
| 72 | * | |
| 73 | * @param id the id of the course | |
| 74 | * @return a course | |
| 75 | */ | |
| 76 | @Operation(summary = "Get a single course") | |
| 77 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 78 | @GetMapping("") | |
| 79 | public Course getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 80 | Course course = | |
| 81 | courseRepository | |
| 82 | .findById(id) | |
| 83 |
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)); |
| 84 | ||
| 85 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::getById → KILLED |
return course; |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Update a single course | |
| 90 | * | |
| 91 | * @param id id of the date to course | |
| 92 | * @param incoming the new course | |
| 93 | * @return the updated course object | |
| 94 | */ | |
| 95 | @Operation(summary = "Update a single course") | |
| 96 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 97 | @PutMapping("") | |
| 98 | public Course updateCourse( | |
| 99 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Course incoming) { | |
| 100 | ||
| 101 | Course course = | |
| 102 | courseRepository | |
| 103 | .findById(id) | |
| 104 |
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)); |
| 105 | ||
| 106 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setCode → KILLED |
course.setCode(incoming.getCode()); |
| 107 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setName → KILLED |
course.setName(incoming.getName()); |
| 108 |
1
1. updateCourse : removed call to edu/ucsb/cs156/happiercows/entities/Course::setTerm → KILLED |
course.setTerm(incoming.getTerm()); |
| 109 | ||
| 110 | courseRepository.save(course); | |
| 111 | ||
| 112 |
1
1. updateCourse : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CourseController::updateCourse → KILLED |
return course; |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Delete a Course | |
| 117 | * | |
| 118 | * @param id the id of the course to delete | |
| 119 | * @return a message indicating the course was deleted | |
| 120 | */ | |
| 121 | @Operation(summary = "Delete a course") | |
| 122 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 123 | @DeleteMapping("") | |
| 124 | public Object deleteCourse(@Parameter(name = "id") @RequestParam Long id) { | |
| 125 | Course course = | |
| 126 | courseRepository | |
| 127 | .findById(id) | |
| 128 |
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)); |
| 129 | ||
| 130 |
1
1. deleteCourse : removed call to edu/ucsb/cs156/happiercows/repositories/CourseRepository::delete → KILLED |
courseRepository.delete(course); |
| 131 |
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)); |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | ||
Mutations | ||
| 36 |
1.1 |
|
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 67 |
1.1 |
|
| 83 |
1.1 |
|
| 85 |
1.1 |
|
| 104 |
1.1 |
|
| 106 |
1.1 |
|
| 107 |
1.1 |
|
| 108 |
1.1 |
|
| 112 |
1.1 |
|
| 128 |
1.1 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |