1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
3 | ||
4 | import com.fasterxml.jackson.core.JsonProcessingException; | |
5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
6 | ||
7 | import org.springframework.beans.factory.annotation.Autowired; | |
8 | import org.springframework.http.ResponseEntity; | |
9 | import org.springframework.security.access.prepost.PreAuthorize; | |
10 | import org.springframework.web.bind.annotation.GetMapping; | |
11 | import org.springframework.web.bind.annotation.RequestMapping; | |
12 | import org.springframework.web.bind.annotation.RestController; | |
13 | import org.springframework.web.bind.annotation.PostMapping; | |
14 | import org.springframework.web.bind.annotation.RequestParam; | |
15 | ||
16 | import edu.ucsb.cs156.happiercows.entities.User; | |
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 user using id") | |
43 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
44 | @PostMapping("/suspend") | |
45 | public Object suspendUser(@Parameter(name = "id") @RequestParam Long id){ | |
46 |
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(id).orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
47 | | |
48 |
1
1. suspendUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(true); |
49 | userRepository.save(user); | |
50 |
1
1. suspendUser : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::suspendUser → KILLED |
return genericMessage("User with id %s has been suspended".formatted(id)); |
51 | } | |
52 | | |
53 | @Operation(summary = "Restore user using id") | |
54 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
55 | @PostMapping("/restore") | |
56 | public Object restoreUser(@Parameter(name = "id") @RequestParam Long id){ | |
57 |
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(id).orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
58 | | |
59 |
1
1. restoreUser : removed call to edu/ucsb/cs156/happiercows/entities/User::setSuspended → KILLED |
user.setSuspended(false); |
60 | userRepository.save(user); | |
61 |
1
1. restoreUser : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UsersController::restoreUser → KILLED |
return genericMessage("User with id %s has been restored".formatted(id)); |
62 | } | |
63 | } | |
64 | ||
65 | ||
66 | ||
Mutations | ||
39 |
1.1 |
|
46 |
1.1 |
|
48 |
1.1 |
|
50 |
1.1 |
|
57 |
1.1 |
|
59 |
1.1 |
|
61 |
1.1 |