1 | package edu.ucsb.cs156.rec.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.rec.entities.RecommendationRequest; | |
4 | import edu.ucsb.cs156.rec.entities.RequestType; | |
5 | import edu.ucsb.cs156.rec.entities.User; | |
6 | import edu.ucsb.cs156.rec.errors.EntityNotFoundException; | |
7 | import edu.ucsb.cs156.rec.models.CurrentUser; | |
8 | import edu.ucsb.cs156.rec.repositories.RecommendationRequestRepository; | |
9 | import edu.ucsb.cs156.rec.repositories.RequestTypeRepository; | |
10 | import edu.ucsb.cs156.rec.repositories.UserRepository; | |
11 | import io.swagger.v3.oas.annotations.Operation; | |
12 | import io.swagger.v3.oas.annotations.Parameter; | |
13 | import io.swagger.v3.oas.annotations.tags.Tag; | |
14 | ||
15 | import java.time.LocalDateTime; | |
16 | import java.time.temporal.ChronoUnit; | |
17 | ||
18 | import org.springframework.beans.factory.annotation.Autowired; | |
19 | import org.springframework.format.annotation.DateTimeFormat; | |
20 | import org.springframework.security.access.prepost.PreAuthorize; | |
21 | import org.springframework.web.bind.annotation.DeleteMapping; | |
22 | import org.springframework.web.bind.annotation.GetMapping; | |
23 | import org.springframework.web.bind.annotation.PutMapping; | |
24 | import org.springframework.web.bind.annotation.PostMapping; | |
25 | import org.springframework.web.bind.annotation.RequestMapping; | |
26 | import org.springframework.web.bind.annotation.RequestBody; | |
27 | import org.springframework.web.bind.annotation.RequestParam; | |
28 | import org.springframework.web.bind.annotation.RestController; | |
29 | import jakarta.validation.Valid; | |
30 | import lombok.extern.slf4j.Slf4j; | |
31 | ||
32 | @Tag(name = "RecommendationRequest") | |
33 | @RequestMapping("/api/recommendationrequest") | |
34 | @RestController | |
35 | @Slf4j | |
36 | public class RecommendationRequestController extends ApiController { | |
37 | | |
38 | @Autowired | |
39 | RecommendationRequestRepository recommendationRequestRepository; | |
40 | ||
41 | @Autowired | |
42 | UserRepository userRepository; | |
43 | ||
44 | @Autowired | |
45 | RequestTypeRepository requestTypeRepository; | |
46 | ||
47 | /** | |
48 | * Any admin can delete a RecommendationRequest | |
49 | * | |
50 | * @param id the id of the RecommendationRequest to delete | |
51 | * @return a message indicating that the RecommendationRequest was deleted | |
52 | */ | |
53 | @Operation(summary = "An admin can delete a RecommendationRequest") | |
54 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
55 | @DeleteMapping("/admin") | |
56 | public Object deleteRecommendationRequestAsAdmin(@Parameter(name = "id") @RequestParam Long id) { | |
57 | RecommendationRequest recommendationRequest = | |
58 | recommendationRequestRepository | |
59 | .findById(id) | |
60 |
1
1. lambda$deleteRecommendationRequestAsAdmin$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$deleteRecommendationRequestAsAdmin$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
61 | ||
62 |
1
1. deleteRecommendationRequestAsAdmin : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
63 | ||
64 |
1
1. deleteRecommendationRequestAsAdmin : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::deleteRecommendationRequestAsAdmin → KILLED |
return genericMessage("RecommendationRequest with id %s deleted".formatted(id)); |
65 | } | |
66 | ||
67 | /** | |
68 | * The user who posted a RecommendationRequest can delete their RecommendationRequest | |
69 | * | |
70 | * @param id the id of the RecommendationRequest to delete | |
71 | * @return a message indicating that the RecommendationRequest was deleted | |
72 | */ | |
73 | @Operation(summary = "User can delete their RecommendationRequest") | |
74 | @PreAuthorize("hasRole('ROLE_USER')") | |
75 | @DeleteMapping("") | |
76 | public Object deleteRecommendationRequestAsUser(@Parameter(name = "id") @RequestParam Long id) { | |
77 | User currentUser = getCurrentUser().getUser(); | |
78 | RecommendationRequest recommendationRequest = | |
79 | recommendationRequestRepository | |
80 | .findByIdAndRequester(id, currentUser) | |
81 |
1
1. lambda$deleteRecommendationRequestAsUser$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$deleteRecommendationRequestAsUser$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
82 | ||
83 |
1
1. deleteRecommendationRequestAsUser : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
84 | ||
85 |
1
1. deleteRecommendationRequestAsUser : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::deleteRecommendationRequestAsUser → KILLED |
return genericMessage("RecommendationRequest with id %s deleted".formatted(id)); |
86 | } | |
87 | ||
88 | /** | |
89 | * The professor can delete incoming RecommendationRequests | |
90 | * | |
91 | * @param id the id of the RecommendationRequest to delete | |
92 | * @return a message indicating that the RecommendationRequest was deleted | |
93 | */ | |
94 | @Operation(summary = "Professor can delete their incoming RecommendationRequest") | |
95 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
96 | @DeleteMapping("/professor") | |
97 | public Object deleteRecommendationRequestAsProfessor(@Parameter(name = "id") @RequestParam Long id) { | |
98 | User currentUser = getCurrentUser().getUser(); | |
99 | RecommendationRequest recommendationRequest = | |
100 | recommendationRequestRepository | |
101 | .findByIdAndProfessor(id, currentUser) | |
102 |
1
1. lambda$deleteRecommendationRequestAsProfessor$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$deleteRecommendationRequestAsProfessor$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
103 | ||
104 |
1
1. deleteRecommendationRequestAsProfessor : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
105 | ||
106 |
1
1. deleteRecommendationRequestAsProfessor : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::deleteRecommendationRequestAsProfessor → KILLED |
return genericMessage("RecommendationRequest with id %s deleted".formatted(id)); |
107 | } | |
108 | ||
109 | ||
110 | /** | |
111 | * The user who posted a RecommendationRequest can update their RecommendationRequest | |
112 | * | |
113 | * @param id the id of the Recommendation Request to update | |
114 | * @param incoming the updated Recommendation Request | |
115 | * @return the updated Recommendation Request object | |
116 | */ | |
117 | @Operation(summary = "User can update their RecommendationRequest") | |
118 | @PreAuthorize("hasRole('ROLE_USER')") | |
119 | @PutMapping("") | |
120 | public RecommendationRequest updateRecommendationRequestAsUser( | |
121 | @Parameter(name = "id") @RequestParam Long id, | |
122 | @RequestBody @Valid RecommendationRequest incoming) { | |
123 | ||
124 | User currentUser = getCurrentUser().getUser(); | |
125 | RecommendationRequest recommendationRequest = | |
126 | recommendationRequestRepository | |
127 | .findByIdAndRequester(id, currentUser) | |
128 |
1
1. lambda$updateRecommendationRequestAsUser$3 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$updateRecommendationRequestAsUser$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
129 | ||
130 |
1
1. updateRecommendationRequestAsUser : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(incoming.getDetails()); |
131 | ||
132 | recommendationRequestRepository.save(recommendationRequest); | |
133 | | |
134 |
1
1. updateRecommendationRequestAsUser : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::updateRecommendationRequestAsUser → KILLED |
return recommendationRequest; |
135 | } | |
136 | ||
137 | /** | |
138 | * Prof can update a Recommendation Request's status | |
139 | * | |
140 | * @param id the id of the Recommendation Request to update | |
141 | * @param incoming the updated Recommendation Request | |
142 | * @return the updated Recommendation Request object | |
143 | */ | |
144 | @Operation(summary = "A Professor can update a recommendation request's status") | |
145 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
146 | @PutMapping("/professor") | |
147 | public RecommendationRequest updateRecommendationRequestAsAdmin( | |
148 | @Parameter(name = "id") @RequestParam Long id, | |
149 | @RequestBody @Valid RecommendationRequest incoming) { | |
150 | ||
151 | RecommendationRequest recommendationRequest = | |
152 | recommendationRequestRepository | |
153 | .findById(id) | |
154 |
1
1. lambda$updateRecommendationRequestAsAdmin$4 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$updateRecommendationRequestAsAdmin$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
155 | ||
156 | | |
157 | ||
158 |
1
1. updateRecommendationRequestAsAdmin : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus(incoming.getStatus()); |
159 | ||
160 |
1
1. updateRecommendationRequestAsAdmin : negated conditional → KILLED |
if ("COMPLETED".equalsIgnoreCase(incoming.getStatus())) { |
161 |
1
1. updateRecommendationRequestAsAdmin : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setCompletionDate → KILLED |
recommendationRequest.setCompletionDate(LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES)); |
162 | } | |
163 | ||
164 | recommendationRequestRepository.save(recommendationRequest); | |
165 | ||
166 |
1
1. updateRecommendationRequestAsAdmin : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::updateRecommendationRequestAsAdmin → KILLED |
return recommendationRequest; |
167 | } | |
168 | ||
169 | /** | |
170 | * This method returns a list of all Recommendation Requests requested by current student. | |
171 | * @return a list of all Recommendation Requests requested by the current user | |
172 | */ | |
173 | @Operation(summary = "List all Recommendation Requests requested by current user") | |
174 | @PreAuthorize("hasRole('ROLE_USER')") | |
175 | @GetMapping("/requester/all") | |
176 | public Iterable<RecommendationRequest> allRequesterRecommendationRequests( | |
177 | ) { | |
178 | User currentUser = getCurrentUser().getUser(); | |
179 | Iterable<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByRequesterId(currentUser.getId()); | |
180 |
1
1. allRequesterRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allRequesterRecommendationRequests → KILLED |
return recommendationRequests; |
181 | } | |
182 | ||
183 | /** | |
184 | * This method returns a list of all Recommendation Requests intended for current user who is a professor. | |
185 | * @return a list of all Recommendation Requests intended for the current user who is a professor | |
186 | */ | |
187 | @Operation(summary = "List all Recommendation Requests for professor") | |
188 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
189 | @GetMapping("/professor/all") | |
190 | public Iterable<RecommendationRequest> allProfessorRecommendationRequests( | |
191 | ) { | |
192 | User currentUser = getCurrentUser().getUser(); | |
193 | Iterable<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByProfessorId(currentUser.getId()); | |
194 |
1
1. allProfessorRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allProfessorRecommendationRequests → KILLED |
return recommendationRequests; |
195 | } | |
196 | ||
197 | /** | |
198 | * This method returns a single recommendation request where the current user is either the requester or the professor. | |
199 | * @param id id of the Recommendation Requests to get | |
200 | * @return a single recommendation request where the current user is either the requester or the professor | |
201 | */ | |
202 | @Operation(summary = "Get a single recommendation request where the current user is either the requester or the professor") | |
203 | @PreAuthorize("hasRole('ROLE_USER')") | |
204 | @GetMapping("") | |
205 | public RecommendationRequest getById( | |
206 | @Parameter(name = "id") @RequestParam Long id) { | |
207 | Long currentUserId = getCurrentUser().getUser().getId(); | |
208 | RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id) | |
209 |
1
1. lambda$getById$5 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$getById$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
210 |
2
1. getById : negated conditional → KILLED 2. getById : negated conditional → KILLED |
if (recommendationRequest.getRequester().getId() != currentUserId && recommendationRequest.getProfessor().getId() != currentUserId) { |
211 | throw new EntityNotFoundException(RecommendationRequest.class, id); | |
212 | } | |
213 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getById → KILLED |
return recommendationRequest; |
214 | } | |
215 | ||
216 | /** | |
217 | * This method creates a new Recommendation Request. Accessible only to users with the role "ROLE_USER" so professors and students can both create. | |
218 | * @param professorId id from a dropdown of professors from the form in create page | |
219 | * @param recommendationType recommendation types of request | |
220 | * @param details details of request | |
221 | * @param dueDate submission date of request | |
222 | * @return the save recommendationrequests (with it's id field set by the database) | |
223 | */ | |
224 | ||
225 | @Operation(summary = "Create a new recommendation request") | |
226 | @PreAuthorize("hasRole('ROLE_USER')") | |
227 | @PostMapping("/post") | |
228 | public RecommendationRequest postRecommendationRequests( | |
229 | @Parameter(name = "professorId") @RequestParam Long professorId, | |
230 | @Parameter(name = "recommendationType") @RequestParam String recommendationType, | |
231 | @Parameter(name = "details") @RequestParam String details, | |
232 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dueDate) | |
233 | { | |
234 | //get current date right now and set status to pending | |
235 | CurrentUser currentUser = getCurrentUser(); | |
236 | RecommendationRequest recommendationRequest = new RecommendationRequest(); | |
237 |
1
1. postRecommendationRequests : negated conditional → KILLED |
if (!recommendationType.equals("Other")) { |
238 |
1
1. lambda$postRecommendationRequests$6 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequests$6 → KILLED |
requestTypeRepository.findByRequestType(recommendationType).orElseThrow(() -> new EntityNotFoundException(RequestType.class, recommendationType)); |
239 | } | |
240 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRecommendationType → KILLED |
recommendationRequest.setRecommendationType(recommendationType); |
241 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(details); |
242 |
1
1. lambda$postRecommendationRequests$7 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequests$7 → KILLED |
User professor = userRepository.findById(professorId).orElseThrow(() -> new EntityNotFoundException(User.class, professorId)); |
243 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setProfessor → KILLED |
recommendationRequest.setProfessor(professor); |
244 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequester → KILLED |
recommendationRequest.setRequester(currentUser.getUser()); |
245 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus("PENDING"); |
246 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDueDate → KILLED |
recommendationRequest.setDueDate(dueDate); |
247 | ||
248 | RecommendationRequest savedRecommendationRequest = recommendationRequestRepository.save(recommendationRequest); | |
249 |
1
1. postRecommendationRequests : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::postRecommendationRequests → KILLED |
return savedRecommendationRequest; |
250 | } | |
251 | ||
252 | /** | |
253 | * This method returns a list of recommendation requests with specified status for a professor. | |
254 | * @return a list of recommendation requests with specified status for a professor. | |
255 | */ | |
256 | @Operation(summary = "Get all recommendation requests with specified status for a professor") | |
257 | @GetMapping("/professor/filtered") | |
258 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
259 | public Iterable<RecommendationRequest> getRecommendationRequestByStatusForProfessor( | |
260 | @RequestParam String status) { | |
261 | User currentUser = getCurrentUser().getUser(); | |
262 | ||
263 |
1
1. getRecommendationRequestByStatusForProfessor : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getRecommendationRequestByStatusForProfessor → KILLED |
return recommendationRequestRepository.findAllByProfessorIdAndStatus( |
264 | currentUser.getId(), status); | |
265 | } | |
266 | /** | |
267 | * This method returns a list of all recommendation requests viewable by an admin user. | |
268 | * @return a list of all recommendation requests | |
269 | */ | |
270 | @Operation(summary = "Get all recommendation requests viewable by an admin user") | |
271 | @GetMapping("/admin") | |
272 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
273 | public Iterable<RecommendationRequest> getAllRecommendationRequests() { | |
274 |
1
1. getAllRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getAllRecommendationRequests → KILLED |
return recommendationRequestRepository.findAll(); |
275 | } | |
276 | } | |
Mutations | ||
60 |
1.1 |
|
62 |
1.1 |
|
64 |
1.1 |
|
81 |
1.1 |
|
83 |
1.1 |
|
85 |
1.1 |
|
102 |
1.1 |
|
104 |
1.1 |
|
106 |
1.1 |
|
128 |
1.1 |
|
130 |
1.1 |
|
134 |
1.1 |
|
154 |
1.1 |
|
158 |
1.1 |
|
160 |
1.1 |
|
161 |
1.1 |
|
166 |
1.1 |
|
180 |
1.1 |
|
194 |
1.1 |
|
209 |
1.1 |
|
210 |
1.1 2.2 |
|
213 |
1.1 |
|
237 |
1.1 |
|
238 |
1.1 |
|
240 |
1.1 |
|
241 |
1.1 |
|
242 |
1.1 |
|
243 |
1.1 |
|
244 |
1.1 |
|
245 |
1.1 |
|
246 |
1.1 |
|
249 |
1.1 |
|
263 |
1.1 |
|
274 |
1.1 |