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
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.
20 use Mojo::Base 'Mojolicious::Plugin';
24 Koha::REST::Plugin::Objects
32 my $patrons_set = Koha::Patrons->new;
33 my $patrons = $c->objects->search( $patrons_set, [\&to_model, \&to_api] );
35 Performs a database search using given Koha::Objects object and query parameters.
36 It (optionally) applies the I<$to_model> function reference before building the
37 query itself, and (optionally) applies I<$to_api> to the result.
39 Returns an arrayref of the hashrefs representing the resulting objects
42 Note: Make sure I<$to_model> and I<$to_api> don't autovivify keys.
47 my ( $self, $app ) = @_;
50 'objects.search' => sub {
51 my ( $c, $objects_set, $to_model, $to_api ) = @_;
53 my $args = $c->validation->output;
56 # Extract reserved params
57 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
59 # Merge sorting into query attributes
60 $c->dbic_merge_sorting(
62 attributes => $attributes,
63 params => $reserved_params
67 # Merge pagination into query attributes
68 $c->dbic_merge_pagination(
70 filter => $attributes,
71 params => $reserved_params
75 # Call the to_model function by reference, if defined
76 if ( defined $filtered_params ) {
78 # Apply the mapping function to the passed params
79 $filtered_params = $to_model->($filtered_params)
81 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
85 my $objects = $objects_set->search( $filtered_params, $attributes );
87 if ($objects->is_paged) {
88 $c->add_pagination_headers({
89 total => $objects->pager->total_entries,
94 my @objects_list = map {
96 ? $to_api->( $_->TO_JSON )
100 return \@objects_list;