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.checkerframework.checker.units.qual.m;
  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. /**
  26.  * This is a REST controller for MenuItemReview
  27.  */

  28. @Tag(name = "MenuItemReview")
  29. @RequestMapping("/api/menu_item_review")
  30. @RestController
  31. @Slf4j

  32. public class MenuItemReviewController extends ApiController {
  33.    
  34.     @Autowired
  35.     MenuItemReviewRepository menuItemReviewRepository;

  36.     /**
  37.      * List all MeuItemReviews
  38.      *
  39.      * @return an iterable of MenuItemReview
  40.      */

  41.     @Operation(summary= "List all menu item reviews")
  42.     @PreAuthorize("hasRole('ROLE_USER')")
  43.     @GetMapping("/all")
  44.     public Iterable<MenuItemReview> allMenuReviews() {
  45.         Iterable<MenuItemReview> menuItemReviews = menuItemReviewRepository.findAll();
  46.         return menuItemReviews;
  47.     }

  48.     /**
  49.      * Get a single date by id
  50.      *
  51.      * @param id the id of the date
  52.      * @return a MenuItemReview
  53.      */


  54.     @Operation(summary= "Look up by id")
  55.     @PreAuthorize("hasRole('ROLE_USER')")
  56.     @GetMapping("")
  57.     public MenuItemReview getById(
  58.             @Parameter(name="id") @RequestParam Long id) {
  59.         MenuItemReview review = menuItemReviewRepository.findById(id)
  60.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));

  61.         return review;
  62.     }

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

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

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

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

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

  93.         return savedMenuItemReview;
  94.     }


  95.     /**
  96.      * Delete a MenuItemReview
  97.      *
  98.      * @param id the id of the date to delete
  99.      * @return a message indicating the date was deleted
  100.      */
  101.     @Operation(summary= "Delete a MenuItemReview")
  102.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  103.     @DeleteMapping("")
  104.     public Object deleteMenuItem(
  105.             @Parameter(name="id") @RequestParam Long id) {
  106.         MenuItemReview menuItemReview = menuItemReviewRepository.findById(id)
  107.                 .orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));

  108.         menuItemReviewRepository.delete(menuItemReview);
  109.         return genericMessage("MenuItemReview with id %s deleted".formatted(id));
  110.     }

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

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

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

  132.         menuItemReviewRepository.save(menuItemReview);

  133.         return menuItemReview;
  134.     }
  135. }