MenuItemReviewController.java

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

  2. import edu.ucsb.cs156.example.entities.MenuItemReview;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.MenuItemReviewRepository;

  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. @Tag(name = "MenuItemReview")
  24. @RequestMapping("/api/menuitemreview")
  25. @RestController
  26. @Slf4j
  27. public class MenuItemReviewController  extends ApiController {
  28.     @Autowired
  29.     MenuItemReviewRepository menuItemReviewRepository;

  30.     @Operation(summary= "List all menu item reviews")
  31.     @PreAuthorize("hasRole('ROLE_USER')")
  32.     @GetMapping("/all")
  33.     public Iterable<MenuItemReview> allMenuItemReviews() {
  34.         Iterable<MenuItemReview> reviews = menuItemReviewRepository.findAll();
  35.         return reviews;
  36.     }

  37.     /**
  38.      * Get a single MenuItemReview by id
  39.      *
  40.      * @param id the id of the review
  41.      * @return a MenuItemReview
  42.      */
  43.     @Operation(summary= "Get a single menu item review")
  44.     @PreAuthorize("hasRole('ROLE_USER')")
  45.     @GetMapping("")
  46.     public MenuItemReview getById(
  47.             @Parameter(name="id") @RequestParam Long id) {
  48.         MenuItemReview menuItemReview = menuItemReviewRepository.findById(id)
  49.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));

  50.         return menuItemReview;
  51.     }

  52.     /**
  53.      * Create a new MenuItemReview
  54.      *
  55.      * @param itemId  the id of the item to be reviewed
  56.      * @param reviewerEmail  the email of the reviewer
  57.      * @param stars  the number of stars given in the review
  58.      * @param comments  the comments of the review
  59.      * @param dateReviewed the date
  60.      * @return the saved ucsbdate
  61.      */
  62.     @Operation(summary= "Create a new review")
  63.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  64.     @PostMapping("/post")
  65.     public MenuItemReview postMenuItemReview(
  66.             // @Parameter(name="quarterYYYYQ") @RequestParam String quarterYYYYQ,
  67.             // @Parameter(name="name") @RequestParam String name,
  68.             @Parameter(name="itemId") @RequestParam Long itemId,
  69.             @Parameter(name="reviewerEmail") @RequestParam String reviewerEmail,
  70.             @Parameter(name="stars") @RequestParam int stars,
  71.             @Parameter(name="comments") @RequestParam String comments,
  72.             @Parameter(name="dateReviewed", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("dateReviewed") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateReviewed)
  73.             throws JsonProcessingException {

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

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

  77.         MenuItemReview menuItemReview = new MenuItemReview();
  78.         menuItemReview.setItemId(itemId);
  79.         menuItemReview.setReviewerEmail(reviewerEmail);
  80.         menuItemReview.setStars(stars);
  81.         menuItemReview.setComments(comments);
  82.         menuItemReview.setDateReviewed(dateReviewed);

  83.         MenuItemReview savedMenuItemReview = menuItemReviewRepository.save(menuItemReview);

  84.         return savedMenuItemReview;
  85.     }

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

  99.                 menuItemReviewRepository.delete(menuItemReview);
  100.         return genericMessage("MenuItemReview with id %s deleted".formatted(id));
  101.     }

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

  115.         MenuItemReview menuItemReview = menuItemReviewRepository.findById(id)
  116.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));

  117.         menuItemReview.setItemId(incoming.getItemId());
  118.         menuItemReview.setReviewerEmail(incoming.getReviewerEmail());
  119.         menuItemReview.setStars(incoming.getStars());
  120.         menuItemReview.setComments(incoming.getComments());
  121.         menuItemReview.setDateReviewed(incoming.getDateReviewed());

  122.         menuItemReviewRepository.save(menuItemReview);

  123.         return menuItemReview;
  124.     }
  125. }