| 1 | package edu.ucsb.cs156.rec.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | import org.springframework.http.ResponseEntity; | |
| 8 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 10 | import org.springframework.web.bind.annotation.GetMapping; | |
| 11 | import org.springframework.web.bind.annotation.PostMapping; | |
| 12 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 13 | import org.springframework.web.bind.annotation.RequestParam; | |
| 14 | import org.springframework.web.bind.annotation.RestController; | |
| 15 | ||
| 16 | import edu.ucsb.cs156.rec.errors.EntityNotFoundException; | |
| 17 | ||
| 18 | import edu.ucsb.cs156.rec.entities.User; | |
| 19 | import edu.ucsb.cs156.rec.repositories.UserRepository; | |
| 20 | import io.swagger.v3.oas.annotations.Operation; | |
| 21 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 22 | import io.swagger.v3.oas.annotations.Parameter; | |
| 23 | ||
| 24 | /** | |
| 25 | * This is a REST controller for getting information about the users. | |
| 26 | * | |
| 27 | * These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
| 28 | */ | |
| 29 | ||
| 30 | @Tag(name="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 | /** | |
| 41 | * This method returns a list of all users. Accessible only to users with the role "ROLE_ADMIN". | |
| 42 | * @return a list of all users | |
| 43 | * @throws JsonProcessingException if there is an error processing the JSON | |
| 44 | */ | |
| 45 | @Operation(summary= "Get a list of all users") | |
| 46 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 47 | @GetMapping("") | |
| 48 | public ResponseEntity<String> users() | |
| 49 | throws JsonProcessingException { | |
| 50 | Iterable<User> users = userRepository.findAll(); | |
| 51 | String body = mapper.writeValueAsString(users); | |
| 52 |
1
1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 53 | } | |
| 54 | ||
| 55 | @Operation(summary = "Get user by id") | |
| 56 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 57 | @GetMapping("/get") | |
| 58 | public User users( | |
| 59 | @Parameter(name = "id", description = "Long, id number of user to get", example = "1", required = true) @RequestParam Long id) | |
| 60 | throws JsonProcessingException { | |
| 61 | User user = userRepository.findById(id) | |
| 62 |
1
1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$users$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 63 |
1
1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED |
return user; |
| 64 | } | |
| 65 | ||
| 66 | @Operation(summary = "Delete a user (admin)") | |
| 67 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 68 | @DeleteMapping("/delete") | |
| 69 | public Object deleteUser_Admin( | |
| 70 | @Parameter(name = "id", description = "Long, id number of user to delete", example = "1", required = true) @RequestParam Long id) { | |
| 71 | User user = userRepository.findById(id) | |
| 72 |
1
1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 73 | ||
| 74 |
1
1. deleteUser_Admin : removed call to edu/ucsb/cs156/rec/repositories/UserRepository::delete → KILLED |
userRepository.delete(user); |
| 75 | ||
| 76 |
1
1. deleteUser_Admin : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::deleteUser_Admin → KILLED |
return genericMessage("User with id %s deleted".formatted(id)); |
| 77 | } | |
| 78 | ||
| 79 | | |
| 80 | @Operation(summary = "Toggle the admin field") | |
| 81 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 82 | @PostMapping("/toggleAdmin") | |
| 83 | public Object toggleAdmin( @Parameter(name = "id", description = "Long, id number of user to toggle their admin field", example = "1", required = true) @RequestParam Long id){ | |
| 84 | User user = userRepository.findById(id) | |
| 85 |
1
1. lambda$toggleAdmin$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleAdmin$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 86 | ||
| 87 |
2
1. toggleAdmin : negated conditional → KILLED 2. toggleAdmin : removed call to edu/ucsb/cs156/rec/entities/User::setAdmin → KILLED |
user.setAdmin(!user.getAdmin()); |
| 88 | userRepository.save(user); | |
| 89 |
1
1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleAdmin → KILLED |
return genericMessage("User with id %s has toggled admin status to %s".formatted(id, user.getAdmin())); |
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | @Operation(summary = "Toggle the professor field") | |
| 94 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 95 | @PostMapping("/toggleProfessor") | |
| 96 | public Object toggleProfessor( @Parameter(name = "id", description = "Long, id number of user to toggle their professor field", example = "1", required = true) @RequestParam Long id){ | |
| 97 | ||
| 98 | User user = userRepository.findById(id) | |
| 99 |
1
1. lambda$toggleProfessor$3 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleProfessor$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 100 | ||
| 101 |
2
1. toggleProfessor : negated conditional → KILLED 2. toggleProfessor : removed call to edu/ucsb/cs156/rec/entities/User::setProfessor → KILLED |
user.setProfessor(!user.getProfessor()); |
| 102 | userRepository.save(user); | |
| 103 |
1
1. toggleProfessor : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleProfessor → KILLED |
return genericMessage("User with id %s has toggled professor status to %s".formatted(id, user.getProfessor())); |
| 104 | } | |
| 105 | ||
| 106 | @Operation(summary = "Toggle the student field") | |
| 107 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 108 | @PostMapping("/toggleStudent") | |
| 109 | public Object toggleStudent( @Parameter(name = "id") @RequestParam Long id){ | |
| 110 | User user = userRepository.findById(id) | |
| 111 |
1
1. lambda$toggleStudent$4 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$toggleStudent$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 112 | ||
| 113 |
2
1. toggleStudent : negated conditional → KILLED 2. toggleStudent : removed call to edu/ucsb/cs156/rec/entities/User::setStudent → KILLED |
user.setStudent(!user.getStudent()); |
| 114 | userRepository.save(user); | |
| 115 |
1
1. toggleStudent : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::toggleStudent → KILLED |
return genericMessage("User with id %s has toggled student status to %s".formatted(id, user.getStudent())); |
| 116 | } | |
| 117 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 72 |
1.1 |
|
| 74 |
1.1 |
|
| 76 |
1.1 |
|
| 85 |
1.1 |
|
| 87 |
1.1 2.2 |
|
| 89 |
1.1 |
|
| 99 |
1.1 |
|
| 101 |
1.1 2.2 |
|
| 103 |
1.1 |
|
| 111 |
1.1 |
|
| 113 |
1.1 2.2 |
|
| 115 |
1.1 |