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