| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | import org.springframework.http.ResponseEntity; | |
| 8 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | import org.springframework.web.bind.annotation.GetMapping; | |
| 10 | import org.springframework.web.bind.annotation.PostMapping; | |
| 11 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 12 | import org.springframework.web.bind.annotation.RequestParam; | |
| 13 | import org.springframework.web.bind.annotation.RestController; | |
| 14 | ||
| 15 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 16 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 17 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 18 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 19 | import io.swagger.v3.oas.annotations.Operation; | |
| 20 | import io.swagger.v3.oas.annotations.Parameter; | |
| 21 | ||
| 22 | import java.time.Instant; | |
| 23 | ||
| 24 | @Tag(name="User information (admin only)") | |
| 25 | @RequestMapping("/api/admin/users") | |
| 26 | @RestController | |
| 27 | public class UsersController extends ApiController { | |
| 28 | @Autowired | |
| 29 | UserRepository userRepository; | |
| 30 | ||
| 31 | @Autowired | |
| 32 | ObjectMapper mapper; | |
| 33 | ||
| 34 | @Operation(summary = "Get a list of all users") | |
| 35 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 36 | @GetMapping("") | |
| 37 | public ResponseEntity<String> users() | |
| 38 | throws JsonProcessingException { | |
| 39 | Iterable<User> users = userRepository.findAll(); | |
| 40 | String body = mapper.writeValueAsString(users); | |
| 41 |
1
1. users : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 42 | } | |
| 43 | | |
| 44 | @Operation(summary = "Suspend a user by id") | |
| 45 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 46 | @PostMapping("/suspend") | |
| 47 | public Object suspendUser(@Parameter(name="userId") @RequestParam long userId ) throws JsonProcessingException { | |
| 48 | ||
| 49 |
1
1. lambda$suspendUser$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$suspendUser$0 → KILLED |
User user = userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException(User.class, userId)); |
| 50 | ||
| 51 |
1
1. suspendUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(true); |
| 52 | userRepository.save(user); | |
| 53 |
1
1. suspendUser : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::suspendUser → KILLED |
return genericMessage("User with id %d suspended".formatted(userId)); |
| 54 | } | |
| 55 | ||
| 56 | @Operation(summary="Restore a user by id") | |
| 57 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 58 | @PostMapping("/restore") | |
| 59 | public Object restoreUser(@Parameter(name="userId") @RequestParam long userId ) throws JsonProcessingException { | |
| 60 |
1
1. lambda$restoreUser$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$restoreUser$0 → KILLED |
User user = userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException(User.class, userId)); |
| 61 | ||
| 62 |
1
1. restoreUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(false); |
| 63 | userRepository.save(user); | |
| 64 |
1
1. restoreUser : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::restoreUser → KILLED |
return genericMessage("User with id %d restored".formatted(userId)); |
| 65 | } | |
| 66 | ||
| 67 | @Operation(summary="Put a user on chat cooldown for a number of seconds (0 clears it)") | |
| 68 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 69 | @PostMapping("/chatCooldown") | |
| 70 | public Object chatCooldown(@Parameter(name="userId") @RequestParam long userId, | |
| 71 | @Parameter(name="seconds") @RequestParam long seconds) { | |
| 72 |
2
1. chatCooldown : changed conditional boundary → KILLED 2. chatCooldown : negated conditional → KILLED |
if (seconds < 0) { |
| 73 |
1
1. chatCooldown : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::chatCooldown → KILLED |
return ResponseEntity.badRequest().body(genericMessage("seconds must be >= 0")); |
| 74 | } | |
| 75 |
1
1. lambda$chatCooldown$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$chatCooldown$0 → KILLED |
User user = userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException(User.class, userId)); |
| 76 | ||
| 77 |
1
1. chatCooldown : removed call to edu/ucsb/cs156/happiercows/entities/User::setChatCooldownUntil → KILLED |
user.setChatCooldownUntil(Instant.now().plusSeconds(seconds)); |
| 78 | userRepository.save(user); | |
| 79 |
1
1. chatCooldown : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::chatCooldown → KILLED |
return genericMessage("User with id %d on chat cooldown for %d seconds".formatted(userId, seconds)); |
| 80 | } | |
| 81 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 49 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 60 |
1.1 |
|
| 62 |
1.1 |
|
| 64 |
1.1 |
|
| 72 |
1.1 2.2 |
|
| 73 |
1.1 |
|
| 75 |
1.1 |
|
| 77 |
1.1 |
|
| 79 |
1.1 |