UCSBDiningCommonsController.java

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

  2. import edu.ucsb.cs156.example.entities.UCSBDiningCommons;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.UCSBDiningCommonsRepository;

  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 UCSBDiningCommons
  22.  */

  23. @Tag(name = "UCSBDiningCommons")
  24. @RequestMapping("/api/ucsbdiningcommons")
  25. @RestController
  26. @Slf4j
  27. public class UCSBDiningCommonsController extends ApiController {

  28.     @Autowired
  29.     UCSBDiningCommonsRepository ucsbDiningCommonsRepository;

  30.     /**
  31.      * THis method returns a list of all ucsbdiningcommons.
  32.      * @return a list of all ucsbdiningcommons
  33.      */
  34.     @Operation(summary= "List all ucsb dining commons")
  35.     @PreAuthorize("hasRole('ROLE_USER')")
  36.     @GetMapping("/all")
  37.     public Iterable<UCSBDiningCommons> allCommonss() {
  38.         Iterable<UCSBDiningCommons> commons = ucsbDiningCommonsRepository.findAll();
  39.         return commons;
  40.     }

  41.     /**
  42.      * This method returns a single diningcommons.
  43.      * @param code code of the diningcommons
  44.      * @return a single diningcommons
  45.      */
  46.     @Operation(summary= "Get a single commons")
  47.     @PreAuthorize("hasRole('ROLE_USER')")
  48.     @GetMapping("")
  49.     public UCSBDiningCommons getById(
  50.             @Parameter(name="code") @RequestParam String code) {
  51.         UCSBDiningCommons commons = ucsbDiningCommonsRepository.findById(code)
  52.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommons.class, code));

  53.         return commons;
  54.     }

  55.     /**
  56.      * This method creates a new diningcommons. Accessible only to users with the role "ROLE_ADMIN".
  57.      * @param code code of the diningcommons
  58.      * @param name name of the diningcommons
  59.      * @param hasSackMeal whether or not the commons has sack meals
  60.      * @param hasTakeOutMeal whether or not the commons has take out meals
  61.      * @param hasDiningCam whether or not the commons has a dining cam
  62.      * @param latitude latitude of the commons
  63.      * @param longitude logitude of the commons
  64.      * @return the save diningcommons
  65.      */
  66.     @Operation(summary= "Create a new commons")
  67.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  68.     @PostMapping("/post")
  69.     public UCSBDiningCommons postCommons(
  70.         @Parameter(name="code") @RequestParam String code,
  71.         @Parameter(name="name") @RequestParam String name,
  72.         @Parameter(name="hasSackMeal") @RequestParam boolean hasSackMeal,
  73.         @Parameter(name="hasTakeOutMeal") @RequestParam boolean hasTakeOutMeal,
  74.         @Parameter(name="hasDiningCam") @RequestParam boolean hasDiningCam,
  75.         @Parameter(name="latitude") @RequestParam double latitude,
  76.         @Parameter(name="longitude") @RequestParam double longitude
  77.         )
  78.         {

  79.         UCSBDiningCommons commons = new UCSBDiningCommons();
  80.         commons.setCode(code);
  81.         commons.setName(name);
  82.         commons.setHasSackMeal(hasSackMeal);
  83.         commons.setHasTakeOutMeal(hasTakeOutMeal);
  84.         commons.setHasDiningCam(hasDiningCam);
  85.         commons.setLatitude(latitude);
  86.         commons.setLongitude(longitude);

  87.         UCSBDiningCommons savedCommons = ucsbDiningCommonsRepository.save(commons);

  88.         return savedCommons;
  89.     }

  90.     /**
  91.      * Delete a diningcommons. Accessible only to users with the role "ROLE_ADMIN".
  92.      * @param code code of the commons
  93.      * @return a message indiciating the commons was deleted
  94.      */
  95.     @Operation(summary= "Delete a UCSBDiningCommons")
  96.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  97.     @DeleteMapping("")
  98.     public Object deleteCommons(
  99.             @Parameter(name="code") @RequestParam String code) {
  100.         UCSBDiningCommons commons = ucsbDiningCommonsRepository.findById(code)
  101.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommons.class, code));

  102.         ucsbDiningCommonsRepository.delete(commons);
  103.         return genericMessage("UCSBDiningCommons with id %s deleted".formatted(code));
  104.     }

  105.     /**
  106.      * Update a single diningcommons. Accessible only to users with the role "ROLE_ADMIN".
  107.      * @param code code of the diningcommons
  108.      * @param incoming the new commons contents
  109.      * @return the updated commons object
  110.      */
  111.     @Operation(summary= "Update a single commons")
  112.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  113.     @PutMapping("")
  114.     public UCSBDiningCommons updateCommons(
  115.             @Parameter(name="code") @RequestParam String code,
  116.             @RequestBody @Valid UCSBDiningCommons incoming) {

  117.         UCSBDiningCommons commons = ucsbDiningCommonsRepository.findById(code)
  118.                 .orElseThrow(() -> new EntityNotFoundException(UCSBDiningCommons.class, code));


  119.         commons.setName(incoming.getName());  
  120.         commons.setHasSackMeal(incoming.getHasSackMeal());
  121.         commons.setHasTakeOutMeal(incoming.getHasTakeOutMeal());
  122.         commons.setHasDiningCam(incoming.getHasDiningCam());
  123.         commons.setLatitude(incoming.getLatitude());
  124.         commons.setLongitude(incoming.getLongitude());

  125.         ucsbDiningCommonsRepository.save(commons);

  126.         return commons;
  127.     }
  128. }