| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | ||
| 5 | import edu.ucsb.cs156.frontiers.entities.Instructor; | |
| 6 | import edu.ucsb.cs156.frontiers.repositories.InstructorRepository; | |
| 7 | ||
| 8 | import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | import org.springframework.http.ResponseEntity; | |
| 10 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 11 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 12 | import org.springframework.web.bind.annotation.GetMapping; | |
| 13 | import org.springframework.web.bind.annotation.PostMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestParam; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | ||
| 18 | import io.swagger.v3.oas.annotations.Operation; | |
| 19 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 20 | import lombok.extern.slf4j.Slf4j; | |
| 21 | ||
| 22 | /** | |
| 23 | * This is a REST controller for getting information about the instructors. | |
| 24 | * These endpoints are only accessible to instructors with the role | |
| 25 | * "ROLE_ADMIN". | |
| 26 | */ | |
| 27 | ||
| 28 | @Tag(name = "Instructors") | |
| 29 | @RequestMapping("/api/admin/instructors") | |
| 30 | @RestController | |
| 31 | @Slf4j | |
| 32 | public class InstructorsController extends ApiController { | |
| 33 | @Autowired | |
| 34 | InstructorRepository instructorRepository; | |
| 35 | ||
| 36 | @Autowired | |
| 37 | ObjectMapper mapper; | |
| 38 | ||
| 39 | /** | |
| 40 | * Create a new Instructor, available only to Admins. | |
| 41 | * | |
| 42 | * @param email the email of the instructor | |
| 43 | * @return the created Instructor | |
| 44 | */ | |
| 45 | @Operation(summary = "Create a new Instructor") | |
| 46 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 47 | @PostMapping("/post") | |
| 48 | public Instructor postInstructor( | |
| 49 | @RequestParam String email) { | |
| 50 | Instructor instructor = Instructor.builder() | |
| 51 | .email(email) | |
| 52 | .build(); | |
| 53 | instructorRepository.save(instructor); | |
| 54 |
1
1. postInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::postInstructor → KILLED |
return instructor; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Get a list of all instructors, available only to Admins. | |
| 59 | * | |
| 60 | * @return a list of all instructors | |
| 61 | */ | |
| 62 | @Operation(summary = "List all Instructors") | |
| 63 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 64 | @GetMapping("/get") | |
| 65 | public Iterable<Instructor> allInstructors() { | |
| 66 | Iterable<Instructor> instructors = instructorRepository.findAll(); | |
| 67 |
1
1. allInstructors : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/InstructorsController::allInstructors → KILLED |
return instructors; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Delete an instructor by email, available only to Admins. | |
| 72 | */ | |
| 73 | @Operation(summary = "Delete an Instructor by email") | |
| 74 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 75 | @DeleteMapping("/delete") | |
| 76 | public ResponseEntity<String> deleteInstructor( | |
| 77 | @RequestParam String email) { | |
| 78 | Instructor instructor = instructorRepository.findById(email).orElse(null); | |
| 79 | ||
| 80 |
1
1. deleteInstructor : negated conditional → KILLED |
if (instructor == null) { |
| 81 |
1
1. deleteInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED |
return ResponseEntity.status(404) |
| 82 | .body(String.format("Instructor with email %s not found.", email)); | |
| 83 | } | |
| 84 | ||
| 85 |
1
1. deleteInstructor : removed call to edu/ucsb/cs156/frontiers/repositories/InstructorRepository::delete → KILLED |
instructorRepository.delete(instructor); |
| 86 |
1
1. deleteInstructor : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/InstructorsController::deleteInstructor → KILLED |
return ResponseEntity.status(200).body(String.format("Instructor with email %s deleted.", email)); |
| 87 | } | |
| 88 | } | |
Mutations | ||
| 54 |
1.1 |
|
| 67 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |