UCSBDiningCommonsMenuItemController.java
package edu.ucsb.cs156.example.controllers;
import com.fasterxml.jackson.core.JsonProcessingException;
import edu.ucsb.cs156.example.entities.UCSBDiningCommonsMenuItem;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsMenuItemRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Tag(name = "UCSBDiningCommonsMenuItems")
@RequestMapping("/api/ucsb-dining-commons-menu-items")
@RestController
@Slf4j
public class UCSBDiningCommonsMenuItemController extends ApiController {
@Autowired UCSBDiningCommonsMenuItemRepository ucsbDiningCommonsMenuItemRepository;
@Operation(summary = "Get all UCSB Dining Commons Menu Items")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/all")
public Iterable<UCSBDiningCommonsMenuItem> allUCSBDiningCommonsMenuItems() {
Iterable<UCSBDiningCommonsMenuItem> items = ucsbDiningCommonsMenuItemRepository.findAll();
return items;
}
@Operation(summary = "Get a single UCSB Dining Commons Menu Item by id")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public UCSBDiningCommonsMenuItem getUCSBDiningCommonsMenuItemById(
@Parameter(name = "id") @RequestParam Long id) {
UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem =
ucsbDiningCommonsMenuItemRepository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
return ucsbDiningCommonsMenuItem;
}
@Operation(summary = "Create a new UCSB Dining Commons Menu Item")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/post")
public UCSBDiningCommonsMenuItem postUCSBDiningCommonsMenuItem(
@Parameter(name = "name") @RequestParam String name,
@Parameter(name = "diningCommonsCode") @RequestParam String diningCommonsCode,
@Parameter(name = "station") @RequestParam String station)
throws JsonProcessingException {
UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = new UCSBDiningCommonsMenuItem();
ucsbDiningCommonsMenuItem.setName(name);
ucsbDiningCommonsMenuItem.setDiningCommonsCode(diningCommonsCode);
ucsbDiningCommonsMenuItem.setStation(station);
UCSBDiningCommonsMenuItem savedUcsbDiningCommonsMenuItem =
ucsbDiningCommonsMenuItemRepository.save(ucsbDiningCommonsMenuItem);
return savedUcsbDiningCommonsMenuItem;
}
}