1 | package edu.ucsb.cs156.gauchoride.controllers; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | ||
6 | import edu.ucsb.cs156.gauchoride.entities.User; | |
7 | import edu.ucsb.cs156.gauchoride.repositories.UserRepository; | |
8 | ||
9 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
10 | ||
11 | import java.util.ArrayList; | |
12 | import java.util.List; | |
13 | ||
14 | import org.springframework.beans.factory.annotation.Autowired; | |
15 | import org.springframework.http.ResponseEntity; | |
16 | import org.springframework.security.access.prepost.PreAuthorize; | |
17 | import org.springframework.web.bind.annotation.DeleteMapping; | |
18 | import org.springframework.web.bind.annotation.GetMapping; | |
19 | import org.springframework.web.bind.annotation.PostMapping; | |
20 | import org.springframework.web.bind.annotation.PathVariable; | |
21 | import org.springframework.web.bind.annotation.RequestMapping; | |
22 | import org.springframework.web.bind.annotation.RequestParam; | |
23 | import org.springframework.web.bind.annotation.RestController; | |
24 | ||
25 | import io.swagger.annotations.Api; | |
26 | import io.swagger.annotations.ApiOperation; | |
27 | import io.swagger.annotations.ApiParam; | |
28 | ||
29 | ||
30 | @Api(description = "User information (admin only)") | |
31 | @RequestMapping("/api/admin/users") | |
32 | @RestController | |
33 | public class UsersController extends ApiController { | |
34 | @Autowired | |
35 | UserRepository userRepository; | |
36 | ||
37 | @Autowired | |
38 | ObjectMapper mapper; | |
39 | ||
40 | @ApiOperation(value = "Get a list of all users") | |
41 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
42 | @GetMapping("") | |
43 | public ResponseEntity<String> users() | |
44 | throws JsonProcessingException { | |
45 | Iterable<User> users = userRepository.findAll(); | |
46 | String body = mapper.writeValueAsString(users); | |
47 |
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
48 | } | |
49 | ||
50 | @ApiOperation(value = "Get a list of all riders") | |
51 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
52 | @GetMapping("/riders") | |
53 | public ResponseEntity<String> riders() | |
54 | throws JsonProcessingException { | |
55 | Iterable<User> users = userRepository.findAll(); | |
56 | List<User> riders = new ArrayList<>(); | |
57 | for(User user : users) { | |
58 |
1
1. riders : negated conditional → KILLED |
if (user.getRider()) { |
59 | riders.add(user); | |
60 | } | |
61 | } | |
62 | String body = mapper.writeValueAsString((Iterable<User>)riders); | |
63 |
1
1. riders : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::riders → KILLED |
return ResponseEntity.ok().body(body); |
64 | } | |
65 | ||
66 | @ApiOperation(value = "Get a list of all drivers") | |
67 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
68 | @GetMapping("/drivers") | |
69 | public ResponseEntity<String> drivers() | |
70 | throws JsonProcessingException { | |
71 | Iterable<User> users = userRepository.findAll(); | |
72 | List<User> drivers = new ArrayList<>(); | |
73 | for(User user : users) { | |
74 |
1
1. drivers : negated conditional → KILLED |
if (user.getDriver()) { |
75 | drivers.add(user); | |
76 | } | |
77 | } | |
78 | String body = mapper.writeValueAsString((Iterable<User>)drivers); | |
79 |
1
1. drivers : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::drivers → KILLED |
return ResponseEntity.ok().body(body); |
80 | } | |
81 | ||
82 | @ApiOperation(value = "Get user by id") | |
83 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
84 | @GetMapping("/get") | |
85 | public User users( | |
86 | @ApiParam("id") @RequestParam Long id) | |
87 | throws JsonProcessingException { | |
88 | User user = userRepository.findById(id) | |
89 |
1
1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$users$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
90 |
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED |
return user; |
91 | } | |
92 | ||
93 | @ApiOperation(value = "Delete a user (admin)") | |
94 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
95 | @DeleteMapping("/delete") | |
96 | public Object deleteUser_Admin( | |
97 | @ApiParam("id") @RequestParam Long id) { | |
98 | User user = userRepository.findById(id) | |
99 |
1
1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
100 | ||
101 |
1
1. deleteUser_Admin : removed call to edu/ucsb/cs156/gauchoride/repositories/UserRepository::delete → KILLED |
userRepository.delete(user); |
102 | ||
103 |
1
1. deleteUser_Admin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::deleteUser_Admin → KILLED |
return genericMessage("User with id %s deleted".formatted(id)); |
104 | } | |
105 | ||
106 | | |
107 | @ApiOperation(value = "Toggle the admin field") | |
108 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
109 | @PostMapping("/toggleAdmin") | |
110 | public Object toggleAdmin( @ApiParam("id") @RequestParam Long id){ | |
111 | User user = userRepository.findById(id) | |
112 |
1
1. lambda$toggleAdmin$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleAdmin$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
113 | ||
114 |
2
1. toggleAdmin : negated conditional → KILLED 2. toggleAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED |
user.setAdmin(!user.getAdmin()); |
115 | userRepository.save(user); | |
116 |
1
1. toggleAdmin : negated conditional → KILLED |
if (user.getAdmin()) { |
117 |
1
1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleAdmin → KILLED |
return genericMessage("User with id %s has toggled admin status from false to true".formatted(id)); |
118 | } | |
119 | else { | |
120 |
1
1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleAdmin → KILLED |
return genericMessage("User with id %s has toggled admin status from true to false".formatted(id)); |
121 | } | |
122 | } | |
123 | ||
124 | @ApiOperation(value = "Toggle the driver field") | |
125 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
126 | @PostMapping("/toggleDriver") | |
127 | public Object toggleDriver( @ApiParam("id") @RequestParam Long id){ | |
128 | User user = userRepository.findById(id) | |
129 |
1
1. lambda$toggleDriver$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleDriver$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
130 | ||
131 |
2
1. toggleDriver : negated conditional → KILLED 2. toggleDriver : removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED |
user.setDriver(!user.getDriver()); |
132 | userRepository.save(user); | |
133 | | |
134 |
1
1. toggleDriver : negated conditional → KILLED |
if (user.getDriver()) { |
135 |
1
1. toggleDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleDriver → KILLED |
return genericMessage("User with id %s has toggled driver status from false to true".formatted(id)); |
136 | } | |
137 | else { | |
138 |
1
1. toggleDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleDriver → KILLED |
return genericMessage("User with id %s has toggled driver status from true to false".formatted(id)); |
139 | } | |
140 | } | |
141 | ||
142 | @ApiOperation(value = "Toggle the rider field") | |
143 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
144 | @PostMapping("/toggleRider") | |
145 | public Object toggleRider( @ApiParam("id") @RequestParam Long id){ | |
146 | User user = userRepository.findById(id) | |
147 |
1
1. lambda$toggleRider$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleRider$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
148 | ||
149 |
2
1. toggleRider : negated conditional → KILLED 2. toggleRider : removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED |
user.setRider(!user.getRider()); |
150 | userRepository.save(user); | |
151 |
1
1. toggleRider : negated conditional → KILLED |
if (user.getRider()) { |
152 |
1
1. toggleRider : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleRider → KILLED |
return genericMessage("User with id %s has toggled rider status from false to true".formatted(id)); |
153 | } | |
154 | else { | |
155 |
1
1. toggleRider : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleRider → KILLED |
return genericMessage("User with id %s has toggled rider status from true to false".formatted(id)); |
156 | } | |
157 | } | |
158 | ||
159 | @ApiOperation(value = "Change the pronouns field") | |
160 | @PreAuthorize("hasRole('ROLE_USER')") | |
161 | @PostMapping("/changePronouns") | |
162 | public Object changePronouns( @ApiParam("pronouns") @RequestParam String pronouns){ | |
163 | User user = super.getCurrentUser().getUser(); | |
164 | ||
165 |
1
1. changePronouns : removed call to edu/ucsb/cs156/gauchoride/entities/User::setPronouns → KILLED |
user.setPronouns(pronouns); |
166 | userRepository.save(user); | |
167 |
1
1. changePronouns : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::changePronouns → KILLED |
return genericMessage("User has changed pronouns to %s".formatted(pronouns)); |
168 | } | |
169 | | |
170 | @ApiOperation(value = "Toggle the wheelchair field") | |
171 | @PreAuthorize("hasRole('ROLE_USER')") | |
172 | @PostMapping("/toggleWheelchair") | |
173 | public Object toggleWheelchair(){ | |
174 | User user = super.getCurrentUser().getUser(); | |
175 | ||
176 |
2
1. toggleWheelchair : negated conditional → KILLED 2. toggleWheelchair : removed call to edu/ucsb/cs156/gauchoride/entities/User::setWheelchair → KILLED |
user.setWheelchair(!user.getWheelchair()); |
177 | userRepository.save(user); | |
178 |
1
1. toggleWheelchair : negated conditional → KILLED |
if (user.getWheelchair()) { |
179 |
1
1. toggleWheelchair : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleWheelchair → KILLED |
return genericMessage("User has toggled wheelchair status from false to true"); |
180 | } | |
181 | else { | |
182 |
1
1. toggleWheelchair : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleWheelchair → KILLED |
return genericMessage("User has toggled wheelchair status from true to false"); |
183 | } | |
184 | } | |
185 | ||
186 | } | |
Mutations | ||
47 |
1.1 |
|
58 |
1.1 |
|
63 |
1.1 |
|
74 |
1.1 |
|
79 |
1.1 |
|
89 |
1.1 |
|
90 |
1.1 |
|
99 |
1.1 |
|
101 |
1.1 |
|
103 |
1.1 |
|
112 |
1.1 |
|
114 |
1.1 2.2 |
|
116 |
1.1 |
|
117 |
1.1 |
|
120 |
1.1 |
|
129 |
1.1 |
|
131 |
1.1 2.2 |
|
134 |
1.1 |
|
135 |
1.1 |
|
138 |
1.1 |
|
147 |
1.1 |
|
149 |
1.1 2.2 |
|
151 |
1.1 |
|
152 |
1.1 |
|
155 |
1.1 |
|
165 |
1.1 |
|
167 |
1.1 |
|
176 |
1.1 2.2 |
|
178 |
1.1 |
|
179 |
1.1 |
|
182 |
1.1 |