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 jakarta.validation.Valid;
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;
/** This is a REST controller for UCSBOrganization */
@Tag(name = "UCSBOrganization")
@RequestMapping("/api/UCSBOrganization")
@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() {
Iterable<UCSBOrganization> organizations = ucsbOrganizationRepository.findAll();
return organizations;
}
@Operation(summary = "Get a single UCSB organization")
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("")
public UCSBOrganization getById(
@Parameter(name = "orgCode") @RequestParam(required = false) String orgCode,
@Parameter(name = "id") @RequestParam(required = false) String id) {
String lookupOrgCode = orgCode != null ? orgCode : id;
UCSBOrganization organization =
ucsbOrganizationRepository
.findById(lookupOrgCode)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, lookupOrgCode));
return organization;
}
/**
* This method creates a new UCSB organization. Accessible only to users with the role
* "ROLE_ADMIN".
*
* @param orgCode organization code
* @param orgTranslationShort short organization translation
* @param orgTranslation organization translation
* @param inactive whether the organization is inactive
* @return the saved UCSB organization
*/
@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 = new UCSBOrganization();
organization.setOrgCode(orgCode);
organization.setOrgTranslationShort(orgTranslationShort);
organization.setOrgTranslation(orgTranslation);
organization.setInactive(inactive);
UCSBOrganization savedOrganization = ucsbOrganizationRepository.save(organization);
return savedOrganization;
}
@Operation(summary = "Update a single UCSB organization")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("")
public UCSBOrganization updateOrganization(
@Parameter(name = "orgCode") @RequestParam(required = false) String orgCode,
@Parameter(name = "id") @RequestParam(required = false) String id,
@RequestBody @Valid UCSBOrganization incoming) {
String lookupOrgCode = orgCode != null ? orgCode : id;
UCSBOrganization organization =
ucsbOrganizationRepository
.findById(lookupOrgCode)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, lookupOrgCode));
organization.setOrgTranslationShort(incoming.getOrgTranslationShort());
organization.setOrgTranslation(incoming.getOrgTranslation());
organization.setInactive(incoming.getInactive());
ucsbOrganizationRepository.save(organization);
return organization;
}
@Operation(summary = "Delete a UCSB organization")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("")
public Object deleteOrganization(
@Parameter(name = "orgCode") @RequestParam(required = false) String orgCode,
@Parameter(name = "id") @RequestParam(required = false) String id) {
String lookupOrgCode = orgCode != null ? orgCode : id;
UCSBOrganization organization =
ucsbOrganizationRepository
.findById(lookupOrgCode)
.orElseThrow(() -> new EntityNotFoundException(UCSBOrganization.class, lookupOrgCode));
ucsbOrganizationRepository.delete(organization);
return genericMessage("record %s deleted".formatted(lookupOrgCode));
}
}