UCSBDatesController.java

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

  2. import edu.ucsb.cs156.example.entities.UCSBDate;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.UCSBDateRepository;

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

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

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

  21. import jakarta.validation.Valid;

  22. import java.time.LocalDateTime;

  23. /**
  24.  * This is a REST controller for UCSBDates
  25.  */

  26. @Tag(name = "UCSBDates")
  27. @RequestMapping("/api/ucsbdates")
  28. @RestController
  29. @Slf4j
  30. public class UCSBDatesController extends ApiController {

  31.     @Autowired
  32.     UCSBDateRepository ucsbDateRepository;

  33.     /**
  34.      * List all UCSB dates
  35.      *
  36.      * @return an iterable of UCSBDate
  37.      */
  38.     @Operation(summary= "List all ucsb dates")
  39.     @PreAuthorize("hasRole('ROLE_USER')")
  40.     @GetMapping("/all")
  41.     public Iterable<UCSBDate> allUCSBDates() {
  42.         Iterable<UCSBDate> dates = ucsbDateRepository.findAll();
  43.         return dates;
  44.     }

  45.     /**
  46.      * Get a single date by id
  47.      *
  48.      * @param id the id of the date
  49.      * @return a UCSBDate
  50.      */
  51.     @Operation(summary= "Get a single date")
  52.     @PreAuthorize("hasRole('ROLE_USER')")
  53.     @GetMapping("")
  54.     public UCSBDate getById(
  55.             @Parameter(name="id") @RequestParam Long id) {
  56.         UCSBDate ucsbDate = ucsbDateRepository.findById(id)
  57.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDate.class, id));

  58.         return ucsbDate;
  59.     }

  60.     /**
  61.      * Create a new date
  62.      *
  63.      * @param quarterYYYYQ  the quarter in the format YYYYQ
  64.      * @param name          the name of the date
  65.      * @param localDateTime the date
  66.      * @return the saved ucsbdate
  67.      */
  68.     @Operation(summary= "Create a new date")
  69.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  70.     @PostMapping("/post")
  71.     public UCSBDate postUCSBDate(
  72.             @Parameter(name="quarterYYYYQ") @RequestParam String quarterYYYYQ,
  73.             @Parameter(name="name") @RequestParam String name,
  74.             @Parameter(name="localDateTime", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("localDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localDateTime)
  75.             throws JsonProcessingException {

  76.         // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
  77.         // See: https://www.baeldung.com/spring-date-parameters

  78.         log.info("localDateTime={}", localDateTime);

  79.         UCSBDate ucsbDate = new UCSBDate();
  80.         ucsbDate.setQuarterYYYYQ(quarterYYYYQ);
  81.         ucsbDate.setName(name);
  82.         ucsbDate.setLocalDateTime(localDateTime);

  83.         UCSBDate savedUcsbDate = ucsbDateRepository.save(ucsbDate);

  84.         return savedUcsbDate;
  85.     }

  86.     /**
  87.      * Delete a UCSBDate
  88.      *
  89.      * @param id the id of the date to delete
  90.      * @return a message indicating the date was deleted
  91.      */
  92.     @Operation(summary= "Delete a UCSBDate")
  93.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  94.     @DeleteMapping("")
  95.     public Object deleteUCSBDate(
  96.             @Parameter(name="id") @RequestParam Long id) {
  97.         UCSBDate ucsbDate = ucsbDateRepository.findById(id)
  98.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDate.class, id));

  99.         ucsbDateRepository.delete(ucsbDate);
  100.         return genericMessage("UCSBDate with id %s deleted".formatted(id));
  101.     }

  102.     /**
  103.      * Update a single date
  104.      *
  105.      * @param id       id of the date to update
  106.      * @param incoming the new date
  107.      * @return the updated date object
  108.      */
  109.     @Operation(summary= "Update a single date")
  110.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  111.     @PutMapping("")
  112.     public UCSBDate updateUCSBDate(
  113.             @Parameter(name="id") @RequestParam Long id,
  114.             @RequestBody @Valid UCSBDate incoming) {

  115.         UCSBDate ucsbDate = ucsbDateRepository.findById(id)
  116.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDate.class, id));

  117.         ucsbDate.setQuarterYYYYQ(incoming.getQuarterYYYYQ());
  118.         ucsbDate.setName(incoming.getName());
  119.         ucsbDate.setLocalDateTime(incoming.getLocalDateTime());

  120.         ucsbDateRepository.save(ucsbDate);

  121.         return ucsbDate;
  122.     }
  123. }