| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.BannedWord; | |
| 4 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.happiercows.repositories.BannedWordRepository; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 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 | import org.springframework.dao.DataIntegrityViolationException; | |
| 21 | import edu.ucsb.cs156.happiercows.services.ChatFilterService; | |
| 22 | ||
| 23 | /** Admin-only API for maintaining the list of words censored in chat. */ | |
| 24 | @Slf4j | |
| 25 | @Tag(name = "Banned Words") | |
| 26 | @RequestMapping("/api/admin/bannedwords") | |
| 27 | @RestController | |
| 28 | public class BannedWordsController extends ApiController { | |
| 29 | ||
| 30 | @Autowired | |
| 31 | private BannedWordRepository bannedWordRepository; | |
| 32 | ||
| 33 | @Operation(summary = "List all banned words") | |
| 34 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 35 | @GetMapping("/all") | |
| 36 | public Iterable<BannedWord> allBannedWords() { | |
| 37 |
1
1. allBannedWords : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::allBannedWords → KILLED |
return bannedWordRepository.findAll(); |
| 38 | } | |
| 39 | ||
| 40 | @Operation(summary = "Add a banned word (stored trimmed and lowercase)") | |
| 41 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 42 | @PostMapping("/post") | |
| 43 | public ResponseEntity<Object> postBannedWord( | |
| 44 | @Parameter(name = "word") @RequestParam String word) { | |
| 45 | String normalized = ChatFilterService.normalize(word); | |
| 46 | ||
| 47 |
1
1. postBannedWord : negated conditional → KILLED |
if (normalized.isEmpty()) { |
| 48 |
1
1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED |
return ResponseEntity.badRequest().body(genericMessage("Word must not be blank")); |
| 49 | } | |
| 50 |
1
1. postBannedWord : negated conditional → KILLED |
if (bannedWordRepository.findByWord(normalized).isPresent()) { |
| 51 |
1
1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED |
return ResponseEntity.badRequest() |
| 52 | .body(genericMessage("Word '%s' is already banned".formatted(normalized))); | |
| 53 | } | |
| 54 | ||
| 55 | try { | |
| 56 | BannedWord saved = bannedWordRepository.save(BannedWord.builder().word(normalized).build()); | |
| 57 |
1
1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED |
return ResponseEntity.ok(saved); |
| 58 | } catch (DataIntegrityViolationException e) { | |
| 59 | // Another request inserted the same word between findByWord() and save() | |
| 60 |
1
1. postBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::postBannedWord → KILLED |
return ResponseEntity.badRequest() |
| 61 | .body(genericMessage("Word '%s' is already banned".formatted(normalized))); | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | @Operation(summary = "Remove a banned word") | |
| 66 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 67 | @DeleteMapping("") | |
| 68 | public Object deleteBannedWord( | |
| 69 | @Parameter(name = "id") @RequestParam Long id) { | |
| 70 | BannedWord bannedWord = bannedWordRepository.findById(id) | |
| 71 |
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)); |
| 72 | ||
| 73 |
1
1. deleteBannedWord : removed call to edu/ucsb/cs156/happiercows/repositories/BannedWordRepository::delete → KILLED |
bannedWordRepository.delete(bannedWord); |
| 74 |
1
1. deleteBannedWord : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/BannedWordsController::deleteBannedWord → KILLED |
return genericMessage("BannedWord with id %s deleted".formatted(id)); |
| 75 | } | |
| 76 | } | |
Mutations | ||
| 37 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 57 |
1.1 |
|
| 60 |
1.1 |
|
| 71 |
1.1 |
|
| 73 |
1.1 |
|
| 74 |
1.1 |