Bugzilla – Attachment 69256 Details for
Bug 19410
Add a helper function for generating object searches for the API
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19410: Move search_for_api into a Mojo helper
Bug-19410-Move-searchforapi-into-a-Mojo-helper.patch (text/plain), 7.04 KB, created by
Tomás Cohen Arazi (tcohen)
on 2017-11-21 19:20:35 UTC
(
hide
)
Description:
Bug 19410: Move search_for_api into a Mojo helper
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2017-11-21 19:20:35 UTC
Size:
7.04 KB
patch
obsolete
>From 1f8d45625e436425e5277bd984de6d2bbc3df17e Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >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 <tomascohen@theke.io> >--- > 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 e695c9cacf..cfc2475d7c 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 0000000000..afd5e8e34e >--- /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 896995945f..398a93cc1a 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 0000000000..6d305f546d >--- /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 <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(); >-- >2.14.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19410
:
67613
|
67731
|
67732
|
67750
|
67751
|
67795
|
67798
|
69248
|
69253
|
69254
|
69255
|
69256
|
69257
|
69258
|
69259
|
69260
|
69261
|
69262
|
69282
|
69283
|
69284
|
69286
|
69287
|
69385
|
69386
|
69387
|
69388
|
69389
|
69397
|
69398
|
69399
|
69400
|
69401
|
69545
|
69703