| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import java.time.Duration; | |
| 4 | import java.time.Instant; | |
| 5 | ||
| 6 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 7 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 8 | ||
| 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.GetMapping; | |
| 13 | import org.springframework.web.bind.annotation.PostMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestParam; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | ||
| 18 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 19 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 20 | import edu.ucsb.cs156.happiercows.errors.InvalidCooldownDurationException; | |
| 21 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 22 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 23 | import io.swagger.v3.oas.annotations.Operation; | |
| 24 | import io.swagger.v3.oas.annotations.Parameter; | |
| 25 | ||
| 26 | @Tag(name="User information (admin only)") | |
| 27 | @RequestMapping("/api/admin/users") | |
| 28 | @RestController | |
| 29 | public class UsersController extends ApiController { | |
| 30 | @Autowired | |
| 31 | UserRepository userRepository; | |
| 32 | ||
| 33 | @Autowired | |
| 34 | ObjectMapper mapper; | |
| 35 | ||
| 36 | @Operation(summary = "Get a list of all users") | |
| 37 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 38 | @GetMapping("") | |
| 39 | public ResponseEntity<String> users() | |
| 40 | throws JsonProcessingException { | |
| 41 | Iterable<User> users = userRepository.findAll(); | |
| 42 | String body = mapper.writeValueAsString(users); | |
| 43 |
1
1. users : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 44 | } | |
| 45 | ||
| 46 | @Operation(summary = "Suspend a user by id") | |
| 47 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 48 | @PostMapping("/suspend") | |
| 49 | public Object suspendUser(@Parameter(name="userId") @RequestParam long userId ) throws JsonProcessingException { | |
| 50 | ||
| 51 |
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)); |
| 52 | ||
| 53 |
1
1. suspendUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(true); |
| 54 | userRepository.save(user); | |
| 55 |
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)); |
| 56 | } | |
| 57 | ||
| 58 | @Operation(summary="Restore a user by id") | |
| 59 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 60 | @PostMapping("/restore") | |
| 61 | public Object restoreUser(@Parameter(name="userId") @RequestParam long userId ) throws JsonProcessingException { | |
| 62 |
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)); |
| 63 | ||
| 64 |
1
1. restoreUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(false); |
| 65 | userRepository.save(user); | |
| 66 |
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)); |
| 67 | } | |
| 68 | ||
| 69 | @Operation(summary="Place a user on chat cooldown") | |
| 70 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 | @PostMapping("/cooldown") | |
| 72 | public Object cooldownUser( | |
| 73 | @Parameter(name="userId") @RequestParam long userId, | |
| 74 | @Parameter(name="durationMinutes") @RequestParam long durationMinutes | |
| 75 | ) throws InvalidCooldownDurationException { | |
| 76 | ||
| 77 |
2
1. cooldownUser : negated conditional → KILLED 2. cooldownUser : changed conditional boundary → KILLED |
if (durationMinutes <= 0) { |
| 78 | throw new InvalidCooldownDurationException( | |
| 79 | "Cooldown duration must be greater than zero minutes" | |
| 80 | ); | |
| 81 | } | |
| 82 | ||
| 83 | User user = userRepository.findById(userId) | |
| 84 |
1
1. lambda$cooldownUser$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$cooldownUser$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, userId)); |
| 85 | ||
| 86 | Instant cooldownUntil = Instant.now().plus(Duration.ofMinutes(durationMinutes)); | |
| 87 |
1
1. cooldownUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setCooldownUntil → KILLED |
user.setCooldownUntil(cooldownUntil); |
| 88 | userRepository.save(user); | |
| 89 | ||
| 90 |
1
1. cooldownUser : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::cooldownUser → KILLED |
return genericMessage( |
| 91 | "User with id %d placed on cooldown for %d minutes" | |
| 92 | .formatted(userId, durationMinutes) | |
| 93 | ); | |
| 94 | } | |
| 95 | ||
| 96 | @Operation(summary="Clear a user's chat cooldown") | |
| 97 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 98 | @PostMapping("/cooldown/clear") | |
| 99 | public Object clearCooldown( | |
| 100 | @Parameter(name="userId") @RequestParam long userId | |
| 101 | ) { | |
| 102 | ||
| 103 | User user = userRepository.findById(userId) | |
| 104 |
1
1. lambda$clearCooldown$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$clearCooldown$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, userId)); |
| 105 | ||
| 106 |
1
1. clearCooldown : removed call to edu/ucsb/cs156/happiercows/entities/User::setCooldownUntil → KILLED |
user.setCooldownUntil(null); |
| 107 | userRepository.save(user); | |
| 108 | ||
| 109 |
1
1. clearCooldown : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::clearCooldown → KILLED |
return genericMessage("Cooldown cleared for user with id %d".formatted(userId)); |
| 110 | } | |
| 111 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 55 |
1.1 |
|
| 62 |
1.1 |
|
| 64 |
1.1 |
|
| 66 |
1.1 |
|
| 77 |
1.1 2.2 |
|
| 84 |
1.1 |
|
| 87 |
1.1 |
|
| 90 |
1.1 |
|
| 104 |
1.1 |
|
| 106 |
1.1 |
|
| 109 |
1.1 |