Bug 26143: Make the API handle per_page=-1

This patch introduces handling for per_page=-1 and actually adds a
missing feature to the API: being able to request all resources on a
route.

To test this:
1. Visit the libraries admin page
2. On the table, choose to display 'All' rows
=> FAIL: it doesn't refresh, the browser console displays a 500 error
code and so the logs
3. Apply the tests patches
4. Run:
   $ kshell
  k$ prove t/Koha/REST/Plugin/Pagination.t \
           t/db_dependent/Koha/REST/Plugin/Objects.t
=> FAIL: Tests fail loudly
5. Apply this patch
6. Restart plack
7. Repeat 2
=> SUCCESS: choosing to display all, works
8. Repeat 4
=> SUCCESS: Tests pass now!
9. Sign off :-D

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

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomas Cohen Arazi 2020-08-05 12:09:40 -03:00 committed by Jonathan Druart
parent d20bc39d75
commit e4cae99f7e
2 changed files with 28 additions and 16 deletions

View File

@ -69,13 +69,15 @@ sub register {
$reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
$reserved_params->{_page} //= 1;
# Merge pagination into query attributes
$c->dbic_merge_pagination(
{
filter => $attributes,
params => $reserved_params
}
);
unless ( $reserved_params->{_per_page} == -1 ) {
# Merge pagination into query attributes
$c->dbic_merge_pagination(
{
filter => $attributes,
params => $reserved_params
}
);
}
# Generate prefetches for embedded stuff
$c->dbic_merge_prefetch(
@ -129,6 +131,12 @@ sub register {
params => $args,
});
}
else {
$c->add_pagination_headers({
total => $objects->count,
params => $args,
});
}
return $objects->to_api({ embed => $embed });
}

View File

@ -61,20 +61,24 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys
my ( $c, $args ) = @_;
my $total = $args->{total};
my $req_page = $args->{params}->{_page};
my $req_page = $args->{params}->{_page} // 1;
my $per_page = $args->{params}->{_per_page} //
C4::Context->preference('RESTdefaultPageSize') // 20;
# do we need to paginate?
return $c unless $req_page;
my $pages = int $total / $per_page;
$pages++
if $total % $per_page > 0;
my $pages;
if ( $per_page == -1 ) {
$req_page = 1;
$pages = 1;
}
else {
$pages = int $total / $per_page;
$pages++
if $total % $per_page > 0;
}
my @links;
if ( $pages > 1 and $req_page > 1 ) { # Previous exists?
if ( $per_page != -1 and $pages > 1 and $req_page > 1 ) { # Previous exists?
push @links,
_build_link(
$c,
@ -86,7 +90,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys
);
}
if ( $pages > 1 and $req_page < $pages ) { # Next exists?
if ( $per_page != -1 and $pages > 1 and $req_page < $pages ) { # Next exists?
push @links,
_build_link(
$c,