From 580f2a39c04bccef269d42f05eba9c1a6d8cacf6 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 21 Nov 2017 14:36:49 +0000 Subject: [PATCH] Bug 19410: Move search_for_api into a Mojo helper Signed-off-by: Tomas Cohen Arazi Signed-off-by: Julian Maurice Signed-off-by: Lari Taskula Signed-off-by: Kyle M Hall --- Koha/Objects.pm | 41 --------------- Koha/REST/Plugin/Objects.pm | 85 +++++++++++++++++++++++++++++++ Koha/REST/V1.pm | 1 + t/db_dependent/Koha/REST/Plugin/Objects.t | 77 ++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 41 deletions(-) create mode 100644 Koha/REST/Plugin/Objects.pm create mode 100644 t/db_dependent/Koha/REST/Plugin/Objects.t diff --git a/Koha/Objects.pm b/Koha/Objects.pm index e695c9c..cfc2475 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -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 ); diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm new file mode 100644 index 0000000..afd5e8e --- /dev/null +++ b/Koha/REST/Plugin/Objects.pm @@ -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; diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm index 8969959..398a93c 100644 --- a/Koha/REST/V1.pm +++ b/Koha/REST/V1.pm @@ -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; diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t new file mode 100644 index 0000000..6d305f5 --- /dev/null +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -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 . + +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/; rel="next",/ ) + ->json_has('/0') + ->json_hasnt('/1') + ->json_is('/0/firstname' => 'Manuel'); +}; + +$schema->storage->txn_rollback(); -- 2.10.2