Bugzilla – Attachment 66610 Details for
Bug 19196
Add pagination helpers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19196: Add Koha::REST::Plugin::Pagination
Bug-19196-Add-KohaRESTPluginPagination.patch (text/plain), 5.06 KB, created by
Tomás Cohen Arazi (tcohen)
on 2017-08-30 12:24:40 UTC
(
hide
)
Description:
Bug 19196: Add Koha::REST::Plugin::Pagination
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2017-08-30 12:24:40 UTC
Size:
5.06 KB
patch
obsolete
>From 4692190b527e171addd8641a64b63bd7ab87253e Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 29 Aug 2017 16:52:48 -0300 >Subject: [PATCH] Bug 19196: Add Koha::REST::Plugin::Pagination > >This patch introduces a Mojolicious plugin to be used on the REST api. >It adds two helper methods: > >total_count_header >================== > >When used, it adds an _X-Total-Count_ header with the value it got passed >like this: > > my $patrons = Koha::Patrons->search; > my $count = $patrons->count; > > $c->total_count_header({ total => $count }); > >=> Response contains > X-Total-Count: $count > >links_header >============ > >When used, it adds a _Link_ header to the reponse with the calculated >values for pagination, like this: > > my $patrons = Koha::Patrons->search; > my $count = $patrons->count; > > $c->links_header({ total => $count, page => 2, per_page => 2 }); > >To test: >- Run: > $ sudo koha-shell kohadev > k$ cd kohaclone > k$ prove t/Koha/REST/Plugin/Pagination.t >=> SUCCESS: Tests pass! >- Sign off :-D > >Sponsored-by: ByWater solutions >Sponsored-by: Camden County >--- > Koha/REST/Plugin/Pagination.pm | 137 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 137 insertions(+) > create mode 100644 Koha/REST/Plugin/Pagination.pm > >diff --git a/Koha/REST/Plugin/Pagination.pm b/Koha/REST/Plugin/Pagination.pm >new file mode 100644 >index 0000000000..94c047cf72 >--- /dev/null >+++ b/Koha/REST/Plugin/Pagination.pm >@@ -0,0 +1,137 @@ >+package Koha::REST::Plugin::Pagination; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Mojo::Base 'Mojolicious::Plugin'; >+ >+=head1 NAME >+ >+Koha::REST::Plugin::Pagination >+ >+=head1 API >+ >+=head2 Mojolicious::Plugin methods >+ >+=head3 register >+ >+=cut >+ >+sub register { >+ my ( $self, $app ) = @_; >+ >+=head2 Helper methods >+ >+=head3 links_header >+ >+ my $patrons = Koha::Patrons->search( ... ); >+ $c->links_header({ >+ total => $patrons->count, >+ page => $input->{page}, >+ per_page => $input->{per_page} >+ }); >+ >+Adds a Link header to the response message $c carries, following RFC5988, including >+the following relation types: 'prev', 'next', 'first' and 'last'. >+ >+=cut >+ >+ $app->helper( >+ 'links_header' => sub { >+ my ( $c, $params ) = @_; >+ >+ my $total = $params->{total}; >+ my $page = $params->{page}; >+ my $per_page = $params->{per_page}; >+ >+ my $pages = int $total / $per_page; >+ $pages++ >+ if $total % $per_page > 0; >+ >+ >+ my @links; >+ >+ if ( $pages > 1 and $page > 1 ) { # Previous exists? >+ push @links, >+ _build_link( $c, { page => $page - 1, per_page => $per_page, rel => 'prev' } ); >+ } >+ >+ if ( $pages > 1 and $page < $pages ) { # Next exists? >+ push @links, >+ _build_link( $c, { page => $page + 1, per_page => $per_page, rel => 'next' } ); >+ } >+ >+ push @links, _build_link( $c, { page => 1, per_page => $per_page, rel => 'first' } ); >+ push @links, _build_link( $c, { page => $pages, per_page => $per_page, rel => 'last' } ); >+ >+ $c->res->headers->add( 'Link' => join( ',', @links ) ); >+ >+ return $c; >+ } >+ ); >+ >+=head3 total_count_header >+ >+ my $patrons = Koha::Patrons->search( ... ); >+ $c->total_count_header({ total => $patrons->count }); >+ >+Adds an X-Total-Count header containing a passed integer to the response message of $c. >+ >+=cut >+ >+ $app->helper( >+ 'total_count_header' => sub { >+ my ( $c, $params ) = @_; >+ my $total = $params->{total}; >+ >+ $c->res->headers->add( 'X-Total-Count' => $total ) >+ if $total; >+ >+ return $c; >+ } >+ ); >+} >+ >+=head2 Internal methods >+ >+=head3 _build_link >+ >+ my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' }); >+ >+Returns a string, suitable for using in Link headers following RFC5988. >+ >+=cut >+ >+sub _build_link { >+ my ( $c, $params ) = @_; >+ >+ my $link = '<' >+ . $c->req->url->clone->query( >+ [ 'per_page' => $params->{per_page}, >+ 'page' => $params->{page} >+ ] >+ )->to_abs >+ . '>; rel="' >+ . $params->{rel} . '"'; >+ >+ # TODO: Find a better solution for this horrible (but needed) fix >+ $link =~ s|api/v1/app\.pl/||; >+ >+ return $link; >+} >+ >+1; >-- >2.14.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19196
:
66599
|
66600
|
66601
|
66609
|
66610
|
66611
|
66656
|
66657
|
66658
|
66820
|
66821
|
66822
|
66988
|
66989
|
66990
|
67338
|
67339
|
67340
|
67346
|
67366
|
67367
|
67368
|
67369