UCSBDiningCommonsMenuItemController.java

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

  2. import edu.ucsb.cs156.example.entities.UCSBDate;
  3. import edu.ucsb.cs156.example.entities.UCSBDiningCommonsMenuItem;
  4. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  5. import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsMenuItemRepository;

  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.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.format.annotation.DateTimeFormat;
  13. import org.springframework.security.access.prepost.PreAuthorize;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.PutMapping;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.RestController;

  22. import jakarta.validation.Valid;

  23. import java.time.LocalDateTime;


  24. @Tag(name = "UCSBDiningCommonsMenuItem")
  25. @RequestMapping("/api/ucsbdiningcommonsmenuitem")
  26. @RestController
  27. @Slf4j
  28. public class UCSBDiningCommonsMenuItemController extends ApiController {
  29.     @Autowired
  30.     UCSBDiningCommonsMenuItemRepository ucsbDiningCommonsMenuItemRepository;

  31.     // Get all records in the table and return as a JSON array
  32.     @Operation(summary= "List all ucsb dinging commons menu item")
  33.     @PreAuthorize("hasRole('ROLE_USER')")
  34.     @GetMapping("/all")
  35.     public Iterable<UCSBDiningCommonsMenuItem> allUCSBDates() {
  36.         Iterable<UCSBDiningCommonsMenuItem> items = ucsbDiningCommonsMenuItemRepository.findAll();
  37.         return items;
  38.     }

  39.     // Use the data in the input parameters to create a new row in the table and return the data as JSON
  40.     @Operation(summary= "Create a new date")
  41.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  42.     @PostMapping("/post")
  43.     public UCSBDiningCommonsMenuItem postUCSBDiningCommonsMenuItem(
  44.             @Parameter(name="diningCommonsCode") @RequestParam String diningCommonsCode,
  45.             @Parameter(name="name") @RequestParam String name,
  46.             @Parameter(name="station") @RequestParam String station)
  47.             throws JsonProcessingException {


  48.         UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = new UCSBDiningCommonsMenuItem();
  49.         ucsbDiningCommonsMenuItem.setDiningCommonsCode(diningCommonsCode);
  50.         ucsbDiningCommonsMenuItem.setName(name);
  51.         ucsbDiningCommonsMenuItem.setStation(station);

  52.         UCSBDiningCommonsMenuItem savedUCSBDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository.save(ucsbDiningCommonsMenuItem);

  53.         return savedUCSBDiningCommonsMenuItem;
  54.     }
  55.    
  56.     /**
  57.      * Get a single date by id
  58.      *
  59.      * @param id the id of the date
  60.      * @return a UCSBDate
  61.      */
  62.     @Operation(summary= "Get a single Dining Commons MenuItem by id")
  63.     @PreAuthorize("hasRole('ROLE_USER')")
  64.     @GetMapping("")
  65.     public UCSBDiningCommonsMenuItem getById(
  66.             @Parameter(name="id") @RequestParam Long id) {
  67.             UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository.findById(id)
  68.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

  69.         return ucsbDiningCommonsMenuItem;
  70.     }
  71.    

  72.     /**
  73.      * Update a single date
  74.      *
  75.      * @param id       id of the date to update
  76.      * @param incoming the new date
  77.      * @return the updated date object
  78.      */
  79.     @Operation(summary= "Update a single menu item")
  80.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  81.     @PutMapping("")
  82.     public UCSBDiningCommonsMenuItem updaDiningCommonsMenuItem(
  83.             @Parameter(name="id") @RequestParam Long id,
  84.             @RequestBody @Valid UCSBDiningCommonsMenuItem incoming) {

  85.             UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository.findById(id)
  86.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

  87.             ucsbDiningCommonsMenuItem.setDiningCommonsCode(incoming.getDiningCommonsCode());
  88.             ucsbDiningCommonsMenuItem.setName(incoming.getName());
  89.             ucsbDiningCommonsMenuItem.setStation(incoming.getStation());

  90.             ucsbDiningCommonsMenuItemRepository.save(ucsbDiningCommonsMenuItem);

  91.         return ucsbDiningCommonsMenuItem;
  92.     }


  93.     /**
  94.      * Delete a UCSBDate
  95.      *
  96.      * @param id the id of the menu item to delete
  97.      * @return a message indicating the menu item was deleted
  98.      */
  99.     @Operation(summary= "Delete a UCSBDiningCommonsMenuItem")
  100.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  101.     @DeleteMapping("")
  102.     public Object deleteUCSBDiningCommonsMenuItem(
  103.             @Parameter(name="id") @RequestParam Long id) {
  104.             UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository.findById(id)
  105.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

  106.         ucsbDiningCommonsMenuItemRepository.delete(ucsbDiningCommonsMenuItem);
  107.         return genericMessage("UCSBDiningCommonsMenuItem with id %s deleted".formatted(id));
  108.     }
  109. }