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