RestaurantsController.java

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

  2. import edu.ucsb.cs156.example.entities.Restaurant;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.RestaurantRepository;
  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 org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.security.access.prepost.PreAuthorize;
  11. import org.springframework.web.bind.annotation.DeleteMapping;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.PutMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.RestController;

  19. import jakarta.validation.Valid;

  20. /**
  21.  * This is a REST controller for Restaurants
  22.  */

  23. @Tag(name = "Restaurants")
  24. @RequestMapping("/api/restaurants")
  25. @RestController
  26. public class RestaurantsController extends ApiController {

  27.     @Autowired
  28.     RestaurantRepository restaurantRepository;

  29.     /**
  30.      * This method returns a list of all restaurants.
  31.      * @return a list of all restaurants
  32.      */
  33.     @Operation(summary = "List all restaurants")
  34.     @PreAuthorize("hasRole('ROLE_USER')")
  35.     @GetMapping("/all")
  36.     public Iterable<Restaurant> allRestaurants() {
  37.         Iterable<Restaurant> restaurants = restaurantRepository.findAll();
  38.         return restaurants;
  39.     }

  40.     /**
  41.      * This method returns a single restaurant.
  42.      * @param id id of the restaurant to get
  43.      * @return a single restaurant
  44.      */
  45.     @Operation(summary = "Get a single restaurant")
  46.     @PreAuthorize("hasRole('ROLE_USER')")
  47.     @GetMapping("")
  48.     public Restaurant getById(
  49.             @Parameter(name = "id") @RequestParam Long id) {
  50.         Restaurant restaurant = restaurantRepository.findById(id)
  51.                 .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));

  52.         return restaurant;
  53.     }

  54.     /**
  55.      * This method creates a new restaurant. Accessible only to users with the role "ROLE_ADMIN".
  56.      * @param name name of the restaurant
  57.      * @param description description of the restaurant
  58.      * @return the save restaurant (with it's id field set by the database)
  59.      */
  60.     @Operation(summary = "Create a new restaurant")
  61.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  62.     @PostMapping("/post")
  63.     public Restaurant postRestaurant(
  64.             @Parameter(name = "name") @RequestParam String name,
  65.             @Parameter(name = "description") @RequestParam String description) {
  66.         Restaurant restaurant = new Restaurant();

  67.         restaurant.setName(name);
  68.         restaurant.setDescription(description);
  69.        
  70.         Restaurant savedrestaurant = restaurantRepository.save(restaurant);
  71.         return savedrestaurant;
  72.     }

  73.     /**
  74.      * Deletes a restaurant. Accessible only to users with the role "ROLE_ADMIN".
  75.      * @param id id of the restaurant to delete
  76.      * @return a message indicating that the restaurant was deleted
  77.      */
  78.     @Operation(summary = "Delete a Restaurant")
  79.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  80.     @DeleteMapping("")
  81.     public Object deleteRestaurant(
  82.             @Parameter(name = "id") @RequestParam Long id) {
  83.         Restaurant restaurant = restaurantRepository.findById(id)
  84.                 .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));

  85.         restaurantRepository.delete(restaurant);
  86.         return genericMessage("Restaurant with id %s deleted".formatted(id));
  87.     }

  88.     /**
  89.      * Update a single restaurant. Accessible only to users with the role "ROLE_ADMIN".
  90.      * @param id id of the restaurant to update
  91.      * @param incoming the new restaurant contents
  92.      * @return the updated restaurant object
  93.      */
  94.     @Operation(summary = "Update a single restaurant")
  95.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  96.     @PutMapping("")
  97.     public Restaurant updateRestaurant(
  98.             @Parameter(name = "id") @RequestParam Long id,
  99.             @RequestBody @Valid Restaurant incoming) {

  100.         Restaurant restaurant = restaurantRepository.findById(id)
  101.                 .orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id));

  102.         restaurant.setName(incoming.getName());
  103.         restaurant.setDescription(incoming.getDescription());

  104.         restaurantRepository.save(restaurant);

  105.         return restaurant;
  106.     }
  107. }