Bug 26143: Make the API handle per_page=-1
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 5 Aug 2020 15:09:40 +0000 (12:09 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 5 Aug 2020 15:36:28 +0000 (17:36 +0200)
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>

Koha/REST/Plugin/Objects.pm
Koha/REST/Plugin/Pagination.pm

index c743e9d..0f08a2d 100644 (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 });
         }
index d89a114..911aa43 100644 (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,