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 | * Any admin can delete a RecommendationRequest | |
48 | * | |
49 | * @param id the id of the RecommendationRequest to delete | |
50 | * @return a message indicating that the RecommendationRequest was deleted | |
51 | */ | |
52 | @Operation(summary = "An admin can delete a RecommendationRequest") | |
53 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
54 | @DeleteMapping("/admin") | |
55 | public Object deleteRecommendationRequestAsAdmin(@Parameter(name = "id") @RequestParam Long id) { | |
56 | RecommendationRequest recommendationRequest = | |
57 | recommendationRequestRepository | |
58 | .findById(id) | |
59 |
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)); |
60 | ||
61 |
1
1. deleteRecommendationRequestAsAdmin : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
62 | ||
63 |
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)); |
64 | } | |
65 | ||
66 | /** | |
67 | * The user who posted a RecommendationRequest can delete their RecommendationRequest | |
68 | * | |
69 | * @param id the id of the RecommendationRequest to delete | |
70 | * @return a message indicating that the RecommendationRequest was deleted | |
71 | */ | |
72 | @Operation(summary = "User can delete their RecommendationRequest") | |
73 | @PreAuthorize("hasRole('ROLE_USER')") | |
74 | @DeleteMapping("") | |
75 | public Object deleteRecommendationRequestAsUser(@Parameter(name = "id") @RequestParam Long id) { | |
76 | User currentUser = getCurrentUser().getUser(); | |
77 | RecommendationRequest recommendationRequest = | |
78 | recommendationRequestRepository | |
79 | .findByIdAndRequester(id, currentUser) | |
80 |
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)); |
81 | ||
82 |
1
1. deleteRecommendationRequestAsUser : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
83 | ||
84 |
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)); |
85 | } | |
86 | ||
87 | /** | |
88 | * The user who posted a RecommendationRequest can update their RecommendationRequest | |
89 | * | |
90 | * @param id the id of the Recommendation Request to update | |
91 | * @param incoming the updated Recommendation Request | |
92 | * @return the updated Recommendation Request object | |
93 | */ | |
94 | @Operation(summary = "User can update their RecommendationRequest") | |
95 | @PreAuthorize("hasRole('ROLE_USER')") | |
96 | @PutMapping("") | |
97 | public RecommendationRequest updateRecommendationRequestAsUser( | |
98 | @Parameter(name = "id") @RequestParam Long id, | |
99 | @RequestBody @Valid RecommendationRequest incoming) { | |
100 | ||
101 | User currentUser = getCurrentUser().getUser(); | |
102 | RecommendationRequest recommendationRequest = | |
103 | recommendationRequestRepository | |
104 | .findByIdAndRequester(id, currentUser) | |
105 |
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)); |
106 | ||
107 |
1
1. updateRecommendationRequestAsUser : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(incoming.getDetails()); |
108 | ||
109 | recommendationRequestRepository.save(recommendationRequest); | |
110 | | |
111 |
1
1. updateRecommendationRequestAsUser : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::updateRecommendationRequestAsUser → KILLED |
return recommendationRequest; |
112 | } | |
113 | ||
114 | /** | |
115 | * Prof can update a Recommendation Request's status | |
116 | * | |
117 | * @param id the id of the Recommendation Request to update | |
118 | * @param incoming the updated Recommendation Request | |
119 | * @return the updated Recommendation Request object | |
120 | */ | |
121 | @Operation(summary = "A Professor can update a recommendation request's status") | |
122 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
123 | @PutMapping("/professor") | |
124 | public RecommendationRequest updateRecommendationRequestAsAdmin( | |
125 | @Parameter(name = "id") @RequestParam Long id, | |
126 | @RequestBody @Valid RecommendationRequest incoming) { | |
127 | ||
128 | RecommendationRequest recommendationRequest = | |
129 | recommendationRequestRepository | |
130 | .findById(id) | |
131 |
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)); |
132 | ||
133 |
1
1. updateRecommendationRequestAsAdmin : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus(incoming.getStatus()); |
134 | ||
135 | recommendationRequestRepository.save(recommendationRequest); | |
136 | ||
137 |
1
1. updateRecommendationRequestAsAdmin : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::updateRecommendationRequestAsAdmin → KILLED |
return recommendationRequest; |
138 | } | |
139 | ||
140 | /** | |
141 | * This method returns a list of all Recommendation Requests requested by current student. | |
142 | * @return a list of all Recommendation Requests requested by the current user | |
143 | */ | |
144 | @Operation(summary = "List all Recommendation Requests requested by current user") | |
145 | @PreAuthorize("hasRole('ROLE_USER')") | |
146 | @GetMapping("/requester/all") | |
147 | public Iterable<RecommendationRequest> allRequesterRecommendationRequests( | |
148 | ) { | |
149 | // 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 | |
150 | User currentUser = getCurrentUser().getUser(); | |
151 | Iterable<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByRequesterId(currentUser.getId()); | |
152 |
1
1. allRequesterRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allRequesterRecommendationRequests → KILLED |
return recommendationRequests; |
153 | } | |
154 | ||
155 | /** | |
156 | * This method returns a list of all Recommendation Requests intended for current user who is a professor. | |
157 | * @return a list of all Recommendation Requests intended for the current user who is a professor | |
158 | */ | |
159 | @Operation(summary = "List all Recommendation Requests for professor") | |
160 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
161 | @GetMapping("/professor/all") | |
162 | public Iterable<RecommendationRequest> allProfessorRecommendationRequests( | |
163 | ) { | |
164 | User currentUser = getCurrentUser().getUser(); | |
165 | Iterable<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByProfessorId(currentUser.getId()); | |
166 |
1
1. allProfessorRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allProfessorRecommendationRequests → KILLED |
return recommendationRequests; |
167 | } | |
168 | ||
169 | /** | |
170 | * This method returns a single recommendation request where the current user is either the requester or the professor. | |
171 | * @param id id of the Recommendation Requests to get | |
172 | * @return a single recommendation request where the current user is either the requester or the professor | |
173 | */ | |
174 | @Operation(summary = "Get a single recommendation request where the current user is either the requester or the professor") | |
175 | @PreAuthorize("hasRole('ROLE_USER')") | |
176 | @GetMapping("") | |
177 | public RecommendationRequest getById( | |
178 | @Parameter(name = "id") @RequestParam Long id) { | |
179 | Long currentUserId = getCurrentUser().getUser().getId(); | |
180 | RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id) | |
181 |
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)); |
182 |
2
1. getById : negated conditional → KILLED 2. getById : negated conditional → KILLED |
if (recommendationRequest.getRequester().getId() != currentUserId && recommendationRequest.getProfessor().getId() != currentUserId) { |
183 | throw new EntityNotFoundException(RecommendationRequest.class, id); | |
184 | } | |
185 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getById → KILLED |
return recommendationRequest; |
186 | } | |
187 | ||
188 | /** | |
189 | * This method creates a new Recommendation Request. Accessible only to users with the role "ROLE_USER" so professors and students can both create. | |
190 | * @param professorId id from a dropdown of professors from the form in create page | |
191 | * @param recommendationType recommendation types of request | |
192 | * @param details details of request | |
193 | * @param dueDate submission date of request | |
194 | * @return the save recommendationrequests (with it's id field set by the database) | |
195 | */ | |
196 | ||
197 | @Operation(summary = "Create a new recommendation request") | |
198 | @PreAuthorize("hasRole('ROLE_USER')") | |
199 | @PostMapping("/post") | |
200 | public RecommendationRequest postRecommendationRequests( | |
201 | @Parameter(name = "professorId") @RequestParam Long professorId, | |
202 | @Parameter(name = "recommendationType") @RequestParam String recommendationType, | |
203 | @Parameter(name = "details") @RequestParam String details, | |
204 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dueDate) | |
205 | { | |
206 | //get current date right now and set status to pending | |
207 | CurrentUser currentUser = getCurrentUser(); | |
208 | RecommendationRequest recommendationRequest = new RecommendationRequest(); | |
209 |
1
1. postRecommendationRequests : negated conditional → KILLED |
if (!recommendationType.equals("Other")) { |
210 |
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)); |
211 | } | |
212 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRecommendationType → KILLED |
recommendationRequest.setRecommendationType(recommendationType); |
213 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(details); |
214 |
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)); |
215 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setProfessor → KILLED |
recommendationRequest.setProfessor(professor); |
216 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequester → KILLED |
recommendationRequest.setRequester(currentUser.getUser()); |
217 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus("PENDING"); |
218 |
1
1. postRecommendationRequests : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDueDate → KILLED |
recommendationRequest.setDueDate(dueDate); |
219 | ||
220 | RecommendationRequest savedRecommendationRequest = recommendationRequestRepository.save(recommendationRequest); | |
221 |
1
1. postRecommendationRequests : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::postRecommendationRequests → KILLED |
return savedRecommendationRequest; |
222 | } | |
223 | ||
224 | /** | |
225 | * This method returns a list of recommendation requests with specified status for a professor. | |
226 | * @return a list of recommendation requests with specified status for a professor. | |
227 | */ | |
228 | @Operation(summary = "Get all recommendation requests with specified status for a professor") | |
229 | @GetMapping("/professor/filtered") | |
230 | @PreAuthorize("hasRole('ROLE_PROFESSOR')") | |
231 | public Iterable<RecommendationRequest> getRecommendationRequestByStatusForProfessor( | |
232 | @RequestParam String status) { | |
233 | User currentUser = getCurrentUser().getUser(); | |
234 | ||
235 |
1
1. getRecommendationRequestByStatusForProfessor : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getRecommendationRequestByStatusForProfessor → KILLED |
return recommendationRequestRepository.findAllByProfessorIdAndStatus( |
236 | currentUser.getId(), status); | |
237 | } | |
238 | } | |
Mutations | ||
59 |
1.1 |
|
61 |
1.1 |
|
63 |
1.1 |
|
80 |
1.1 |
|
82 |
1.1 |
|
84 |
1.1 |
|
105 |
1.1 |
|
107 |
1.1 |
|
111 |
1.1 |
|
131 |
1.1 |
|
133 |
1.1 |
|
137 |
1.1 |
|
152 |
1.1 |
|
166 |
1.1 |
|
181 |
1.1 |
|
182 |
1.1 2.2 |
|
185 |
1.1 |
|
209 |
1.1 |
|
210 |
1.1 |
|
212 |
1.1 |
|
213 |
1.1 |
|
214 |
1.1 |
|
215 |
1.1 |
|
216 |
1.1 |
|
217 |
1.1 |
|
218 |
1.1 |
|
221 |
1.1 |
|
235 |
1.1 |