1 | package edu.ucsb.cs156.dining.controllers; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | ||
6 | import edu.ucsb.cs156.dining.statuses.ModerationStatus; | |
7 | import org.springframework.beans.factory.annotation.Autowired; | |
8 | import org.springframework.http.ResponseEntity; | |
9 | import org.springframework.security.access.prepost.PreAuthorize; | |
10 | import org.springframework.web.bind.annotation.GetMapping; | |
11 | import org.springframework.web.bind.annotation.RequestMapping; | |
12 | import org.springframework.web.bind.annotation.RestController; | |
13 | ||
14 | import edu.ucsb.cs156.dining.entities.User; | |
15 | import edu.ucsb.cs156.dining.repositories.UserRepository; | |
16 | import io.swagger.v3.oas.annotations.Operation; | |
17 | import io.swagger.v3.oas.annotations.tags.Tag; | |
18 | ||
19 | ||
20 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
21 | import edu.ucsb.cs156.dining.entities.User; | |
22 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
23 | import org.springframework.web.bind.annotation.*; | |
24 | import org.springframework.http.HttpStatus; | |
25 | import org.springframework.web.server.ResponseStatusException; | |
26 | import java.util.List; | |
27 | ||
28 | import java.time.LocalDate; | |
29 | ||
30 | /** | |
31 | * This is a REST controller for getting information about the users. | |
32 | * | |
33 | * These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
34 | */ | |
35 | ||
36 | @Tag(name="User information (admin only)") | |
37 | @RequestMapping("/api") | |
38 | @RestController | |
39 | public class UsersController extends ApiController { | |
40 | @Autowired | |
41 | UserRepository userRepository; | |
42 | ||
43 | @Autowired | |
44 | ObjectMapper mapper; | |
45 | ||
46 | /** | |
47 | * This method returns a list of all users. Accessible only to users with the role "ROLE_ADMIN". | |
48 | * @return a list of all users | |
49 | * @throws JsonProcessingException if there is an error processing the JSON | |
50 | */ | |
51 | @Operation(summary= "Get a list of all users") | |
52 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
53 | @GetMapping("/admin/users") | |
54 | public ResponseEntity<String> users() | |
55 | throws JsonProcessingException { | |
56 | ||
57 | ||
58 | Iterable<User> users = userRepository.findAll(); | |
59 | String body = mapper.writeValueAsString(users); | |
60 |
1
1. users : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
61 | } | |
62 | ||
63 | /** | |
64 | * This method returns list of all users with a proposed alias. | |
65 | * @return a list of users with a proposed alias | |
66 | * @throws JsonProcessingException if there is an error processing the JSON | |
67 | */ | |
68 | @Operation(summary = "Get a list of all users with a proposed alias") | |
69 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
70 | @GetMapping("/admin/usersWithProposedAlias") | |
71 | public ResponseEntity<String> getUsersWithProposedAlias() | |
72 | throws JsonProcessingException { | |
73 | Iterable<User> users = userRepository.findByProposedAliasNotNull(); | |
74 | String body = mapper.writeValueAsString(users); | |
75 |
1
1. getUsersWithProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getUsersWithProposedAlias → KILLED |
return ResponseEntity.ok().body(body); |
76 | } | |
77 | ||
78 | /** | |
79 | * This method allows the user to update their alias. | |
80 | * @param proposedAlias the new alias | |
81 | * @return the updated user | |
82 | */ | |
83 | @Operation(summary = "Update proposed alias of the current user") | |
84 | @PreAuthorize("hasRole('ROLE_USER')") | |
85 | @PostMapping("/currentUser/updateAlias") | |
86 | public ResponseEntity<User> updateProposedAlias(@RequestParam String proposedAlias) { | |
87 | CurrentUser currentUser = super.getCurrentUser(); | |
88 | User user = currentUser.getUser(); | |
89 | ||
90 |
1
1. updateProposedAlias : negated conditional → KILLED |
if (userRepository.findByAlias(proposedAlias).isPresent()) { |
91 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Alias already in use."); | |
92 | } | |
93 | | |
94 |
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED |
user.setProposedAlias(proposedAlias); |
95 |
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.AWAITING_REVIEW); |
96 | User savedUser = userRepository.save(user); | |
97 | | |
98 |
1
1. updateProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateProposedAlias → KILLED |
return ResponseEntity.ok(savedUser); |
99 | } | |
100 | | |
101 | /** | |
102 | * This method allows an admin to update the moderation status of a user's alias. | |
103 | * @param id the id of the user to update | |
104 | * @param approved the new moderation status | |
105 | * @return the updated user | |
106 | */ | |
107 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
108 | @PutMapping("/currentUser/updateAliasModeration") | |
109 | public User updateAliasModeration( | |
110 | @RequestParam long id, | |
111 | @RequestParam Boolean approved) { | |
112 | | |
113 | User user = userRepository.findById(id) | |
114 |
1
1. lambda$updateAliasModeration$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::lambda$updateAliasModeration$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
115 | | |
116 | ||
117 |
1
1. updateAliasModeration : negated conditional → KILLED |
if (approved) { |
118 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setAlias → KILLED |
user.setAlias(user.getProposedAlias()); |
119 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.APPROVED); |
120 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setDateApproved → KILLED |
user.setDateApproved(LocalDate.now()); |
121 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED |
user.setProposedAlias(null); |
122 | } else { | |
123 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.REJECTED); |
124 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED |
user.setProposedAlias(null); |
125 | } | |
126 | | |
127 | userRepository.save(user); | |
128 | ||
129 |
1
1. updateAliasModeration : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateAliasModeration → KILLED |
return user; |
130 | } | |
131 | } | |
Mutations | ||
60 |
1.1 |
|
75 |
1.1 |
|
90 |
1.1 |
|
94 |
1.1 |
|
95 |
1.1 |
|
98 |
1.1 |
|
114 |
1.1 |
|
117 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
121 |
1.1 |
|
123 |
1.1 |
|
124 |
1.1 |
|
129 |
1.1 |