| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.Moderator; | |
| 4 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 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 java.util.List; | |
| 11 | import java.util.Map; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.http.ResponseEntity; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 17 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 18 | import org.springframework.web.bind.annotation.GetMapping; | |
| 19 | import org.springframework.web.bind.annotation.PostMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 21 | import org.springframework.web.bind.annotation.RequestParam; | |
| 22 | import org.springframework.web.bind.annotation.RestController; | |
| 23 | ||
| 24 | /** This is a REST controller for Moderator */ | |
| 25 | @Tag(name = "Moderator") | |
| 26 | @RequestMapping("/api/admin/moderators") | |
| 27 | @RestController | |
| 28 | @Slf4j | |
| 29 | public class ModeratorController extends ApiController { | |
| 30 | @Autowired ModeratorRepository moderatorRepository; | |
| 31 | ||
| 32 | /** | |
| 33 | * Create a new moderator | |
| 34 | * | |
| 35 | * @param email the email in typical email format | |
| 36 | * @return the saved moderator | |
| 37 | */ | |
| 38 | @Operation(summary = "Create a new moderator") | |
| 39 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 40 | @PostMapping("/post") | |
| 41 | public Moderator postModerator(@Parameter(name = "email") @RequestParam String email) { | |
| 42 | String convertedEmail = CanonicalFormConverter.convertToValidEmail(email); | |
| 43 |
1
1. postModerator : negated conditional → KILLED |
if (moderatorRepository.findByEmail(convertedEmail).isPresent()) { |
| 44 | throw new IllegalArgumentException("%s is already a moderator".formatted(convertedEmail)); | |
| 45 | } | |
| 46 | Moderator moderator = new Moderator(convertedEmail); | |
| 47 | Moderator savedModerator = moderatorRepository.save(moderator); | |
| 48 |
1
1. postModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorController::postModerator → KILLED |
return savedModerator; |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * List all moderators | |
| 53 | * | |
| 54 | * @return an iterable of Moderator | |
| 55 | */ | |
| 56 | @Operation(summary = "List all moderators") | |
| 57 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 58 | @GetMapping("/all") | |
| 59 | public Iterable<Moderator> allModerators() { | |
| 60 |
1
1. allModerators : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ModeratorController::allModerators → KILLED |
return moderatorRepository.findAll(); |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Delete a Moderator | |
| 65 | * | |
| 66 | * @param email the email of the moderator to delete | |
| 67 | * @return a message indicating the moderator was deleted | |
| 68 | */ | |
| 69 | @Operation(summary = "Delete a Moderator") | |
| 70 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 | @DeleteMapping("/delete") | |
| 72 | public Object deleteModerator(@Parameter(name = "email") @RequestParam String email) { | |
| 73 | List<Moderator> moderators = moderatorRepository.findAllByEmail(email); | |
| 74 |
1
1. deleteModerator : negated conditional → KILLED |
if (moderators.isEmpty()) { |
| 75 | throw new EntityNotFoundException(Moderator.class, email); | |
| 76 | } | |
| 77 | moderatorRepository.deleteByEmail(email); | |
| 78 |
1
1. deleteModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorController::deleteModerator → KILLED |
return genericMessage("Moderator with id %s deleted".formatted(email)); |
| 79 | } | |
| 80 | ||
| 81 | @ExceptionHandler(IllegalArgumentException.class) | |
| 82 | public ResponseEntity<Map<String, String>> handleIllegalArgumentException( | |
| 83 | IllegalArgumentException ex) { | |
| 84 |
1
1. handleIllegalArgumentException : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorController::handleIllegalArgumentException → KILLED |
return ResponseEntity.badRequest().body(Map.of("message", ex.getMessage())); |
| 85 | } | |
| 86 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 48 |
1.1 |
|
| 60 |
1.1 |
|
| 74 |
1.1 |
|
| 78 |
1.1 |
|
| 84 |
1.1 |