RecommendationRequestController.java
package edu.ucsb.cs156.example.controllers;
import edu.ucsb.cs156.example.entities.RecommendationRequest;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.RecommendationRequestRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/** This is a REST controller for RecommendationRequests. */
@Tag(name = "RecommendationRequests")
@RequestMapping("/api/recommendationrequests")
@RestController
@Slf4j
public class RecommendationRequestController extends ApiController {
@Autowired RecommendationRequestRepository recommendationRequestRepository;
/**
* List all recommendation requests.
*
* @return an iterable of RecommendationRequest
*/
@Operation(summary = "List all recommendation requests")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/all")
public Iterable<RecommendationRequest> allRecommendationRequests() {
return recommendationRequestRepository.findAll();
}
/**
* Get a single RecommendationRequest by ID.
*
* @param id the ID of the RecommendationRequest
* @return the RecommendationRequest with the given ID
*/
@Operation(summary = "Get a single recommendation request by ID")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public RecommendationRequest getById(@Parameter(name = "id") @RequestParam Long id) {
return recommendationRequestRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
}
/**
* Create a new recommendation request.
*
* @param requesterEmail email of the student asking for the letter
* @param professorEmail email of the professor writing the letter
* @param explanation what the letter is for
* @param dateRequested when the request was made
* @param dateNeeded when the letter is needed
* @param done whether the request has been fulfilled
* @return the saved RecommendationRequest
*/
@Operation(summary = "Create a new recommendation request")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/post")
public RecommendationRequest postRecommendationRequest(
@Parameter(name = "requesterEmail") @RequestParam String requesterEmail,
@Parameter(name = "professorEmail") @RequestParam String professorEmail,
@Parameter(name = "explanation") @RequestParam String explanation,
@Parameter(name = "dateRequested", description = "in iso format, e.g. 2022-01-03T00:00:00")
@RequestParam
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime dateRequested,
@Parameter(name = "dateNeeded", description = "in iso format, e.g. 2022-01-03T00:00:00")
@RequestParam
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime dateNeeded,
@Parameter(name = "done") @RequestParam boolean done) {
RecommendationRequest request = new RecommendationRequest();
request.setRequesterEmail(requesterEmail);
request.setProfessorEmail(professorEmail);
request.setExplanation(explanation);
request.setDateRequested(dateRequested);
request.setDateNeeded(dateNeeded);
request.setDone(done);
return recommendationRequestRepository.save(request);
}
/**
* Update a single RecommendationRequest.
*
* @param id the ID of the RecommendationRequest to update
* @param incoming the updated RecommendationRequest data
* @return the updated RecommendationRequest
*/
@Operation(summary = "Update a single recommendation request")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("")
public RecommendationRequest updateRecommendationRequest(
@Parameter(name = "id") @RequestParam Long id,
@RequestBody @Valid RecommendationRequest incoming) {
RecommendationRequest request =
recommendationRequestRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
request.setRequesterEmail(incoming.getRequesterEmail());
request.setProfessorEmail(incoming.getProfessorEmail());
request.setExplanation(incoming.getExplanation());
request.setDateRequested(incoming.getDateRequested());
request.setDateNeeded(incoming.getDateNeeded());
request.setDone(incoming.isDone());
recommendationRequestRepository.save(request);
return request;
}
/**
* Delete a RecommendationRequest.
*
* @param id the ID of the RecommendationRequest to delete
* @return a message indicating the RecommendationRequest was deleted
*/
@Operation(summary = "Delete a RecommendationRequest")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("")
public Object deleteRecommendationRequest(@Parameter(name = "id") @RequestParam Long id) {
RecommendationRequest request =
recommendationRequestRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(RecommendationRequest.class, id));
recommendationRequestRepository.delete(request);
return genericMessage("RecommendationRequest with id %s deleted".formatted(id));
}
}