| 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 | @Tag(name = "User information (admin only)") | |
| 23 | @RequestMapping("/api/admin/users") | |
| 24 | @RestController | |
| 25 | public class UsersController extends ApiController { | |
| 26 |     @Autowired | |
| 27 |     UserRepository userRepository; | |
| 28 | ||
| 29 |     @Autowired | |
| 30 |     ObjectMapper mapper; | |
| 31 | ||
| 32 |     @Operation(summary = "Get a list of all users") | |
| 33 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 34 |     @GetMapping("") | |
| 35 |     public ResponseEntity<String> users() | |
| 36 |             throws JsonProcessingException { | |
| 37 |         Iterable<User> users = userRepository.findAll(); | |
| 38 |         String body = mapper.writeValueAsString(users); | |
| 39 | 
1
1. users : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::users → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 40 |     } | |
| 41 | ||
| 42 |     @Operation(summary = "Suspend a user by id") | |
| 43 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 44 |     @PostMapping("/suspend") | |
| 45 |     public Object suspendUser(@Parameter(name = "userId") @RequestParam long userId) throws JsonProcessingException { | |
| 46 | ||
| 47 | 
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)); | 
| 48 | ||
| 49 | 
1
1. suspendUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED | 
        user.setSuspended(true); | 
| 50 |         userRepository.save(user); | |
| 51 | 
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)); | 
| 52 |     } | |
| 53 | ||
| 54 |     @Operation(summary = "Restore a user by id") | |
| 55 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 56 |     @PostMapping("/restore") | |
| 57 |     public Object restoreUser(@Parameter(name = "userId") @RequestParam long userId) throws JsonProcessingException { | |
| 58 | 
1
1. lambda$restoreUser$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::lambda$restoreUser$1 → KILLED | 
        User user = userRepository.findById(userId).orElseThrow(() -> new EntityNotFoundException(User.class, userId)); | 
| 59 | ||
| 60 | 
1
1. restoreUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED | 
        user.setSuspended(false); | 
| 61 |         userRepository.save(user); | |
| 62 | 
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)); | 
| 63 |     } | |
| 64 | } | |
Mutations | ||
| 39 | 
 
 1.1  | 
|
| 47 | 
 
 1.1  | 
|
| 49 | 
 
 1.1  | 
|
| 51 | 
 
 1.1  | 
|
| 58 | 
 
 1.1  | 
|
| 60 | 
 
 1.1  | 
|
| 62 | 
 
 1.1  |