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