class ClientTransplantRequestsController {

    @GetMapping("/clients/transplantRequests")
    @JsonView(Views.Overview.class)
    public ResponseEntity<PaginatedTransplantList> getAllTransplantRequests(
            @RequestParam(value = "offset", required = false) Integer offset,
            @RequestParam(value = "count", required = false) Integer count,
            @RequestParam(value = "regions", required = false) Set<String> regions,
            @RequestParam(value = "organs", required = false) Set<Organ> organs,
            @RequestHeader(value = "X-Auth-Token", required = false) String authToken)
            throws AuthenticationException {

        // Verify that request has clinician/admin authorization
        State.getAuthenticationManager().verifyClinicianOrAdmin(authToken);

        final Set<String> regionsToFilter = new HashSet<>();
        if (regions != null) {
            for (String region : regions) {
                regionsToFilter.add(region.replace("%20", " "));
            }
        }

        // Get all requests that match region/organ filters
        List<TransplantRequestView> matchingRequests = State.getClientManager().getAllTransplantRequests().stream()
                .filter(request -> regionsToFilter.isEmpty()
                        || regionsToFilter.contains(request.getClient().getRegion())
                        || (regionsToFilter.contains("International")
                        && request.getClient().getCountry() != Country.NZ))
                .filter(request -> organs == null || organs.isEmpty() || organs.contains(request.getRequestedOrgan()))
                .map(TransplantRequestView::new)
                .collect(Collectors.toList());

        // Return subset for given offset/count parameters (used for pagination)
        if (offset == null) {
            offset = 0;
        }
        if (count == null) {
            return new ResponseEntity<>(new PaginatedTransplantList(
                    matchingRequests.subList(
                            Math.min(offset, matchingRequests.size()),
                            matchingRequests.size()),
                    matchingRequests.size()),
                    HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new PaginatedTransplantList(
                    matchingRequests.subList(
                            Math.min(offset, matchingRequests.size()),
                            Math.min(offset + count, matchingRequests.size())),
                    matchingRequests.size()),
                    HttpStatus.OK);
        }
    }

}