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