MenuItemReviewController.java

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

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

  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.hibernate.engine.internal.Collections;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.format.annotation.DateTimeFormat;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  15. import org.springframework.web.bind.annotation.DeleteMapping;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.PutMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;

  23. import jakarta.validation.Valid;

  24. import java.time.LocalDateTime;
  25. import edu.ucsb.cs156.example.entities.MenuItemReview;

  26. /**
  27.  * This is a REST controller for MenuItemReview
  28.  */

  29. @Tag(name = "MenuItemReview")
  30. @RequestMapping("/api/menuitemreview")
  31. @RestController
  32. @Slf4j
  33. public class MenuItemReviewController extends ApiController {
  34.    
  35.     @Autowired
  36.     MenuItemReviewRepository menuItemReviewRepository;

  37.     /**
  38.      * List all MenuItemReviews
  39.      *
  40.      * @return an iterable of MenuItemReviews
  41.      */
  42.     @Operation(summary= "List all menu item reviews")
  43.     @PreAuthorize("hasRole('ROLE_USER')")
  44.     @GetMapping("/all")
  45.     public Iterable<MenuItemReview> allMenuItemReviews() {
  46.         Iterable<MenuItemReview> review = menuItemReviewRepository.findAll();
  47.         return review;
  48.     }

  49.     /**
  50.      * Get a single menuitemreview by id
  51.      *
  52.      * @param id the id of the menuitemreview
  53.      * @return a menuitemreview
  54.      */
  55.     @Operation(summary= "Get a single menuitemreview")
  56.     @PreAuthorize("hasRole('ROLE_USER')")
  57.     @GetMapping("")
  58.     public MenuItemReview getById(
  59.             @Parameter(name="id") @RequestParam Long id) {
  60.         MenuItemReview menuItemReview = menuItemReviewRepository.findById(id)
  61.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));

  62.         return menuItemReview;
  63.     }

  64.     /**
  65.      * Create a new menu item review
  66.      *
  67.      * @param itemId        id of the menu item
  68.      * @param reviewerEmail email of the reviewer
  69.      * @param stars         the number of stars the item received
  70.      * @param dateReviewed    the date the review was made
  71.      * @param comments      comments of the review
  72.      * @return the saved menu item review
  73.      */
  74.     @Operation(summary= "Create a new menu item review")
  75.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  76.     @PostMapping("/post")
  77.     public MenuItemReview postMenuItemReview(
  78.             @Parameter(name="itemId") @RequestParam long itemId,
  79.             @Parameter(name="reviewerEmail") @RequestParam String reviewerEmail,
  80.             @Parameter(name="stars") @RequestParam int stars,
  81.             @Parameter(name="comments") @RequestParam String comments,
  82.             @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)
  83.             throws JsonProcessingException {

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

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

  87.         MenuItemReview menuItemReview = new MenuItemReview();
  88.         menuItemReview.setItemId(itemId);
  89.         menuItemReview.setReviewerEmail(reviewerEmail);
  90.         menuItemReview.setStars(stars);
  91.         menuItemReview.setDateReviewed(dateReviewed);
  92.         menuItemReview.setComments(comments);

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

  94.         return savedMenuItemReview;
  95.     }

  96.     /**
  97.      * Delete a menuitemreview
  98.      *
  99.      * @param id the id of the menuitemreview to delete
  100.      * @return a message indicating the menuitemreview was deleted
  101.      */
  102.     @Operation(summary= "Delete a menuitemreview")
  103.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  104.     @DeleteMapping("")
  105.     public Object deleteMenuItemReview(
  106.             @Parameter(name="id") @RequestParam Long id) {
  107.                 MenuItemReview menuItemReview = menuItemReviewRepository.findById(id)
  108.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));
  109.         menuItemReviewRepository.delete(menuItemReview);
  110.         return genericMessage("menuitemreview with id %s deleted".formatted(id));
  111.     }


  112.     /**
  113.      * Update a menu item review
  114.      *
  115.      * @param id       id of the menuitemreview to update
  116.      * @param incoming the new menuitemreview
  117.      * @return the updated menuitemreview object
  118.      */
  119.     @Operation(summary= "Update a single menuitemreview")
  120.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  121.     @PutMapping("")
  122.     public MenuItemReview updateMenuItemReview(
  123.             @Parameter(name="id") @RequestParam Long id,
  124.             @RequestBody @Valid MenuItemReview incoming) {

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

  127.                 menuItemReview.setItemId(incoming.getItemId());
  128.                 menuItemReview.setReviewerEmail(incoming.getReviewerEmail());
  129.                 menuItemReview.setStars(incoming.getStars());
  130.                 menuItemReview.setDateReviewed(incoming.getDateReviewed());
  131.                 menuItemReview.setComments(incoming.getComments());

  132.         menuItemReviewRepository.save(menuItemReview);

  133.         return menuItemReview;
  134.     }
  135. }