UCSBDiningCommonsMenuItemController.java
package edu.ucsb.cs156.example.controllers;
import edu.ucsb.cs156.example.entities.UCSBDate;
import edu.ucsb.cs156.example.entities.UCSBDiningCommonsMenuItems;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.UCSBDateRepository;
import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsMenuItemsRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import liquibase.pro.packaged.id;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.core.JsonProcessingException;
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;
import javax.validation.Valid;
import java.time.LocalDateTime;
@Tag(name = "UCSBDiningCommonsMenuItem")
@RequestMapping("/api/UCSBDiningCommonsMenuItem")
@RestController
@Slf4j
public class UCSBDiningCommonsMenuItemController extends ApiController {
@Autowired
UCSBDiningCommonsMenuItemsRepository ucsbDiningCommonsMenuItemsRepository;
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/all")
public Iterable<UCSBDiningCommonsMenuItems> allUCSBDates() {
Iterable<UCSBDiningCommonsMenuItems> dates = ucsbDiningCommonsMenuItemsRepository.findAll();
return dates;
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/post")
public UCSBDiningCommonsMenuItems postUCSBDiningCommonsMenuItems(
@Parameter(name="diningCommonsCode") @RequestParam String diningCommonsCode,
@Parameter(name="name") @RequestParam String name,
@Parameter(name="station") @RequestParam String station)
throws JsonProcessingException {
// For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
// See: https://www.baeldung.com/spring-date-parameters
log.info("station={}", station);
UCSBDiningCommonsMenuItems ucsbDiningCommonsMenuItems = new UCSBDiningCommonsMenuItems();
ucsbDiningCommonsMenuItems.setDiningCommonsCode(diningCommonsCode);
ucsbDiningCommonsMenuItems.setName(name);
ucsbDiningCommonsMenuItems.setStation(station);
UCSBDiningCommonsMenuItems savedUcsbDiningCommonsMenuItems = ucsbDiningCommonsMenuItemsRepository.save(ucsbDiningCommonsMenuItems);
return savedUcsbDiningCommonsMenuItems;
}
@Operation(summary= "Get a single menu item")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public UCSBDiningCommonsMenuItems getById(
@Parameter(name="id") @RequestParam Long id) {
UCSBDiningCommonsMenuItems ucsbDiningCommonsMenuItems = ucsbDiningCommonsMenuItemsRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItems.class, id));
return ucsbDiningCommonsMenuItems;
}
@Operation(summary= "Update a single menu")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("")
public UCSBDiningCommonsMenuItems updateUCSBDiningCommonsMenuItem(
@Parameter(name="id") @RequestParam Long id,
@RequestBody @Valid UCSBDiningCommonsMenuItems incoming) {
UCSBDiningCommonsMenuItems menuItem = ucsbDiningCommonsMenuItemsRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItems.class, id));
menuItem.setDiningCommonsCode(incoming.getDiningCommonsCode());
menuItem.setName(incoming.getName());
menuItem.setStation(incoming.getStation());
ucsbDiningCommonsMenuItemsRepository.save(menuItem);
return menuItem;
}
@Operation(summary= "Delete a UCSBDine")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("")
public Object deleteUCSBDate(
@Parameter(name="id") @RequestParam Long id) {
UCSBDiningCommonsMenuItems ucsbDate = ucsbDiningCommonsMenuItemsRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItems.class, id));
ucsbDiningCommonsMenuItemsRepository.delete(ucsbDate);
return genericMessage("UCSBDiningCommonsMenuItems with id 123 not found".formatted(id));
}
}