| 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 java.util.ArrayList; | |
| 7 | import java.util.HashMap; | |
| 8 | import java.util.List; | |
| 9 | import java.util.Map; | |
| 10 | ||
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.http.ResponseEntity; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.GetMapping; | |
| 15 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 16 | import org.springframework.web.bind.annotation.PutMapping; | |
| 17 | import org.springframework.web.bind.annotation.PostMapping; | |
| 18 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 19 | import org.springframework.web.bind.annotation.RestController; | |
| 20 | ||
| 21 | import edu.ucsb.cs156.rec.entities.RecommendationRequest; | |
| 22 | import edu.ucsb.cs156.rec.entities.User; | |
| 23 | import edu.ucsb.cs156.rec.repositories.UserRepository; | |
| 24 | import io.swagger.v3.core.util.Json; | |
| 25 | import io.swagger.v3.oas.annotations.Operation; | |
| 26 | import io.swagger.v3.oas.annotations.Parameter; | |
| 27 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 28 | ||
| 29 | import org.springframework.web.bind.annotation.RequestParam; | |
| 30 | ||
| 31 | import edu.ucsb.cs156.rec.errors.EntityNotFoundException; | |
| 32 | ||
| 33 | /** | |
| 34 | * This is a REST controller for getting information about the users. | |
| 35 | * | |
| 36 | * These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
| 37 | */ | |
| 38 | ||
| 39 | @Tag(name="User information (admin only except for /professors)") | |
| 40 | @RequestMapping("/api/admin/users") | |
| 41 | @RestController | |
| 42 | public class UsersController extends ApiController { | |
| 43 | @Autowired | |
| 44 | UserRepository userRepository; | |
| 45 | ||
| 46 | @Autowired | |
| 47 | ObjectMapper mapper; | |
| 48 | ||
| 49 | /** | |
| 50 | * This method returns a list of all users. Accessible only to users with the role "ROLE_ADMIN". | |
| 51 | * @return a list of all users | |
| 52 | * @throws JsonProcessingException if there is an error processing the JSON | |
| 53 | */ | |
| 54 | @Operation(summary= "Get a list of all users") | |
| 55 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 56 | @GetMapping("") | |
| 57 | public ResponseEntity<String> users() | |
| 58 | throws JsonProcessingException { | |
| 59 | Iterable<User> users = userRepository.findAll(); | |
| 60 | String body = mapper.writeValueAsString(users); | |
| 61 |
1
1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * This method returns a list of all Recommendation Requests requested by current student. | |
| 66 | * @return a list of all Recommendation Requests requested by the current user | |
| 67 | */ | |
| 68 | @Operation(summary = "List all professors") | |
| 69 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 70 | @GetMapping("/professors") | |
| 71 | public Iterable<Map<String, Object>> allProfessors( | |
| 72 | ) { | |
| 73 | Iterable<User> professors = userRepository.professorIsTrue(); | |
| 74 | // to add privacy, only return professor_id and professor_name | |
| 75 | List<Map<String, Object>> limited_list = new ArrayList<>(); | |
| 76 | for (User professor : professors) { | |
| 77 | Map<String, Object> map = new HashMap<>(); | |
| 78 | map.put("id", professor.getId()); | |
| 79 | map.put("fullName", professor.getFullName()); | |
| 80 | limited_list.add(map); | |
| 81 | } | |
| 82 |
1
1. allProfessors : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/UsersController::allProfessors → KILLED |
return limited_list; |
| 83 | } | |
| 84 | ||
| 85 | @Operation(summary= "Get user by id") | |
| 86 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 87 | @GetMapping("/get") | |
| 88 | public User users( | |
| 89 | @Parameter(name="id", description="Long, id number of user to get", example="1", required=true) @RequestParam Long id) | |
| 90 | throws JsonProcessingException { | |
| 91 |
1
1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$users$0 → KILLED |
User user = userRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 92 | | |
| 93 |
1
1. users : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::users → KILLED |
return user; |
| 94 | } | |
| 95 | ||
| 96 | @Operation(summary= "Delete a user (admin)") | |
| 97 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 98 | @DeleteMapping("/delete") | |
| 99 | public Object deleteUser_Admin( | |
| 100 | @Parameter(name="id", description="Long, id number of user to delete", example="1", required=true) @RequestParam Long id) { | |
| 101 |
1
1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED |
User user = userRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 102 |
1
1. deleteUser_Admin : removed call to edu/ucsb/cs156/rec/repositories/UserRepository::delete → KILLED |
userRepository.delete(user); |
| 103 |
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 has been deleted.".formatted(id)); |
| 104 | } | |
| 105 | ||
| 106 | @Operation(summary = "Toggle the admin field") | |
| 107 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 108 | @PostMapping("/toggleAdmin") | |
| 109 | public Object toggleAdmin( @Parameter(name = "id", description = "Long, id number of user to toggle their admin field", example = "1", required = true) @RequestParam Long id){ | |
| 110 | User user = userRepository.findById(id) | |
| 111 |
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)); |
| 112 | ||
| 113 |
2
1. toggleAdmin : removed call to edu/ucsb/cs156/rec/entities/User::setAdmin → KILLED 2. toggleAdmin : negated conditional → KILLED |
user.setAdmin(!user.getAdmin()); |
| 114 | userRepository.save(user); | |
| 115 |
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())); |
| 116 | } | |
| 117 | ||
| 118 | @Operation(summary = "Toggle the professor field") | |
| 119 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 120 | @PostMapping("/toggleProfessor") | |
| 121 | public Object toggleProfessor( @Parameter(name = "id", description = "Long, id number of user to toggle their professor field", example = "1", required = true) @RequestParam Long id){ | |
| 122 | User user = userRepository.findById(id) | |
| 123 |
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)); |
| 124 | ||
| 125 |
2
1. toggleProfessor : negated conditional → KILLED 2. toggleProfessor : removed call to edu/ucsb/cs156/rec/entities/User::setProfessor → KILLED |
user.setProfessor(!user.getProfessor()); |
| 126 | userRepository.save(user); | |
| 127 |
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())); |
| 128 | } | |
| 129 | ||
| 130 | @Operation(summary = "Toggle the student field") | |
| 131 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 132 | @PostMapping("/toggleStudent") | |
| 133 | public Object toggleStudent( @Parameter(name = "id", description = "Long, id number of user to toggle their student field", example = "1", required = true) @RequestParam Long id){ | |
| 134 | User user = userRepository.findById(id) | |
| 135 |
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)); |
| 136 | ||
| 137 |
2
1. toggleStudent : negated conditional → KILLED 2. toggleStudent : removed call to edu/ucsb/cs156/rec/entities/User::setStudent → KILLED |
user.setStudent(!user.getStudent()); |
| 138 | userRepository.save(user); | |
| 139 |
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())); |
| 140 | } | |
| 141 | } | |
Mutations | ||
| 61 |
1.1 |
|
| 82 |
1.1 |
|
| 91 |
1.1 |
|
| 93 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 103 |
1.1 |
|
| 111 |
1.1 |
|
| 113 |
1.1 2.2 |
|
| 115 |
1.1 |
|
| 123 |
1.1 |
|
| 125 |
1.1 2.2 |
|
| 127 |
1.1 |
|
| 135 |
1.1 |
|
| 137 |
1.1 2.2 |
|
| 139 |
1.1 |