1 | package edu.ucsb.cs156.rec.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.rec.entities.RecommendationRequest; | |
4 | import edu.ucsb.cs156.rec.errors.EntityNotFoundException; | |
5 | import edu.ucsb.cs156.rec.repositories.RecommendationRequestRepository; | |
6 | import edu.ucsb.cs156.rec.repositories.UserRepository; | |
7 | import edu.ucsb.cs156.rec.entities.User; | |
8 | import edu.ucsb.cs156.rec.entities.RequestType; | |
9 | import edu.ucsb.cs156.rec.repositories.RequestTypeRepository; | |
10 | ||
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 | import lombok.extern.slf4j.Slf4j; | |
15 | ||
16 | import com.fasterxml.jackson.core.JsonProcessingException; | |
17 | ||
18 | import org.checkerframework.checker.units.qual.g; | |
19 | import org.springframework.beans.factory.annotation.Autowired; | |
20 | import org.springframework.boot.context.properties.source.IterableConfigurationPropertySource; | |
21 | import org.springframework.format.annotation.DateTimeFormat; | |
22 | import org.springframework.http.ResponseEntity; | |
23 | import org.springframework.security.access.prepost.PreAuthorize; | |
24 | import org.springframework.web.bind.annotation.DeleteMapping; | |
25 | import org.springframework.web.bind.annotation.GetMapping; | |
26 | import org.springframework.web.bind.annotation.PathVariable; | |
27 | import org.springframework.web.bind.annotation.PostMapping; | |
28 | import org.springframework.web.bind.annotation.PutMapping; | |
29 | import org.springframework.web.bind.annotation.RequestBody; | |
30 | import org.springframework.web.bind.annotation.RequestMapping; | |
31 | import org.springframework.web.bind.annotation.RequestParam; | |
32 | import org.springframework.web.bind.annotation.RestController; | |
33 | import org.springframework.security.access.AccessDeniedException; | |
34 | import org.springframework.http.HttpStatus; | |
35 | ||
36 | import jakarta.validation.Valid; | |
37 | ||
38 | import java.time.LocalDate; | |
39 | import java.util.List; | |
40 | ||
41 | /** | |
42 | * This is a REST controller for RecommendationRequest | |
43 | */ | |
44 | ||
45 | @Tag(name = "RecommendationRequest") | |
46 | @RequestMapping("/api/recommendationrequest") | |
47 | @RestController | |
48 | @Slf4j | |
49 | public class RecommendationRequestController extends ApiController{ | |
50 | | |
51 | @Autowired | |
52 | RecommendationRequestRepository recommendationRequestRepository; | |
53 | ||
54 | @Autowired | |
55 | UserRepository userRepository; | |
56 | ||
57 | @Autowired | |
58 | RequestTypeRepository requestTypeRepository; | |
59 | ||
60 | /** | |
61 | * List all recommendation requests | |
62 | * | |
63 | * @return an iterable of RecommendationRequest | |
64 | */ | |
65 | @Operation(summary= "List all recommendation requests") | |
66 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
67 | @GetMapping("/all") | |
68 | public Iterable<RecommendationRequest> allRecommendationRequests() { | |
69 | Iterable<RecommendationRequest> requests = recommendationRequestRepository.findAll(); | |
70 |
1
1. allRecommendationRequests : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::allRecommendationRequests → KILLED |
return requests; |
71 | } | |
72 | ||
73 | /** | |
74 | * Get a single recommendation request by id | |
75 | * | |
76 | * @param id the id of the recommendation request | |
77 | * @throws EntityNotFoundException if the recommendation request is not found | |
78 | * @return a recommendation request | |
79 | */ | |
80 | @Operation(summary= "Get a single recommendation request") | |
81 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
82 | @GetMapping("") | |
83 | public RecommendationRequest getById( | |
84 | @Parameter(name="id") @RequestParam Long id) { | |
85 | RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id) | |
86 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
87 | ||
88 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getById → KILLED |
return recommendationRequest; |
89 | } | |
90 | ||
91 | /** | |
92 | * Create a new recommendation request | |
93 | * @param professorEmail the email of the professor | |
94 | * @param recommendationType the type of recommendations | |
95 | * @param details the other details of the request | |
96 | * @throws EntityNotFoundException if the professor is not found or is hte user does not have the professor role | |
97 | * @return the created RecommendationRequest | |
98 | */ | |
99 | | |
100 | @Operation(summary= "Create a new recommendation request") | |
101 | @PreAuthorize("hasRole('ROLE_STUDENT')") | |
102 | @PostMapping("/post") | |
103 | public RecommendationRequest postRecommendationRequest( | |
104 | @Parameter(name="professorEmail") @RequestParam String professorEmail, | |
105 | @Parameter(name="recommendationType") @RequestParam String recommendationType, | |
106 | @Parameter(name="details") @RequestParam String details) | |
107 | throws JsonProcessingException { | |
108 | ||
109 | User prof = userRepository.findByEmail(professorEmail) | |
110 |
1
1. lambda$postRecommendationRequest$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequest$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, professorEmail)); |
111 | | |
112 |
1
1. postRecommendationRequest : negated conditional → KILLED |
if (!prof.getProfessor()) { |
113 | throw new EntityNotFoundException(User.class, professorEmail); | |
114 | } | |
115 | ||
116 | RequestType type = requestTypeRepository.findByRequestType(recommendationType) | |
117 |
1
1. lambda$postRecommendationRequest$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$postRecommendationRequest$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RequestType.class, recommendationType)); |
118 | ||
119 | LocalDate currDate = LocalDate.now(); | |
120 | ||
121 | RecommendationRequest recommendationRequest = new RecommendationRequest(); | |
122 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setProfessor → KILLED |
recommendationRequest.setProfessor(prof); |
123 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRequester → KILLED |
recommendationRequest.setRequester(getCurrentUser().getUser()); |
124 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRecommendationType → KILLED |
recommendationRequest.setRecommendationType(type); |
125 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(details); |
126 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setSubmissionDate → KILLED |
recommendationRequest.setSubmissionDate(currDate); |
127 |
1
1. postRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus("pending"); |
128 | ||
129 | RecommendationRequest savedRecommendationRequest = recommendationRequestRepository.save(recommendationRequest); | |
130 | ||
131 |
1
1. postRecommendationRequest : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::postRecommendationRequest → KILLED |
return savedRecommendationRequest; |
132 | } | |
133 | ||
134 | /** | |
135 | * Delete a recommendation request | |
136 | * | |
137 | * @param id the id of the recommendation request to delete | |
138 | * @throws EntityNotFoundException if the recommendation request is not found | |
139 | * @return a message indicating the recommedation request was deleted | |
140 | */ | |
141 | @Operation(summary= "Delete a recommendation request") | |
142 | @PreAuthorize("hasRole('ROLE_USER')") | |
143 | @DeleteMapping("") | |
144 | public Object deleteRecommendationRequest( | |
145 | @Parameter(name="id") @RequestParam Long id) { | |
146 | RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id) | |
147 |
1
1. lambda$deleteRecommendationRequest$3 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$deleteRecommendationRequest$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
148 | ||
149 |
1
1. deleteRecommendationRequest : negated conditional → KILLED |
if( recommendationRequest.getRequester().getId() != getCurrentUser().getUser().getId()) { |
150 | throw new AccessDeniedException(null); | |
151 | } | |
152 | ||
153 |
1
1. deleteRecommendationRequest : removed call to edu/ucsb/cs156/rec/repositories/RecommendationRequestRepository::delete → KILLED |
recommendationRequestRepository.delete(recommendationRequest); |
154 |
1
1. deleteRecommendationRequest : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::deleteRecommendationRequest → KILLED |
return genericMessage("RecommendationRequest with id %s deleted".formatted(id)); |
155 | } | |
156 | ||
157 | /** | |
158 | * Update a single recommendation request | |
159 | * | |
160 | * @param id id of the recommendation request to update | |
161 | * @param incoming the new recommendation request | |
162 | * @throws EntityNotFoundException if the recommendation request is not found | |
163 | * @return the updated recommendation request object | |
164 | */ | |
165 | @Operation(summary= "Update a single recommendation request") | |
166 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')") | |
167 | @PutMapping("") | |
168 | public RecommendationRequest updateRecommendationRequest( | |
169 | @Parameter(name="id") @RequestParam Long id, | |
170 | @RequestBody @Valid RecommendationRequest incoming) { | |
171 | ||
172 | RecommendationRequest recommendationRequest = recommendationRequestRepository.findById(id) | |
173 |
1
1. lambda$updateRecommendationRequest$4 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::lambda$updateRecommendationRequest$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id)); |
174 | ||
175 | LocalDate currDate = LocalDate.now(); | |
176 | ||
177 |
1
1. updateRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setRecommendationType → KILLED |
recommendationRequest.setRecommendationType(incoming.getRecommendationType()); |
178 |
1
1. updateRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setDetails → KILLED |
recommendationRequest.setDetails(incoming.getDetails()); |
179 |
1
1. updateRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setCompletionDate → KILLED |
recommendationRequest.setCompletionDate(currDate); |
180 |
1
1. updateRecommendationRequest : removed call to edu/ucsb/cs156/rec/entities/RecommendationRequest::setStatus → KILLED |
recommendationRequest.setStatus(incoming.getStatus()); |
181 | ||
182 | recommendationRequestRepository.save(recommendationRequest); | |
183 | ||
184 |
1
1. updateRecommendationRequest : replaced return value with null for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::updateRecommendationRequest → KILLED |
return recommendationRequest; |
185 | } | |
186 | ||
187 | /** | |
188 | * Get all recommendation requests by Professor id | |
189 | * | |
190 | * @param userId the user Id of the professor | |
191 | * @throws EntityNotFoundException if the professor is not found | |
192 | * @return a list of all rec reqs directed towards the professor with the given userId | |
193 | */ | |
194 | @Operation(summary= "Get all recommendation requests by Professor's user id") | |
195 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')") | |
196 | @GetMapping("/professor/{userId}") | |
197 | public List<RecommendationRequest> getAllRecommendationRequestsByProfessor( | |
198 | @PathVariable(value="userId") long userId) { | |
199 | ||
200 |
1
1. getAllRecommendationRequestsByProfessor : negated conditional → KILLED |
if( !userRepository.existsById(userId)) { |
201 | throw new EntityNotFoundException(User.class, userId); | |
202 | } | |
203 | List<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByProfessorId(userId); | |
204 |
1
1. getAllRecommendationRequestsByProfessor : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getAllRecommendationRequestsByProfessor → KILLED |
return recommendationRequests; |
205 | } | |
206 | ||
207 | /** | |
208 | * Get all recommendation requests by Requester id | |
209 | * @param userId the user Id of the requester | |
210 | * @throws EntityNotFoundException if the requester is not found | |
211 | * @return a list of all rec reqs made by the requester with the given userId | |
212 | */ | |
213 | @Operation(summary = "Get all recommendation requests by Requester's user id") | |
214 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_STUDENT')") | |
215 | @GetMapping("/requester/{userId}") | |
216 | public List<RecommendationRequest> getAllRecommendationRequestsByRequester( | |
217 | @PathVariable(value="userId") long userId){ | |
218 | ||
219 |
1
1. getAllRecommendationRequestsByRequester : negated conditional → KILLED |
if( !userRepository.existsById(userId)) { |
220 | throw new EntityNotFoundException(User.class, userId); | |
221 | } | |
222 | List<RecommendationRequest> recommendationRequests = recommendationRequestRepository.findAllByRequesterId(userId); | |
223 |
1
1. getAllRecommendationRequestsByRequester : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RecommendationRequestController::getAllRecommendationRequestsByRequester → KILLED |
return recommendationRequests; |
224 | ||
225 | } | |
226 | } | |
Mutations | ||
70 |
1.1 |
|
86 |
1.1 |
|
88 |
1.1 |
|
110 |
1.1 |
|
112 |
1.1 |
|
117 |
1.1 |
|
122 |
1.1 |
|
123 |
1.1 |
|
124 |
1.1 |
|
125 |
1.1 |
|
126 |
1.1 |
|
127 |
1.1 |
|
131 |
1.1 |
|
147 |
1.1 |
|
149 |
1.1 |
|
153 |
1.1 |
|
154 |
1.1 |
|
173 |
1.1 |
|
177 |
1.1 |
|
178 |
1.1 |
|
179 |
1.1 |
|
180 |
1.1 |
|
184 |
1.1 |
|
200 |
1.1 |
|
204 |
1.1 |
|
219 |
1.1 |
|
223 |
1.1 |