UCSBOrganizationsController.java

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

  2. import edu.ucsb.cs156.example.entities.UCSBDiningCommons;
  3. import edu.ucsb.cs156.example.entities.UCSBOrganizations;
  4. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  5. import edu.ucsb.cs156.example.repositories.UCSBOrganizationsRepository;


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

  20. import jakarta.validation.Valid;

  21. @Tag(name = "UCSBOrganizations")
  22. @RequestMapping("/api/ucsborganizations")
  23. @RestController
  24. @Slf4j

  25. public class UCSBOrganizationsController extends ApiController {

  26.     @Autowired
  27.     UCSBOrganizationsRepository ucsbOrganizationsRepository;

  28.     @Operation(summary= "List all ucsb organizations")
  29.     @PreAuthorize("hasRole('ROLE_USER')")
  30.     @GetMapping("/all")
  31.     public Iterable<UCSBOrganizations> allOrganizations() {
  32.         Iterable<UCSBOrganizations> organization = ucsbOrganizationsRepository.findAll();
  33.         return organization;
  34.     }

  35.     @Operation(summary= "Get a single organization")
  36.     @PreAuthorize("hasRole('ROLE_USER')")
  37.     @GetMapping("")
  38.     public UCSBOrganizations getById(
  39.             @Parameter(name="orgCode") @RequestParam String orgCode) {
  40.         UCSBOrganizations organization = ucsbOrganizationsRepository.findById(orgCode)
  41.                 .orElseThrow(() -> new EntityNotFoundException(UCSBOrganizations.class, orgCode));
  42.         return organization;
  43.     }
  44.    
  45.     @Operation(summary= "Create a new organization")
  46.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  47.     @PostMapping("/post")
  48.     public UCSBOrganizations postOrganization(
  49.         @Parameter(name="orgCode") @RequestParam String orgCode,
  50.         @Parameter(name="orgTranslationShort") @RequestParam String orgTranslationShort,
  51.         @Parameter(name="orgTranslation") @RequestParam String orgTranslation,
  52.         @Parameter(name="inactive") @RequestParam boolean inactive
  53.         )
  54.         {

  55.         UCSBOrganizations organization = new UCSBOrganizations();
  56.         organization.setOrgCode(orgCode);
  57.         organization.setOrgTranslationShort(orgTranslationShort);
  58.         organization.setOrgTranslation(orgTranslation);
  59.         organization.setInactive(inactive);

  60.         UCSBOrganizations savedOrganizations = ucsbOrganizationsRepository.save(organization);

  61.         return savedOrganizations;
  62.     }

  63.     @Operation(summary= "Update a single organization")
  64.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  65.     @PutMapping("")
  66.     public UCSBOrganizations updateOrganizations(
  67.             @Parameter(name="orgCode") @RequestParam String orgCode,
  68.             @RequestBody @Valid UCSBOrganizations incoming) {

  69.         UCSBOrganizations organizations = ucsbOrganizationsRepository.findById(orgCode)
  70.                 .orElseThrow(() -> new EntityNotFoundException(UCSBOrganizations.class, orgCode));

  71.         organizations.setOrgTranslationShort(incoming.getOrgTranslationShort());
  72.         organizations.setOrgTranslation(incoming.getOrgTranslation());
  73.         organizations.setInactive(incoming.getInactive());

  74.         ucsbOrganizationsRepository.save(organizations);

  75.         return organizations;
  76.     }

  77.     @Operation(summary= "Delete a UCSBOrganization")
  78.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  79.     @DeleteMapping("")
  80.     public Object deleteOrganization(
  81.             @Parameter(name="orgCode") @RequestParam String orgCode) {
  82.         UCSBOrganizations organizations = ucsbOrganizationsRepository.findById(orgCode)
  83.                 .orElseThrow(() -> new EntityNotFoundException(UCSBOrganizations.class, orgCode));

  84.         ucsbOrganizationsRepository.delete(organizations);
  85.         return genericMessage("UCSBOrganizations with id %s deleted".formatted(orgCode));
  86.     }
  87. }