| 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 moderators. These endpoints are only | |
| 22 | * accessible to users with the role "ROLE_ADMIN". | |
| 23 | */ | |
| 24 | @Tag(name = "Moderators") | |
| 25 | @RequestMapping("/api/admin/moderators") | |
| 26 | @RestController | |
| 27 | @Slf4j | |
| 28 | public class ModeratorsController extends ApiController { | |
| 29 | @Autowired ModeratorRepository moderatorRepository; | |
| 30 | @Autowired ObjectMapper mapper; | |
| 31 | ||
| 32 | /** | |
| 33 | * Create a new Moderator, available only to Admins. | |
| 34 | * | |
| 35 | * @param email the email of the moderator | |
| 36 | * @return the created Moderator | |
| 37 | */ | |
| 38 | @Operation(summary = "Create a new Moderator") | |
| 39 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 40 | @PostMapping("/post") | |
| 41 | public Moderator postModerator(@RequestParam String email) { | |
| 42 | String convertedEmail = CanonicalFormConverter.convertToValidEmail(email); | |
| 43 | Moderator moderator = Moderator.builder().email(convertedEmail).build(); | |
| 44 | moderatorRepository.save(moderator); | |
| 45 |
1
1. postModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorsController::postModerator → KILLED |
return moderator; |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Get a list of all moderators, available only to Admins. | |
| 50 | * | |
| 51 | * @return a list of all moderators | |
| 52 | */ | |
| 53 | @Operation(summary = "List all Moderators") | |
| 54 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 55 | @GetMapping("/get") | |
| 56 | public Iterable<Moderator> allModerators() { | |
| 57 | Iterable<Moderator> moderators = moderatorRepository.findAll(); | |
| 58 |
1
1. allModerators : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ModeratorsController::allModerators → KILLED |
return moderators; |
| 59 | } | |
| 60 | ||
| 61 | /** Delete a moderator by email, available only to Admins. */ | |
| 62 | @Operation(summary = "Delete a Moderator by email") | |
| 63 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 64 | @DeleteMapping("/delete") | |
| 65 | public ResponseEntity<String> deleteModerator(@RequestParam String email) { | |
| 66 | Moderator moderator = moderatorRepository.findById(email).orElse(null); | |
| 67 |
1
1. deleteModerator : negated conditional → KILLED |
if (moderator == null) { |
| 68 |
1
1. deleteModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorsController::deleteModerator → KILLED |
return ResponseEntity.status(404) |
| 69 | .body(String.format("Moderator with email %s not found.", email)); | |
| 70 | } | |
| 71 |
1
1. deleteModerator : removed call to edu/ucsb/cs156/dining/repositories/ModeratorRepository::delete → KILLED |
moderatorRepository.delete(moderator); |
| 72 |
1
1. deleteModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorsController::deleteModerator → KILLED |
return ResponseEntity.status(200) |
| 73 | .body(String.format("Moderator with email %s deleted.", email)); | |
| 74 | } | |
| 75 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 58 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |