UCSBOrganizationController.java
package edu.ucsb.cs156.example.controllers;
import edu.ucsb.cs156.example.entities.UCSBOrganization;
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
import edu.ucsb.cs156.example.repositories.UCSBOrganizationRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jakarta.validation.Valid;
@Tag(name = "UCSBOrganizations")
@RequestMapping("/api/ucsborganizations")
@RestController
@Slf4j
public class UCSBOrganizationController extends ApiController {
@Autowired
UCSBOrganizationRepository ucsbOrganizationRepository;
/**
* This method returns a list of all UCSB organizations.
* @return a list of all UCSB organizations
*/
@Operation(summary = "List all UCSB Organizations")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/all")
public Iterable<UCSBOrganization> allOrganizations() {
return ucsbOrganizationRepository.findAll();
}
/**
* This method returns a single UCSB organization.
* Accepts both `id` and `orgCode` as parameters.
* @param id the id of the UCSB organization
* @param orgCode the orgCode of the UCSB organization
* @return a single UCSB organization
*/
@Operation(summary = "Get a single UCSB Organization")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public UCSBOrganization getById(
@RequestParam(required = false) String id,
@RequestParam(required = false) String orgCode) {
String identifier = orgCode;
return ucsbOrganizationRepository.findById(identifier)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, identifier));
}
/**
* This method creates a new UCSB organization.
* Accessible only to users with the role "ROLE_ADMIN".
*/
@Operation(summary = "Create a new UCSB Organization")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/post")
public UCSBOrganization postOrganization(
@Parameter(name = "orgCode") @RequestParam String orgCode,
@Parameter(name = "orgTranslationShort") @RequestParam String orgTranslationShort,
@Parameter(name = "orgTranslation") @RequestParam String orgTranslation,
@Parameter(name = "inactive") @RequestParam boolean inactive) {
UCSBOrganization organization = UCSBOrganization.builder()
.orgCode(orgCode)
.orgTranslationShort(orgTranslationShort)
.orgTranslation(orgTranslation)
.inactive(inactive)
.build();
return ucsbOrganizationRepository.save(organization);
}
/**
* Delete a UCSB organization.
* Accessible only to users with the role "ROLE_ADMIN".
* Accepts both `id` and `orgCode` as parameters.
*/
@Operation(summary = "Delete a UCSB Organization")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("")
public Object deleteOrganization(
@RequestParam(required = false) String id,
@RequestParam(required = false) String orgCode) {
String identifier = orgCode;
UCSBOrganization organization = ucsbOrganizationRepository.findById(identifier)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, identifier));
ucsbOrganizationRepository.delete(organization);
return genericMessage("UCSBOrganization with id %s deleted".formatted(identifier));
}
/**
* Update a single UCSB organization.
* Accessible only to users with the role "ROLE_ADMIN".
* Accepts both `id` and `orgCode` as parameters.
*/
@Operation(summary = "Update a single UCSB Organization")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("")
public UCSBOrganization updateOrganization(
@RequestParam(required = false) String id,
@RequestParam(required = true) String orgCode,
@RequestBody @Valid UCSBOrganization incoming) {
String identifier = orgCode;
UCSBOrganization organization = ucsbOrganizationRepository.findById(identifier)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, identifier));
// organization.setOrgCode(incoming.getOrgCode());
organization.setOrgTranslationShort(incoming.getOrgTranslationShort());
organization.setOrgTranslation(incoming.getOrgTranslation());
organization.setInactive(incoming.getInactive());
ucsbOrganizationRepository.save(organization);
return organization;
}
}