MenuItemReviewController.java
package edu.ucsb.cs156.example.controllers;
import edu.ucsb.cs156.example.entities.MenuItemReview;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.MenuItemReviewRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Tag(name = "MenuItemReview")
@RequestMapping("/api/menuitemreview")
@RestController
@Slf4j
public class MenuItemReviewController extends ApiController {
@Autowired MenuItemReviewRepository menuItemReviewRepository;
@Operation(summary = "List all menu item reviews")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/all")
public Iterable<MenuItemReview> allMenuItemReviews() {
Iterable<MenuItemReview> reviews = menuItemReviewRepository.findAll();
return reviews;
}
@Operation(summary = "Create a new review")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/post")
public MenuItemReview postMenuItemReview(
@Parameter(name = "itemId") @RequestParam Long itemId,
@Parameter(name = "reviewerEmail") @RequestParam String reviewerEmail,
@Parameter(name = "stars") @RequestParam int stars,
@Parameter(
name = "dateReviewed",
description = "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS)")
@RequestParam("dateReviewed")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime dateReviewed,
@Parameter(name = "comments") @RequestParam String comments) {
log.info(
"postMenuItemReview: itemId={}, reviewerEmail={}, stars={}, dateReviewed={}, comments={}",
itemId,
reviewerEmail,
stars,
dateReviewed,
comments);
MenuItemReview review = new MenuItemReview();
review.setItemId(itemId);
review.setReviewerEmail(reviewerEmail);
review.setStars(stars);
review.setDateReviewed(dateReviewed);
review.setComments(comments);
MenuItemReview savedReview = menuItemReviewRepository.save(review);
return savedReview;
}
/**
* Get a single record from the table; use the value passed in as a @RequestParam to do a lookup
* by id. If a matching row is found, return the row as a JSON object, otherwise throw an
* EntityNotFoundException.
*
* @param id the id of the review
* @return a MenuItemReview
*/
@Operation(summary = "Get a single review")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public MenuItemReview getById(@Parameter(name = "id") @RequestParam Long id) {
MenuItemReview menuItemReview =
menuItemReviewRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));
return menuItemReview;
}
/**
* Get a single record from the table; use the value passed in as a @RequestParam to do a lookup
* by id. If a matching row is found, update that row with the values passed in as a JSON object.
* If a matching row is not found, throw an EntityNotFoundException.
*
* @param id id of the review to update
* @param incoming the new review
* @return the updated review object
*/
@Operation(summary = "Update a single review")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("")
public MenuItemReview updateMenuItemReview(
@Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid MenuItemReview incoming) {
MenuItemReview menuItemReview =
menuItemReviewRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));
menuItemReview.setItemId(incoming.getItemId());
menuItemReview.setReviewerEmail(incoming.getReviewerEmail());
menuItemReview.setStars(incoming.getStars());
menuItemReview.setDateReviewed(incoming.getDateReviewed());
menuItemReview.setComments(incoming.getComments());
menuItemReviewRepository.save(menuItemReview);
return menuItemReview;
}
/**
* Delete a review * @param id the id of the review to delete
*
* @return a message indicating the review was deleted
*/
@Operation(summary = "Delete a review")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("")
public Object deleteMenuItemReview(@Parameter(name = "id") @RequestParam Long id) {
MenuItemReview menuItemReview =
menuItemReviewRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(MenuItemReview.class, id));
menuItemReviewRepository.delete(menuItemReview);
return genericMessage("MenuItemReview with id %s deleted".formatted(id));
}
}