Skip to content
Snippets Groups Projects

Resolve "Person endpoints: filterBy for all people"

3 files
+ 201
7
Compare changes
  • Side-by-side
  • Inline

Files

@@ -17,8 +17,6 @@
*/
package nl.tudelft.labracore.controller;
import static org.springframework.http.ResponseEntity.ok;
import java.util.List;
import javax.transaction.Transactional;
@@ -30,11 +28,12 @@ import nl.tudelft.labracore.dto.view.PersonViewDTO;
import nl.tudelft.labracore.dto.view.RoleViewDTO;
import nl.tudelft.labracore.model.Person;
import nl.tudelft.labracore.repository.PersonRepository;
import nl.tudelft.labracore.repository.ProgramRepository;
import nl.tudelft.labracore.service.CohortService;
import nl.tudelft.librador.dto.view.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
@@ -47,6 +46,12 @@ public class PersonController {
@Autowired
private PersonRepository pr;
@Autowired
private ProgramRepository pgr;
@Autowired
private CohortService cs;
@Autowired
private ApplicationContext context;
@@ -59,7 +64,7 @@ public class PersonController {
*/
@PostMapping
@PreAuthorize("hasAuthority('PERSON_CREATE')")
public @ResponseBody Long addPerson(@Valid @RequestBody PersonCreateDTO dto, Errors errors) {
public Long addPerson(@Valid @RequestBody PersonCreateDTO dto, Errors errors) {
return pr.save(dto.apply(errors)).getId();
}
@@ -73,7 +78,7 @@ public class PersonController {
*/
@PatchMapping("{id}")
@PreAuthorize("hasAuthority('PERSON_UPDATE')")
public @ResponseBody Long patchPerson(@PathVariable Long id,
public Long patchPerson(@PathVariable Long id,
@Valid @RequestBody PersonPatchDTO patch,
Errors errors) {
return patch.apply(pr.findByIdOrThrow(id), errors, context).getId();
@@ -87,9 +92,9 @@ public class PersonController {
*/
@GetMapping("{id}")
@PreAuthorize("hasAuthority('PERSON_READ')")
public ResponseEntity<PersonViewDTO> getPersonById(@PathVariable Long id) {
public PersonViewDTO getPersonById(@PathVariable Long id) {
Person person = pr.findByIdOrThrow(id);
return ok(View.convert(person, PersonViewDTO.class));
return View.convert(person, PersonViewDTO.class);
}
/**
@@ -118,6 +123,102 @@ public class PersonController {
return View.convert(person, PersonViewDTO.class);
}
/**
* Searches out those people that have a username containing the given partial username.
*
* @param username The partial username to find.
* @return The list of people with names that contain the given part.
*/
@GetMapping("search/username")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByUsernameContaining(@RequestBody String username) {
return View.convertList(pr.findAllByUsernameContaining(username), PersonViewDTO.class);
}
/**
* Searches out those people that have a username containing the given partial username. Additionally, it
* only does this search for people within a certain program.
*
* @param username The partial username to find.
* @param programId The id of the program to find.
* @return The list of people with names that contain the given part.
*/
@GetMapping("search/username/{programId}")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByProgramAndUsernameContaining(@RequestBody String username,
@PathVariable Long programId) {
return View.convertList(pr.findAllByUsernameContainingAndProgramsContaining(
username, pgr.findByIdOrThrow(programId)), PersonViewDTO.class);
}
/**
* Searches out those people that have a display name containing the given partial display name.
*
* @param displayName The partial display name to find.
* @return The list of people with names that contain the given part.
*/
@GetMapping("search/display-name")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByDisplayNameContaining(@RequestBody String displayName) {
return View.convertList(pr.findAllByDisplayNameContaining(displayName), PersonViewDTO.class);
}
/**
* Searches out those people that have a display name containing the given partial display name.
* Additionally, it only does this search for people within a certain program.
*
* @param displayName The partial display name to find.
* @param programId The id of the program to find.
* @return The list of people with names that contain the given part.
*/
@GetMapping("search/display-name/{programId}")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByDisplayNameContaining(@RequestBody String displayName,
@PathVariable Long programId) {
return View.convertList(pr.findAllByDisplayNameContainingAndProgramsContaining(
displayName, pgr.findByIdOrThrow(programId)), PersonViewDTO.class);
}
/**
* Searches out those people that have a number starting with the given partial number.
*
* @param number The partial number to find.
* @return The list of people with numbers that start with the given part.
*/
@GetMapping("search/number")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByNumberContaining(@RequestBody Integer number) {
return View.convertList(pr.findAllByNumberStartingWith(number), PersonViewDTO.class);
}
/**
* Searches out those people that have a number starting with the given partial number. Additionally, it
* only does this search for people within a certain program.
*
* @param number The partial number to find.
* @param programId The id of the program to find.
* @return The list of people with numbers that start with the given part.
*/
@GetMapping("search/number/{programId}")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> searchPeopleByNumberContaining(@RequestBody Integer number,
@PathVariable Long programId) {
return View.convertList(pr.findAllByNumberStartingWithAndProgramsContaining(
number, pgr.findByIdOrThrow(programId)), PersonViewDTO.class);
}
/**
* Finds all the people belonging to a cohort of students.
*
* @param cohortId The ID of the cohort to find all students for.
* @return The full list of students in the cohort.
*/
@GetMapping("by/cohort/{cohortId}")
@PreAuthorize("hasAuthority('PERSON_READ')")
public List<PersonViewDTO> getPeopleByCohort(@PathVariable Long cohortId) {
return View.convertList(cs.getPeopleInCohort(cohortId), PersonViewDTO.class);
}
/**
* Gets the roles of a Person with the given id.
*
Loading