| 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 io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.http.ResponseEntity; | |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 12 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 13 | import org.springframework.web.bind.annotation.GetMapping; | |
| 14 | import org.springframework.web.bind.annotation.PostMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestParam; | |
| 17 | import org.springframework.web.bind.annotation.RestController; | |
| 18 | ||
| 19 | /** | |
| 20 | * This is a REST controller for getting information about the moderators. These endpoints are only | |
| 21 | * accessible to users with the role "ROLE_ADMIN". | |
| 22 | */ | |
| 23 | @Tag(name = "Moderators") | |
| 24 | @RequestMapping("/api/admin/moderators") | |
| 25 | @RestController | |
| 26 | @Slf4j | |
| 27 | public class ModeratorsController extends ApiController { | |
| 28 | ||
| 29 | @Autowired private ModeratorRepository moderatorRepository; | |
| 30 | ||
| 31 | @Autowired private 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 postModerator(@RequestParam String email) { | |
| 43 | String convertedEmail = email.trim().toLowerCase(); | |
| 44 | ||
| 45 | Moderator moderator = Moderator.builder().email(convertedEmail).build(); | |
| 46 | moderatorRepository.save(moderator); | |
| 47 |
1
1. postModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorsController::postModerator → 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("/get") | |
| 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/ModeratorsController::allModerators → KILLED |
return moderators; |
| 61 | } | |
| 62 | ||
| 63 | /** Delete a 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(@RequestParam String email) { | |
| 68 | Moderator moderator = moderatorRepository.findById(email).orElse(null); | |
| 69 | ||
| 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/ModeratorsController::deleteModerator → KILLED |
return ResponseEntity.status(404) |
| 72 | .body(String.format("Moderator with email %s not found.", email)); | |
| 73 | } | |
| 74 | ||
| 75 |
1
1. deleteModerator : removed call to edu/ucsb/cs156/dining/repositories/ModeratorRepository::delete → KILLED |
moderatorRepository.delete(moderator); |
| 76 |
1
1. deleteModerator : replaced return value with null for edu/ucsb/cs156/dining/controllers/ModeratorsController::deleteModerator → KILLED |
return ResponseEntity.status(200) |
| 77 | .body(String.format("Moderator with email %s deleted.", email)); | |
| 78 | } | |
| 79 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 60 |
1.1 |
|
| 70 |
1.1 |
|
| 71 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |