Bug 19410: Move search_for_api into a Mojo helper
[koha.git] / Koha / REST / Plugin / Objects.pm
blobafd5e8e34e646abb938aa91c463ce563202b532f
1 package Koha::REST::Plugin::Objects;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Plugin';
22 =head1 NAME
24 Koha::REST::Plugin::Objects
26 =head1 API
28 =head2 Helper methods
30 =head3 objects.search
32 my $patrons_set = Koha::Patrons->new;
33 my $patrons = $c->objects->search($patrons_set);
35 Performs a database search using given Koha::Objects object and query parameters
37 Returns a Koha::Objects object
39 =cut
41 sub register {
42 my ( $self, $app ) = @_;
44 $app->helper(
45 'objects.search' => sub {
46 my ( $c, $objects_set ) = @_;
48 my $args = $c->validation->output;
49 my $attributes = {};
51 # Extract reserved params
52 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
54 # Merge sorting into query attributes
55 $c->dbic_merge_sorting(
57 attributes => $attributes,
58 params => $reserved_params
62 # Merge pagination into query attributes
63 $c->dbic_merge_pagination(
65 filter => $attributes,
66 params => $reserved_params
70 # Perform search
71 my $objects = $objects_set->search( $filtered_params, $attributes );
73 if ($objects->is_paged) {
74 $c->add_pagination_headers({
75 total => $objects->pager->total_entries,
76 params => $args,
77 });
80 return $objects;