From 9b3d898a40111c6155f449922f707460d7c984b8 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 29 Jan 2020 09:24:35 -0300 Subject: [PATCH] Bug 24528: Add a syntax to x-koha-embed to specify counts Content-Type: text/plain; charset=utf-8 This patch adds a + syntax to specify on the x-koha-embed header that we want the count of a relation in the response. For example: GET /patrons/1 x-koha-embed: checkouts+count Would return a JSON representation of a Koha::Patron, with a new attribute added: checkouts_count, which will be the result of calling $patron->checkouts->count. This is all done automatically in to_api. This patch makes parsing the x-koha-embed header build the right structure for passing to the to_api method. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/REST/Plugin/Query.t => SUCCESS: Tests pass! 3. Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: David Nind Signed-off-by: David Nind Signed-off-by: Marcel de Rooy --- Koha/REST/Plugin/Query.pm | 8 +++++++- t/Koha/REST/Plugin/Query.t | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm index 769fd0b614..80bdf26216 100644 --- a/Koha/REST/Plugin/Query.pm +++ b/Koha/REST/Plugin/Query.pm @@ -263,7 +263,13 @@ sub _parse_embed { $result->{$curr} = { children => _parse_embed( $next ) }; } else { - $result->{$curr} = {}; + if ( $curr =~ m/^(?.*)\+count/ ) { + my $key = $+{relation} . "_count"; + $result->{$key} = { is_count => 1 }; + } + else { + $result->{$curr} = {}; + } } return $result; diff --git a/t/Koha/REST/Plugin/Query.t b/t/Koha/REST/Plugin/Query.t index 44fc48b8be..a10289ac5b 100644 --- a/t/Koha/REST/Plugin/Query.t +++ b/t/Koha/REST/Plugin/Query.t @@ -140,7 +140,8 @@ get '/stash_embed' => sub { 'x-koha-embed' => [ 'checkouts', 'checkouts.item', - 'library' + 'library', + 'holds+count' ] } } @@ -304,7 +305,7 @@ subtest '_build_query_params_from_api' => sub { subtest 'stash_embed() tests' => sub { - plan tests => 12; + plan tests => 15; my $t = Test::Mojo->new; @@ -316,6 +317,10 @@ subtest 'stash_embed() tests' => sub { ->status_is(200) ->json_is( { checkouts => { children => { item => {} } }, library => {} } ); + $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } ) + ->status_is(200) + ->json_is( { holds_count => { is_count => 1 } } ); + $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } ) ->status_is(400) ->json_is( -- 2.11.0