| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.courses.entities.User; | |
| 6 | import edu.ucsb.cs156.courses.repositories.UserRepository; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.data.domain.Page; | |
| 11 | import org.springframework.data.domain.PageRequest; | |
| 12 | import org.springframework.http.ResponseEntity; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.GetMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestParam; | |
| 17 | import org.springframework.web.bind.annotation.RestController; | |
| 18 | ||
| 19 | @Tag(name = "User information (admin only)") | |
| 20 | @RequestMapping("/api/admin/users") | |
| 21 | @RestController | |
| 22 | public class UsersController extends ApiController { | |
| 23 | @Autowired UserRepository userRepository; | |
| 24 | ||
| 25 | @Autowired ObjectMapper mapper; | |
| 26 | ||
| 27 | @Operation(summary = "Get a list of all users") | |
| 28 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 29 | @GetMapping("") | |
| 30 | public ResponseEntity<String> users() throws JsonProcessingException { | |
| 31 | Iterable<User> users = userRepository.findAll(); | |
| 32 | String body = mapper.writeValueAsString(users); | |
| 33 |
1
1. users : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 34 | } | |
| 35 | ||
| 36 | @Operation(summary = "Get a paginated list of users") | |
| 37 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 38 | @GetMapping("/paged") | |
| 39 | public Page<User> getUsersPaginated( | |
| 40 | @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | |
| 41 | PageRequest pageRequest = PageRequest.of(page, size); | |
| 42 |
1
1. getUsersPaginated : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::getUsersPaginated → KILLED |
return userRepository.findAll(pageRequest); |
| 43 | } | |
| 44 | } | |
Mutations | ||
| 33 |
1.1 |
|
| 42 |
1.1 |