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