Bug 27353: Set X-Base-Total-Count header for REST API

We already set X-Total-Count to the total number of filtered rows,
but we don't have the total number of non-filtered rows.

Test plan:
This is easy to test on top of bug 27352 or bug 27251, apply them if not
pushed yet.
1. Create 40 items with public notes = "xxx" for biblionumber=4
then, using Postman (or whatever you prefer):
http://kohadev-intra.mydnsname.org:8081/api/v1/biblios/4/items?_page=1&_per_page=20&q=[{"me.public_notes"%3A{"like"%3A"%25x%25"}}]&_match=contains
Check the headers and confirm you see X-Total-Count=40 and
X-Base-Total-Count=44

2. go to /cgi-bin/koha/tools/quotes.pl
You see "Showing 1 to 20 of 28 entries"
Search "he"
Showing 1 to 20 of 22 entries (filtered from 28 total entries)

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
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:
Jonathan Druart 2021-01-06 16:34:23 +01:00
parent 58c1ddc151
commit 7ddb60e292
5 changed files with 26 additions and 21 deletions

View file

@ -124,19 +124,15 @@ sub register {
}
# Perform search
my $objects = $result_set->search( $filtered_params, $attributes );
my $total = $result_set->search->count;
if ($objects->is_paged) {
$c->add_pagination_headers({
total => $objects->pager->total_entries,
params => $args,
});
}
else {
$c->add_pagination_headers({
total => $objects->count,
params => $args,
});
}
$c->add_pagination_headers(
{
total => ($objects->is_paged ? $objects->pager->total_entries : $objects->count),
base_total => $total,
params => $args,
}
);
return $objects->to_api({ embed => $embed });
}

View file

@ -50,7 +50,7 @@ sub register {
Adds a Link header to the response message $c carries, following RFC5988, including
the following relation types: 'prev', 'next', 'first' and 'last'.
It also adds X-Total-Count, containing the total results count.
It also adds X-Total-Count containing the total results count, and X-Base-Total-Count containing the total of the non-filtered results count.
If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref.
@ -61,6 +61,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys
my ( $c, $args ) = @_;
my $total = $args->{total};
my $base_total = $args->{base_total};
my $req_page = $args->{params}->{_page} // 1;
my $per_page = $args->{params}->{_per_page} //
C4::Context->preference('RESTdefaultPageSize') // 20;
@ -114,6 +115,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys
# Add X-Total-Count header
$c->res->headers->add( 'X-Total-Count' => $total );
$c->res->headers->add( 'X-Base-Total-Count' => $base_total );
return $c;
}
);

View file

@ -82,14 +82,15 @@ sub list {
if $restricted;
my $patrons = $patrons_rs->search( $filtered_params, $attributes );
if ( $patrons_rs->is_paged ) {
$c->add_pagination_headers(
{
total => $patrons->pager->total_entries,
params => $args,
}
);
}
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 );
}

View file

@ -544,6 +544,9 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
json.recordsTotal = total;
json.recordsFiltered = total;
}
if(total = this._xhr.getResponseHeader('x-base-total-count')) {
json.recordsTotal = total;
}
return JSON.stringify(json);
},
'data': function( data, settings ) {

View file

@ -207,6 +207,9 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, {
json.recordsTotal = total;
json.recordsFiltered = total;
}
if(total = this._xhr.getResponseHeader('x-base-total-count')) {
json.recordsTotal = total;
}
return JSON.stringify(json);
},
'data': function( data, settings ) {