| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | |
| 4 | import org.springframework.http.ResponseEntity; | |
| 5 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 6 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 7 | import org.springframework.web.bind.annotation.GetMapping; | |
| 8 | import org.springframework.web.bind.annotation.PostMapping; | |
| 9 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 10 | import org.springframework.web.bind.annotation.RequestParam; | |
| 11 | import org.springframework.web.bind.annotation.RestController; | |
| 12 | ||
| 13 | import edu.ucsb.cs156.happiercows.entities.BannedWord; | |
| 14 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.BannedWordRepository; | |
| 16 | import io.swagger.v3.oas.annotations.Operation; | |
| 17 | import io.swagger.v3.oas.annotations.Parameter; | |
| 18 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 19 | ||
| 20 | @Tag(name = "Banned words for chat (admin only)") | |
| 21 | @RequestMapping("/api/admin/bannedwords") | |
| 22 | @RestController | |
| 23 | public class BannedWordsController extends ApiController { | |
| 24 | @Autowired | |
| 25 | BannedWordRepository bannedWordRepository; | |
| 26 | ||
| 27 | @Operation(summary = "Get all banned words") | |
| 28 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 29 | @GetMapping("/all") | |
| 30 | public Iterable<BannedWord> allBannedWords() { | |
| 31 |
1
1. allBannedWords : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::allBannedWords → KILLED |
return bannedWordRepository.findAll(); |
| 32 | } | |
| 33 | ||
| 34 | @Operation(summary = "Add a banned word or phrase (stored lowercase)") | |
| 35 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 36 | @PostMapping("/post") | |
| 37 | public Object addBannedWord(@Parameter(name = "word") @RequestParam String word) { | |
| 38 | // Collapse runs of whitespace so "darn it" and "darn it" count as the same phrase | |
| 39 | String normalized = word.trim().replaceAll("\\s+", " ").toLowerCase(); | |
| 40 |
1
1. addBannedWord : negated conditional → KILLED |
if (normalized.isEmpty()) { |
| 41 |
1
1. addBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::addBannedWord → KILLED |
return ResponseEntity.badRequest().body(genericMessage("Banned word cannot be blank")); |
| 42 | } | |
| 43 |
1
1. addBannedWord : negated conditional → KILLED |
if (bannedWordRepository.findByWord(normalized).isPresent()) { |
| 44 |
1
1. addBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::addBannedWord → KILLED |
return ResponseEntity.badRequest().body(genericMessage("'%s' is already banned".formatted(normalized))); |
| 45 | } | |
| 46 |
1
1. addBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::addBannedWord → KILLED |
return bannedWordRepository.save(BannedWord.builder().word(normalized).build()); |
| 47 | } | |
| 48 | ||
| 49 | @Operation(summary = "Delete a banned word by id") | |
| 50 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 51 | @DeleteMapping("") | |
| 52 | public Object deleteBannedWord(@Parameter(name = "id") @RequestParam long id) { | |
| 53 | BannedWord bannedWord = bannedWordRepository.findById(id) | |
| 54 |
1
1. lambda$deleteBannedWord$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::lambda$deleteBannedWord$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(BannedWord.class, id)); |
| 55 |
1
1. deleteBannedWord : removed call to edu/ucsb/cs156/happiercows/repositories/BannedWordRepository::delete → KILLED |
bannedWordRepository.delete(bannedWord); |
| 56 |
1
1. deleteBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::deleteBannedWord → KILLED |
return genericMessage("BannedWord with id %d deleted".formatted(id)); |
| 57 | } | |
| 58 | } | |
Mutations | ||
| 31 |
1.1 |
|
| 40 |
1.1 |
|
| 41 |
1.1 |
|
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 46 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |