Bug 27854: Clean GET /patrons controller

The controller method was written a lot of time before the enhancements
we added to the objects.search helper, and it is now much easier to
handle the 'restricted' param use case.

No need to do it like that anymore.

This patch fetches the 'restricted' param from the query parameters and
cleans it from the validated data, so we can just pass the resultset to
$c->objects->search as in all other controllers.

And we had tests for the expected behavior, so testing this is as easy
as:

1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass!
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests still pass! Of course!
4. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomas Cohen Arazi 2021-03-03 16:37:09 -03:00 committed by Jonathan Druart
parent 4a65fe61f4
commit 46a024a6e0
1 changed files with 9 additions and 47 deletions

View File

@ -44,62 +44,24 @@ sub list {
return try {
my $patrons_rs = Koha::Patrons->new;
my $args = $c->validation->output;
my $attributes = {};
my $query = {};
my $restricted = delete $c->validation->output->{restricted};
$query->{debarred} = { '!=' => undef }
if $restricted;
# Extract reserved params
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
my $patrons_rs = Koha::Patrons->search($query);
my $patrons = $c->objects->search( $patrons_rs );
my $restricted = delete $filtered_params->{restricted};
# Merge sorting into query attributes
$c->dbic_merge_sorting(
{
attributes => $attributes,
params => $reserved_params,
result_set => $patrons_rs
}
return $c->render(
status => 200,
openapi => $patrons
);
# Merge pagination into query attributes
$c->dbic_merge_pagination(
{
filter => $attributes,
params => $reserved_params
}
);
if ( defined $filtered_params ) {
# Apply the mapping function to the passed params
$filtered_params = $patrons_rs->attributes_from_api($filtered_params);
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
}
# translate 'restricted' => 'debarred'
$filtered_params->{debarred} = { '!=' => undef }
if $restricted;
my $patrons = $patrons_rs->search( $filtered_params, $attributes );
my $total = $patrons_rs->search->count;
$c->add_pagination_headers(
{
total => ($patrons->is_paged ? $patrons->pager->total_entries : $patrons->count),
base_total => $total,
params => $args,
}
);
return $c->render( status => 200, openapi => $patrons->to_api );
}
catch {
$c->unhandled_exception($_);
};
}
=head3 get
Controller function that handles retrieving a single Koha::Patron object