Bug 19686: (follow-up) Add to_api param for completeness
This patch adds (yet) another param to objects.search: a reference to a to_api function to be applied when processing the search results for output. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/Objects.t => SUCCESS: Test count raised, and tests pass! - Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
parent
4bd62d0a8c
commit
ae7f0ae4b5
2 changed files with 72 additions and 10 deletions
|
@ -30,15 +30,16 @@ Koha::REST::Plugin::Objects
|
|||
=head3 objects.search
|
||||
|
||||
my $patrons_set = Koha::Patrons->new;
|
||||
my $patrons = $c->objects->search( $patrons_set, [\&to_model] );
|
||||
my $patrons = $c->objects->search( $patrons_set, [\&to_model, \&to_api] );
|
||||
|
||||
Performs a database search using given Koha::Objects object and query parameters.
|
||||
Optionally, it applies the I<$to_model> function reference before building the
|
||||
query itself.
|
||||
It (optionally) applies the I<$to_model> function reference before building the
|
||||
query itself, and (optionally) applies I<$to_api> to the result.
|
||||
|
||||
Note: Make sure I<$to_model> doesn't autovivify keys.
|
||||
Returns an arrayref of the hashrefs representing the resulting objects
|
||||
for JSON rendering.
|
||||
|
||||
Returns a Koha::Objects object
|
||||
Note: Make sure I<$to_model> and I<$to_api> don't autovivify keys.
|
||||
|
||||
=cut
|
||||
|
||||
|
@ -47,7 +48,7 @@ sub register {
|
|||
|
||||
$app->helper(
|
||||
'objects.search' => sub {
|
||||
my ( $c, $objects_set, $to_model ) = @_;
|
||||
my ( $c, $objects_set, $to_model, $to_api ) = @_;
|
||||
|
||||
my $args = $c->validation->output;
|
||||
my $attributes = {};
|
||||
|
@ -90,7 +91,13 @@ sub register {
|
|||
});
|
||||
}
|
||||
|
||||
return $objects;
|
||||
my @objects_list = map {
|
||||
( defined $to_api )
|
||||
? $to_api->( $_->TO_JSON )
|
||||
: $_->TO_JSON
|
||||
} $objects->as_list;
|
||||
|
||||
return \@objects_list;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -38,11 +38,19 @@ get '/patrons_to_model' => sub {
|
|||
my $c = shift;
|
||||
$c->validation->output($c->req->params->to_hash);
|
||||
my $patrons_set = Koha::Patrons->new;
|
||||
my $patrons = $c->objects->search( $patrons_set, \&_to_model );
|
||||
my $patrons = $c->objects->search( $patrons_set, \&to_model );
|
||||
$c->render( status => 200, json => $patrons );
|
||||
};
|
||||
|
||||
sub _to_model {
|
||||
get '/patrons_to_model_to_api' => sub {
|
||||
my $c = shift;
|
||||
$c->validation->output($c->req->params->to_hash);
|
||||
my $patrons_set = Koha::Patrons->new;
|
||||
my $patrons = $c->objects->search( $patrons_set, \&to_model, \&to_api );
|
||||
$c->render( status => 200, json => $patrons );
|
||||
};
|
||||
|
||||
sub to_model {
|
||||
my $params = shift;
|
||||
|
||||
if ( exists $params->{nombre} ) {
|
||||
|
@ -52,6 +60,16 @@ sub _to_model {
|
|||
return $params;
|
||||
}
|
||||
|
||||
sub to_api {
|
||||
my $params = shift;
|
||||
|
||||
if ( exists $params->{firstname} ) {
|
||||
$params->{nombre} = delete $params->{firstname};
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
# The tests
|
||||
use Test::More tests => 1;
|
||||
use Test::Mojo;
|
||||
|
@ -66,7 +84,7 @@ my $builder = t::lib::TestBuilder->new;
|
|||
|
||||
subtest 'objects.search helper' => sub {
|
||||
|
||||
plan tests => 62;
|
||||
plan tests => 90;
|
||||
|
||||
my $t = Test::Mojo->new;
|
||||
|
||||
|
@ -175,5 +193,42 @@ subtest 'objects.search helper' => sub {
|
|||
->json_is('/1/firstname' => 'Manuela')
|
||||
->json_is('/2/firstname' => 'Emanuel');
|
||||
|
||||
## _to_model && _to_api tests
|
||||
# _match=starts_with
|
||||
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
|
||||
->status_is(200)
|
||||
->json_has('/0')
|
||||
->json_has('/1')
|
||||
->json_hasnt('/2')
|
||||
->json_is('/0/nombre' => 'Manuel')
|
||||
->json_is('/1/nombre' => 'Manuela');
|
||||
|
||||
# _match=ends_with
|
||||
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
|
||||
->status_is(200)
|
||||
->json_has('/0')
|
||||
->json_has('/1')
|
||||
->json_hasnt('/2')
|
||||
->json_is('/0/nombre' => 'Manuel')
|
||||
->json_is('/1/nombre' => 'Emanuel');
|
||||
|
||||
# _match=exact
|
||||
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
|
||||
->status_is(200)
|
||||
->json_has('/0')
|
||||
->json_hasnt('/1')
|
||||
->json_is('/0/nombre' => 'Manuel');
|
||||
|
||||
# _match=contains
|
||||
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
|
||||
->status_is(200)
|
||||
->json_has('/0')
|
||||
->json_has('/1')
|
||||
->json_has('/2')
|
||||
->json_hasnt('/3')
|
||||
->json_is('/0/nombre' => 'Manuel')
|
||||
->json_is('/1/nombre' => 'Manuela')
|
||||
->json_is('/2/nombre' => 'Emanuel');
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue