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 | ||
12 | import org.springframework.beans.factory.annotation.Autowired; | |
13 | import org.springframework.http.ResponseEntity; | |
14 | import org.springframework.security.access.prepost.PreAuthorize; | |
15 | import org.springframework.web.bind.annotation.DeleteMapping; | |
16 | import org.springframework.web.bind.annotation.GetMapping; | |
17 | import org.springframework.web.bind.annotation.PostMapping; | |
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 | import io.swagger.v3.oas.annotations.tags.Tag; | |
23 | import io.swagger.v3.oas.annotations.Operation; | |
24 | import io.swagger.v3.oas.annotations.Parameter; | |
25 | ||
26 | ||
27 | @Tag(name = "User information (admin only)") | |
28 | @RequestMapping("/api/admin/users") | |
29 | @RestController | |
30 | public class UsersController extends ApiController { | |
31 | @Autowired | |
32 | UserRepository userRepository; | |
33 | ||
34 | @Autowired | |
35 | ObjectMapper mapper; | |
36 | ||
37 | @Operation(summary = "Get a list of all users") | |
38 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
39 | @GetMapping("") | |
40 | public ResponseEntity<String> users() | |
41 | throws JsonProcessingException { | |
42 | Iterable<User> users = userRepository.findAll(); | |
43 | String body = mapper.writeValueAsString(users); | |
44 |
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
45 | } | |
46 | ||
47 | @Operation(summary = "Get user by id") | |
48 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
49 | @GetMapping("/get") | |
50 | public User users( | |
51 | @Parameter(name = "id", description = "Long, id number of user to get", example = "1", required = true) @RequestParam Long id) | |
52 | throws JsonProcessingException { | |
53 | User user = userRepository.findById(id) | |
54 |
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)); |
55 |
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED |
return user; |
56 | } | |
57 | ||
58 | @Operation(summary = "Delete a user (admin)") | |
59 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
60 | @DeleteMapping("/delete") | |
61 | public Object deleteUser_Admin( | |
62 | @Parameter(name = "id", description = "Long, id number of user to delete", example = "1", required = true) @RequestParam Long id) { | |
63 | User user = userRepository.findById(id) | |
64 |
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)); |
65 | ||
66 |
1
1. deleteUser_Admin : removed call to edu/ucsb/cs156/gauchoride/repositories/UserRepository::delete → KILLED |
userRepository.delete(user); |
67 | ||
68 |
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)); |
69 | } | |
70 | ||
71 | | |
72 | @Operation(summary = "Toggle the admin field") | |
73 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
74 | @PostMapping("/toggleAdmin") | |
75 | public Object toggleAdmin( @Parameter(name = "id", description = "Long, id number of user to toggle their admin field", example = "1", required = true) @RequestParam Long id){ | |
76 | User user = userRepository.findById(id) | |
77 |
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)); |
78 | ||
79 |
2
1. toggleAdmin : negated conditional → KILLED 2. toggleAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED |
user.setAdmin(!user.getAdmin()); |
80 | userRepository.save(user); | |
81 |
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 to %s".formatted(id, user.getAdmin())); |
82 | } | |
83 | ||
84 | ||
85 | @Operation(summary = "Toggle the driver field") | |
86 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
87 | @PostMapping("/toggleDriver") | |
88 | public Object toggleDriver( @Parameter(name = "id", description = "Long, id number of user to toggle their driver field", example = "1", required = true) @RequestParam Long id){ | |
89 | ||
90 | User user = userRepository.findById(id) | |
91 |
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)); |
92 | ||
93 |
2
1. toggleDriver : negated conditional → KILLED 2. toggleDriver : removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED |
user.setDriver(!user.getDriver()); |
94 | userRepository.save(user); | |
95 |
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 to %s".formatted(id, user.getDriver())); |
96 | } | |
97 | ||
98 | @Operation(summary = "Toggle the rider field") | |
99 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
100 | @PostMapping("/toggleRider") | |
101 | public Object toggleRider( @Parameter(name = "id") @RequestParam Long id){ | |
102 | User user = userRepository.findById(id) | |
103 |
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)); |
104 | ||
105 |
2
1. toggleRider : negated conditional → KILLED 2. toggleRider : removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED |
user.setRider(!user.getRider()); |
106 | userRepository.save(user); | |
107 |
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 to %s".formatted(id, user.getRider())); |
108 | } | |
109 | } | |
Mutations | ||
44 |
1.1 |
|
54 |
1.1 |
|
55 |
1.1 |
|
64 |
1.1 |
|
66 |
1.1 |
|
68 |
1.1 |
|
77 |
1.1 |
|
79 |
1.1 2.2 |
|
81 |
1.1 |
|
91 |
1.1 |
|
93 |
1.1 2.2 |
|
95 |
1.1 |
|
103 |
1.1 |
|
105 |
1.1 2.2 |
|
107 |
1.1 |