| 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.Parameter; | |
| 9 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 10 | import java.util.Arrays; | |
| 11 | import java.util.List; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.data.domain.PageRequest; | |
| 14 | import org.springframework.data.domain.Sort.Direction; | |
| 15 | import org.springframework.http.ResponseEntity; | |
| 16 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 17 | import org.springframework.web.bind.annotation.GetMapping; | |
| 18 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 19 | import org.springframework.web.bind.annotation.RequestParam; | |
| 20 | import org.springframework.web.bind.annotation.RestController; | |
| 21 | ||
| 22 | @Tag(name = "User information (admin only)") | |
| 23 | @RequestMapping("/api/admin/users") | |
| 24 | @RestController | |
| 25 | public class UsersController extends ApiController { | |
| 26 | @Autowired UserRepository userRepository; | |
| 27 | ||
| 28 | @Autowired ObjectMapper mapper; | |
| 29 | ||
| 30 | @Operation(summary = "Get a list of all users") | |
| 31 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 32 | @GetMapping("") | |
| 33 | public ResponseEntity<String> users() throws JsonProcessingException { | |
| 34 | Iterable<User> users = userRepository.findAll(); | |
| 35 | String body = mapper.writeValueAsString(users); | |
| 36 |
1
1. users : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 37 | } | |
| 38 | ||
| 39 | @Operation(summary = "Search users by email, first name, last name or full name, with pages") | |
| 40 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 41 | @GetMapping("/q") | |
| 42 | public Iterable<User> usersByNameAndEmail( | |
| 43 | @Parameter( | |
| 44 | name = "searchString", | |
| 45 | description = "text to look for in email and names, case-insensitive", | |
| 46 | example = "gaucho", | |
| 47 | required = true) | |
| 48 | @RequestParam | |
| 49 | String searchString, | |
| 50 | @Parameter( | |
| 51 | name = "page", | |
| 52 | description = "what page of the data", | |
| 53 | example = "0", | |
| 54 | required = true) | |
| 55 | @RequestParam | |
| 56 | int page, | |
| 57 | @Parameter( | |
| 58 | name = "pageSize", | |
| 59 | description = "size of each page", | |
| 60 | example = "5", | |
| 61 | required = true) | |
| 62 | @RequestParam | |
| 63 | int pageSize, | |
| 64 | @Parameter( | |
| 65 | name = "sortField", | |
| 66 | description = "field to sort by", | |
| 67 | example = "email", | |
| 68 | required = true) | |
| 69 | @RequestParam | |
| 70 | String sortField, | |
| 71 | @Parameter( | |
| 72 | name = "sortDirection", | |
| 73 | description = "sort direction", | |
| 74 | example = "ASC", | |
| 75 | required = true) | |
| 76 | @RequestParam | |
| 77 | String sortDirection) { | |
| 78 | PageRequest pageRequest = buildPageRequest(page, pageSize, sortField, sortDirection); | |
| 79 |
1
1. usersByNameAndEmail : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UsersController::usersByNameAndEmail → KILLED |
return userRepository |
| 80 | .findByEmailContainingIgnoreCaseOrGivenNameContainingIgnoreCaseOrFamilyNameContainingIgnoreCaseOrFullNameContainingIgnoreCase( | |
| 81 | searchString, searchString, searchString, searchString, pageRequest); | |
| 82 | } | |
| 83 | ||
| 84 | @Operation(summary = "Get a list of all users with pages") | |
| 85 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 86 | @GetMapping(params = {"page", "pageSize", "sortField", "sortDirection"}) | |
| 87 | public Iterable<User> usersPaged( | |
| 88 | @Parameter( | |
| 89 | name = "page", | |
| 90 | description = "what page of the data", | |
| 91 | example = "0", | |
| 92 | required = true) | |
| 93 | @RequestParam | |
| 94 | int page, | |
| 95 | @Parameter( | |
| 96 | name = "pageSize", | |
| 97 | description = "size of each page", | |
| 98 | example = "5", | |
| 99 | required = true) | |
| 100 | @RequestParam | |
| 101 | int pageSize, | |
| 102 | @Parameter( | |
| 103 | name = "sortField", | |
| 104 | description = "field to sort by", | |
| 105 | example = "email", | |
| 106 | required = true) | |
| 107 | @RequestParam | |
| 108 | String sortField, | |
| 109 | @Parameter( | |
| 110 | name = "sortDirection", | |
| 111 | description = "sort direction", | |
| 112 | example = "ASC", | |
| 113 | required = true) | |
| 114 | @RequestParam | |
| 115 | String sortDirection) | |
| 116 | throws JsonProcessingException { | |
| 117 | PageRequest pageRequest = buildPageRequest(page, pageSize, sortField, sortDirection); | |
| 118 | ||
| 119 | Iterable<User> users = userRepository.findAll(pageRequest); | |
| 120 |
1
1. usersPaged : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UsersController::usersPaged → KILLED |
return users; |
| 121 | } | |
| 122 | ||
| 123 | private PageRequest buildPageRequest( | |
| 124 | int page, int pageSize, String sortField, String sortDirection) { | |
| 125 | List<String> allowedSortFields = Arrays.asList("givenName", "familyName", "email"); | |
| 126 |
1
1. buildPageRequest : negated conditional → KILLED |
if (!allowedSortFields.contains(sortField)) { |
| 127 | throw new IllegalArgumentException( | |
| 128 | String.format( | |
| 129 | "%s is not a valid sort field. Valid values are %s", sortField, allowedSortFields)); | |
| 130 | } | |
| 131 | ||
| 132 | List<String> allowedSortDirections = Arrays.asList("ASC", "DESC"); | |
| 133 |
1
1. buildPageRequest : negated conditional → KILLED |
if (!allowedSortDirections.contains(sortDirection)) { |
| 134 | throw new IllegalArgumentException( | |
| 135 | String.format( | |
| 136 | "%s is not a valid sort direction. Valid values are %s", | |
| 137 | sortDirection, allowedSortDirections)); | |
| 138 | } | |
| 139 | ||
| 140 | Direction sortDirectionObject = Direction.ASC; | |
| 141 |
1
1. buildPageRequest : negated conditional → KILLED |
if (sortDirection.equals("DESC")) { |
| 142 | sortDirectionObject = Direction.DESC; | |
| 143 | } | |
| 144 | ||
| 145 |
1
1. buildPageRequest : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::buildPageRequest → KILLED |
return PageRequest.of(page, pageSize, sortDirectionObject, sortField); |
| 146 | } | |
| 147 | } | |
Mutations | ||
| 36 |
1.1 |
|
| 79 |
1.1 |
|
| 120 |
1.1 |
|
| 126 |
1.1 |
|
| 133 |
1.1 |
|
| 141 |
1.1 |
|
| 145 |
1.1 |