From 93bf4c9318fdde9df22b0d677f6fb83b8f4fc87c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 3 Mar 2023 16:51:00 -0300 Subject: [PATCH] Bug 33080: Add small warpper helper to call add_pagination_headers This patch introduces a small helper that uses the stashed values in `search_rs` to properly build the pagination headers based on the resultset. To test: 1. Run all the API tests and make sure nothing breaks 2. Run: $ ktd --shell $ t/db_dependent/Koha/REST/Plugin/Pagination.t => SUCCESS: Tests pass! Signed-off-by: Martin Renvoize --- Koha/REST/Plugin/Objects.pm | 45 +++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 462fdf97da..af0b0b4519 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -114,7 +114,10 @@ shouldn't be called twice in it. 'objects.search' => sub { my ( $c, $result_set ) = @_; - return $c->objects->to_api( $c->objects->search_rs($result_set) ); + my $objects_rs = $c->objects->search_rs($result_set); + $c->objects->generate_pagination_headers($objects_rs); + + return $c->objects->to_api( $objects_rs ); } ); @@ -248,17 +251,8 @@ shouldn't be called twice in it. my $objects = $result_set->search( $filtered_params, $attributes ); my $total = $result_set->search->count; - $c->add_pagination_headers( - { - total => ( - $objects->is_paged - ? $objects->pager->total_entries - : $objects->count - ), - base_total => $total, - params => $args, - } - ); + $c->stash('koha.pagination.base_total' => $result_set->count); + $c->stash('koha.pagination.query_params' => $args); return $objects; } @@ -293,6 +287,33 @@ Returns the API representation of the passed resultset. ); } ); + +=head3 objects.total_from_resultset + + my $objects_rs = $objects->search_rs( $rs ); + my $api_representation = $c->objects->generate_pagination_headers( $objects_rs ); + +Returns the API representation of the passed resultset. + +=cut + + $app->helper( + 'objects.generate_pagination_headers' => sub { + my ( $c, $result_set ) = @_; + + $c->add_pagination_headers( + { + total => $c->objects->total_from_resultset( $result_set ), + base_total => $c->stash('koha.pagination.base_total'), + params => $c->stash('koha.pagination.query_params'), + } + ); + + return $result_set->is_paged + ? $result_set->pager->total_entries + : $result_set->count + } + ); } 1; -- 2.39.2