Bug 19410: Move search_for_api into a Mojo helper
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Julian Maurice <julian.maurice@biblibre.com> Signed-off-by: Lari Taskula <lari.taskula@jns.fi> 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
9f5b10126d
commit
86706c3b34
|
|
@ -171,47 +171,6 @@ sub search_related {
|
|||
}
|
||||
}
|
||||
|
||||
=head3 search_for_api
|
||||
|
||||
my $objects = Koha::Objects->search_for_api( $c );
|
||||
|
||||
Searches for objects given a controller object I<$c>.
|
||||
|
||||
=cut
|
||||
|
||||
sub search_for_api {
|
||||
my ( $self, $c ) = @_;
|
||||
|
||||
my $args = $c->validation->output;
|
||||
my $attributes;
|
||||
|
||||
# Extract reserved params
|
||||
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
|
||||
|
||||
# Merge sorting into query attributes
|
||||
$c->dbic_merge_sorting(
|
||||
{
|
||||
attributes => $attributes,
|
||||
params => $reserved_params
|
||||
}
|
||||
);
|
||||
|
||||
# Merge pagination into query attributes
|
||||
$c->dbic_merge_pagination(
|
||||
{
|
||||
attributes => $attributes,
|
||||
params => $reserved_params
|
||||
}
|
||||
);
|
||||
|
||||
# Perform search
|
||||
my $objects = $self->search( $filtered_params, $attributes );
|
||||
$c->add_pagination_headers({ total => $objects->count, params => $args })
|
||||
if $objects->is_paged;
|
||||
|
||||
return $objects;
|
||||
}
|
||||
|
||||
=head2 _build_query_params_from_api
|
||||
|
||||
my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
package Koha::REST::Plugin::Objects;
|
||||
|
||||
# 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::Objects
|
||||
|
||||
=head1 API
|
||||
|
||||
=head2 Helper methods
|
||||
|
||||
=head3 objects.search
|
||||
|
||||
my $patrons_set = Koha::Patrons->new;
|
||||
my $patrons = $c->objects->search($patrons_set);
|
||||
|
||||
Performs a database search using given Koha::Objects object and query parameters
|
||||
|
||||
Returns a Koha::Objects object
|
||||
|
||||
=cut
|
||||
|
||||
sub register {
|
||||
my ( $self, $app ) = @_;
|
||||
|
||||
$app->helper(
|
||||
'objects.search' => sub {
|
||||
my ( $c, $objects_set ) = @_;
|
||||
|
||||
my $args = $c->validation->output;
|
||||
my $attributes = {};
|
||||
|
||||
# Extract reserved params
|
||||
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
|
||||
|
||||
# Merge sorting into query attributes
|
||||
$c->dbic_merge_sorting(
|
||||
{
|
||||
attributes => $attributes,
|
||||
params => $reserved_params
|
||||
}
|
||||
);
|
||||
|
||||
# Merge pagination into query attributes
|
||||
$c->dbic_merge_pagination(
|
||||
{
|
||||
filter => $attributes,
|
||||
params => $reserved_params
|
||||
}
|
||||
);
|
||||
|
||||
# Perform search
|
||||
my $objects = $objects_set->search( $filtered_params, $attributes );
|
||||
|
||||
if ($objects->is_paged) {
|
||||
$c->add_pagination_headers({
|
||||
total => $objects->pager->total_entries,
|
||||
params => $args,
|
||||
});
|
||||
}
|
||||
|
||||
return $objects;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
@ -55,6 +55,7 @@ sub startup {
|
|||
});
|
||||
$self->plugin( 'Koha::REST::Plugin::Pagination' );
|
||||
$self->plugin( 'Koha::REST::Plugin::Query' );
|
||||
$self->plugin( 'Koha::REST::Plugin::Objects' );
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
# 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, see <http://www.gnu.org/licenses>.
|
||||
|
||||
use Modern::Perl;
|
||||
use Koha::Patrons;
|
||||
|
||||
# Dummy app for testing the plugin
|
||||
use Mojolicious::Lite;
|
||||
|
||||
app->log->level('error');
|
||||
|
||||
plugin 'Koha::REST::Plugin::Objects';
|
||||
plugin 'Koha::REST::Plugin::Query';
|
||||
plugin 'Koha::REST::Plugin::Pagination';
|
||||
|
||||
get '/patrons' => sub {
|
||||
my $c = shift;
|
||||
$c->validation->output($c->req->params->to_hash);
|
||||
my $patrons = $c->objects->search(Koha::Patrons->new);
|
||||
$c->render( status => 200, json => $patrons->TO_JSON );
|
||||
};
|
||||
|
||||
|
||||
# The tests
|
||||
|
||||
use Test::More tests => 1;
|
||||
use Test::Mojo;
|
||||
|
||||
use t::lib::TestBuilder;
|
||||
use Koha::Database;
|
||||
|
||||
my $schema = Koha::Database->new()->schema();
|
||||
$schema->storage->txn_begin();
|
||||
|
||||
my $builder = t::lib::TestBuilder->new;
|
||||
$builder->build({
|
||||
source => 'Borrower',
|
||||
value => {
|
||||
firstname => 'Manuel',
|
||||
},
|
||||
});
|
||||
$builder->build({
|
||||
source => 'Borrower',
|
||||
value => {
|
||||
firstname => 'Manuel',
|
||||
},
|
||||
});
|
||||
|
||||
subtest 'objects.search helper' => sub {
|
||||
|
||||
plan tests => 6;
|
||||
|
||||
my $t = Test::Mojo->new;
|
||||
|
||||
$t->get_ok('/patrons?firstname=Manuel&_per_page=1&_page=1')
|
||||
->status_is(200)
|
||||
->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
|
||||
->json_has('/0')
|
||||
->json_hasnt('/1')
|
||||
->json_is('/0/firstname' => 'Manuel');
|
||||
};
|
||||
|
||||
$schema->storage->txn_rollback();
|
||||
Loading…
Reference in New Issue