HelpRequestController.java

  1. package edu.ucsb.cs156.example.controllers;

  2. import edu.ucsb.cs156.example.entities.HelpRequest;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.HelpRequestRepository;
  5. import edu.ucsb.cs156.example.repositories.UCSBDateRepository;

  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.Parameter;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import lombok.extern.slf4j.Slf4j;

  10. import com.fasterxml.jackson.core.JsonProcessingException;

  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.format.annotation.DateTimeFormat;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.PutMapping;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.RestController;

  22. import jakarta.validation.Valid;

  23. import java.time.LocalDateTime;

  24. @Tag(name = "HelpRequest")
  25. @RequestMapping("/api/helprequest")
  26. @RestController
  27. @Slf4j

  28. public class HelpRequestController extends ApiController {

  29.   @Autowired
  30.   HelpRequestRepository helpRequestRepository;

  31.   /**
  32.      * List all HelpRequests
  33.      *
  34.      * @return an iterable of HelpRequest
  35.      */
  36.     @Operation(summary= "List all HelpRequests")
  37.     @PreAuthorize("hasRole('ROLE_USER')")
  38.     @GetMapping("/all")
  39.     public Iterable<HelpRequest> allHelpRequests() {
  40.         Iterable<HelpRequest> requests = helpRequestRepository.findAll();
  41.         return requests;
  42.     }

  43.     /**
  44.      * Post a new HelpRequest
  45.      *
  46.      * @param requesterEmail
  47.      * @param teamId
  48.      * @param tableOrBreakoutRoom
  49.      * @param requestTime
  50.      * @param explanation
  51.      * @param solved
  52.      * @return the new HelpRequest
  53.      */
  54.     @Operation(summary= "Create a new HelpRequest")
  55.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  56.     @PostMapping("/post")
  57.     public HelpRequest postMenuItemReview(
  58.             @Parameter(name="requesterEmail") @RequestParam String requesterEmail,
  59.             @Parameter(name="teamId") @RequestParam String teamId,
  60.             @Parameter(name="tableOrBreakoutRoom") @RequestParam String tableOrBreakoutRoom,
  61.             @Parameter(name="requestTime", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("requestTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime requestTime,
  62.             @Parameter(name="explanation") @RequestParam String explanation,
  63.             @Parameter(name="solved") @RequestParam boolean solved)
  64.             throws JsonProcessingException {

  65.               // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
  66.               // See: https://www.baeldung.com/spring-date-parameters
  67.      
  68.               log.info("requestTime={}", requestTime);
  69.      
  70.               HelpRequest helpRequest = new HelpRequest();
  71.               helpRequest.setRequesterEmail(requesterEmail);
  72.               helpRequest.setTeamId(teamId);
  73.               helpRequest.setTableOrBreakoutRoom(tableOrBreakoutRoom);
  74.               helpRequest.setRequestTime(requestTime);
  75.               helpRequest.setExplanation(explanation);
  76.               helpRequest.setSolved(solved);
  77.               HelpRequest savedMenuItemReview = helpRequestRepository.save(helpRequest);
  78.               return savedMenuItemReview;
  79.           }
  80.          
  81.    
  82.            /** Get a single request by id
  83.            *
  84.            * @param id the id of the review
  85.            * @return a HelpRequest object
  86.            */
  87.            @Operation(summary= "Get a single request")
  88.            @PreAuthorize("hasRole('ROLE_USER')")
  89.            @GetMapping("")
  90.            public HelpRequest getById(
  91.                    @Parameter(name="id") @RequestParam Long id) {
  92.                HelpRequest helpRequest = helpRequestRepository.findById(id)
  93.                        .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));

  94.                return helpRequest;
  95.            }
  96.                
  97.              /**
  98.             * Delete a HelpRequest
  99.             *
  100.             * @param id         the id of the review to delete
  101.             * @return           a message indicating the review was deleted
  102.              */
  103.              @Operation(summary= "Delete a HelpRequest")
  104.              @PreAuthorize("hasRole('ROLE_ADMIN')")
  105.              @DeleteMapping("")
  106.              public Object deleteHelpRequest(
  107.                    @Parameter(name="id") @RequestParam Long id) {
  108.                HelpRequest helpRequest = helpRequestRepository.findById(id)
  109.                        .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));
  110.                
  111.                helpRequestRepository.delete(helpRequest);
  112.                return genericMessage("HelpRequest with id %s deleted".formatted(id));
  113.              }
  114.                  
  115.                
  116.              /**
  117.              * Update a single review
  118.              *
  119.              * @param id            id of the review to update
  120.              * @param incoming      the new review
  121.              * @return the updated review object
  122.              */
  123.              @Operation(summary= "Update a single request")
  124.              @PreAuthorize("hasRole('ROLE_ADMIN')")
  125.              @PutMapping("")
  126.              public HelpRequest updateMenuItemReview(
  127.                @Parameter(name="id") @RequestParam Long id,
  128.                @RequestBody @Valid HelpRequest incoming) {

  129.                HelpRequest helpRequest = helpRequestRepository.findById(id)
  130.                        .orElseThrow(() -> new EntityNotFoundException(HelpRequest.class, id));

  131.                   helpRequest.setRequesterEmail(incoming.getRequesterEmail());
  132.                   helpRequest.setTeamId(incoming.getTeamId());
  133.                   helpRequest.setTableOrBreakoutRoom(incoming.getTableOrBreakoutRoom());
  134.                   helpRequest.setRequestTime(incoming.getRequestTime());
  135.                   helpRequest.setExplanation(incoming.getExplanation());
  136.                   helpRequest.setSolved(incoming.getSolved());

  137.                helpRequestRepository.save(helpRequest);
  138.                return helpRequest;
  139.      }
  140. }