UCSBDiningCommonsMenuItemsController.java

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

  2. import edu.ucsb.cs156.example.entities.UCSBDiningCommons;
  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. /**
  25.  * This is a REST controller for UCSBDiningCommonsMenuItem
  26.  */

  27. @Tag(name = "UCSBDiningCommonsMenuItems")
  28. @RequestMapping("/api/ucsbdiningcommonsmenuitems")
  29. @RestController
  30. @Slf4j
  31. public class UCSBDiningCommonsMenuItemsController extends ApiController {

  32.     @Autowired
  33.     UCSBDiningCommonsMenuItemRepository ucsbDiningCommonsMenuItemRepository;

  34.     /**
  35.      * List all UCSB Dining Commons Menu Items
  36.      *
  37.      * @return an iterable of UCSBDiningCommonsMenuItem
  38.      */
  39.     @Operation(summary= "List all ucsb dining commons menu items")
  40.     @PreAuthorize("hasRole('ROLE_USER')")
  41.     @GetMapping("/all")
  42.     public Iterable<UCSBDiningCommonsMenuItem> allUCSBDiningCommonsMenuItems() {
  43.         Iterable<UCSBDiningCommonsMenuItem> ucsbDiningCommonsMenuItems = ucsbDiningCommonsMenuItemRepository.findAll();
  44.         return ucsbDiningCommonsMenuItems;
  45.     }

  46.     /**
  47.      * Get a single UCSBDiningCommonsMenuItem via ID
  48.      *  
  49.      * @param id the id of the UCSBDiningCommonsMenuItem
  50.      * @return a single UCSBDiningCommonsMenuItem
  51.      * @throws EntityNotFoundException if the UCSBDiningCommonsMenuItem is not found
  52.      */
  53.     @Operation(summary= "Get a single ucsb dining commons menu item")  
  54.     @PreAuthorize("hasRole('ROLE_USER')")
  55.     @GetMapping("")
  56.     public UCSBDiningCommonsMenuItem getById(
  57.             @Parameter(name="id") @RequestParam Long id)
  58.     throws EntityNotFoundException {
  59.         UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository
  60.                 .findById(id)
  61.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
  62.         return ucsbDiningCommonsMenuItem;
  63.     }

  64.     /**
  65.      * Update a single UCSBDiningCommonsMenuItem via diningCommonsCode
  66.      *
  67.      * @param id the id of the UCSBDiningCommonsMenuItem
  68.      * @param ucsbDiningCommonsMenuItem the new UCSBDiningCommonsMenuItem
  69.      * @return a single UCSBDiningCommonsMenuItem
  70.      */
  71.     @Operation(summary= "Update a single ucsb dining commons menu item")
  72.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  73.     @PutMapping("")
  74.     public UCSBDiningCommonsMenuItem updateUCSBDiningCommonsMenuItem(
  75.             @Parameter(name="id") @RequestParam Long id,
  76.             @Valid @RequestBody UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem) {

  77.         UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItemToUpdate = ucsbDiningCommonsMenuItemRepository
  78.                 .findById(id)
  79.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));

  80.         ucsbDiningCommonsMenuItemToUpdate.setDiningCommonsCode(ucsbDiningCommonsMenuItem.getDiningCommonsCode());
  81.         ucsbDiningCommonsMenuItemToUpdate.setName(ucsbDiningCommonsMenuItem.getName());
  82.         ucsbDiningCommonsMenuItemToUpdate.setStation(ucsbDiningCommonsMenuItem.getStation());
  83.         ucsbDiningCommonsMenuItemRepository.save(ucsbDiningCommonsMenuItemToUpdate);
  84.         return ucsbDiningCommonsMenuItemToUpdate;
  85.     }

  86.     /**
  87.      * Deletes a UCSBDiningCommonsMenuItem
  88.      *
  89.      * @param id the id of the UCSBDiningCommonsMenuItem
  90.      * @throws EntityNotFoundException if the UCSBDiningCommonsMenuItem is not found
  91.      */
  92.     @Operation(summary= "Delete a UCSBDiningCommonsMenuItem")
  93.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  94.     @DeleteMapping("")
  95.     public Object deleteById(
  96.             @Parameter(name="id") @RequestParam Long id)
  97.     throws EntityNotFoundException {
  98.         UCSBDiningCommonsMenuItem ucsbDiningCommonsMenuItem = ucsbDiningCommonsMenuItemRepository
  99.                 .findById(id)
  100.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommonsMenuItem.class, id));
  101.         ucsbDiningCommonsMenuItemRepository.delete(ucsbDiningCommonsMenuItem);
  102.         return genericMessage("UCSBDiningCommonsMenuItem with id %s deleted".formatted(id));
  103.     }

  104.     /**
  105.      * Create a new UCSBDiningCommonsMenuItem
  106.      *
  107.      * @param diningCommonsCode code of the UCSBDiningCommons
  108.      * @param name name of the UCSBDiningCommons
  109.      * @param station station of the UCSBDiningCommons
  110.      * @return the created UCSBDiningCommonsMenuItem
  111.      */
  112.     @Operation(summary= "Create a new UCSBDiningCommonsMenuItem")
  113.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  114.     @PostMapping("/post")
  115.     public UCSBDiningCommonsMenuItem postUCSBDiningCommonsMenuItem(
  116.             @Parameter(name="diningCommonsCode") @RequestParam String diningCommonsCode,
  117.             @Parameter(name="name") @RequestParam String name,
  118.             @Parameter(name="station") @RequestParam String station
  119.     ) throws JsonProcessingException{
  120.         UCSBDiningCommonsMenuItem newUcsbDiningCommonsMenuItem = new UCSBDiningCommonsMenuItem();
  121.         newUcsbDiningCommonsMenuItem.setDiningCommonsCode(diningCommonsCode);
  122.         newUcsbDiningCommonsMenuItem.setName(name);
  123.         newUcsbDiningCommonsMenuItem.setStation(station);

  124.         UCSBDiningCommonsMenuItem savedItems = ucsbDiningCommonsMenuItemRepository.save(newUcsbDiningCommonsMenuItem);

  125.         return savedItems;
  126.     }
  127. }