UCSBOrganizationController.java

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

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

  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 com.fasterxml.jackson.core.JsonProcessingException;

  20. import jakarta.validation.Valid;

  21. @Tag(name = "UCSBOrganizations")
  22. @RequestMapping("/api/ucsborganizations")
  23. @RestController
  24. @Slf4j
  25. public class UCSBOrganizationController extends ApiController{
  26.     @Autowired
  27.     UCSBOrganizationsRepository ucsbOrganizationsRepository;
  28.     /**
  29.      * This method returns a list of all ucsborganizations.
  30.      * @return a list of all ucsborganizations
  31.      */
  32.     @Operation(summary= "List all ucsb organizations")
  33.     @PreAuthorize("hasRole('ROLE_USER')")
  34.     @GetMapping("/all")
  35.     public Iterable<UCSBOrganizations> allOrganizations() {
  36.         Iterable<UCSBOrganizations> organizations = ucsbOrganizationsRepository.findAll();
  37.         return organizations;
  38.     }

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

  53.     /**
  54.      * This method creates a new organiation. Accessible only to users with the role "ROLE_ADMIN".
  55.      * @param orgCode code of the organization
  56.      * @param orgTranslationShort short translation of the organization
  57.      * @param orgTranslation translation of the organization
  58.      * @param inactive whether the organization is inactive
  59.      * @return the newly created organization
  60.      */
  61.     @Operation(summary= "Create a new organization")
  62.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  63.     @PostMapping("/post")
  64.     public UCSBOrganizations postOrgs(
  65.         @Parameter(name="orgCode") @RequestParam String orgCode,
  66.         @Parameter(name="orgTranslationShort") @RequestParam String orgTranslationShort,
  67.         @Parameter(name="orgTranslation") @RequestParam String orgTranslation,
  68.         @Parameter(name="inactive") @RequestParam boolean inactive
  69.         )
  70.         {
  71.         UCSBOrganizations org = new UCSBOrganizations();
  72.         org.setOrgCode(orgCode);
  73.         org.setOrgTranslationShort(orgTranslationShort);
  74.         org.setOrgTranslation(orgTranslation);
  75.         org.setInactive(inactive);
  76.         UCSBOrganizations savedOrg = ucsbOrganizationsRepository.save(org);
  77.         return savedOrg;
  78.     }

  79.     /**
  80.      * Update a single org. Accessible only to users with the role "ROLE_ADMIN".
  81.      * @param orgCode the code of the org to update
  82.      * @param incoming the new org contents
  83.      * @return the updated org object
  84.      */
  85.     @Operation(summary= "Update a single organization")
  86.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  87.     @PutMapping("")
  88.     public UCSBOrganizations updateOrgs(
  89.             @Parameter(name="orgCode") @RequestParam String orgCode,
  90.             @RequestBody @Valid UCSBOrganizations incoming) {
  91.         UCSBOrganizations org = ucsbOrganizationsRepository.findById(orgCode)
  92.                 .orElseThrow(() -> new EntityNotFoundException(UCSBOrganizations.class, orgCode));
  93.         org.setOrgCode(incoming.getOrgCode());
  94.         org.setOrgTranslationShort(incoming.getOrgTranslationShort());
  95.         org.setOrgTranslation(incoming.getOrgTranslation());
  96.         org.setInactive(incoming.getInactive());
  97.         ucsbOrganizationsRepository.save(org);
  98.         return org;
  99.     }

  100.     /**
  101.      * Delete an organization. Accessible only to users with the role "ROLE_ADMIN".
  102.      * @param orgCode code of the organization
  103.      * @return a message indiciating the organization was deleted
  104.      */
  105.     @Operation(summary= "Delete a UCSBOrganization")
  106.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  107.     @DeleteMapping("")
  108.     public Object deleteOrgs(
  109.             @Parameter(name="orgCode") @RequestParam String orgCode) {
  110.         UCSBOrganizations org = ucsbOrganizationsRepository.findById(orgCode)
  111.                 .orElseThrow(() -> new EntityNotFoundException(UCSBOrganizations.class, orgCode));
  112.         ucsbOrganizationsRepository.delete(org);
  113.         return genericMessage("UCSBOrganizations with id %s deleted".formatted(orgCode));
  114.     }
  115. }