Bug 22521: DBRev 18.12.00.055
[koha.git] / Koha / REST / Plugin / Objects.pm
1 package Koha::REST::Plugin::Objects;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 =head1 NAME
23
24 Koha::REST::Plugin::Objects
25
26 =head1 API
27
28 =head2 Helper methods
29
30 =head3 objects.search
31
32     my $patrons_set = Koha::Patrons->new;
33     my $patrons = $c->objects->search( $patrons_set, [\&to_model, \&to_api] );
34
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.
38
39 Returns an arrayref of the hashrefs representing the resulting objects
40 for JSON rendering.
41
42 Note: Make sure I<$to_model> and I<$to_api> don't autovivify keys.
43
44 =cut
45
46 sub register {
47     my ( $self, $app ) = @_;
48
49     $app->helper(
50         'objects.search' => sub {
51             my ( $c, $objects_set, $to_model, $to_api ) = @_;
52
53             my $args = $c->validation->output;
54             my $attributes = {};
55
56             # Extract reserved params
57             my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
58
59             # Merge sorting into query attributes
60             $c->dbic_merge_sorting(
61                 {
62                     attributes => $attributes,
63                     params     => $reserved_params
64                 }
65             );
66
67             # Merge pagination into query attributes
68             $c->dbic_merge_pagination(
69                 {
70                     filter => $attributes,
71                     params => $reserved_params
72                 }
73             );
74
75             # Call the to_model function by reference, if defined
76             if ( defined $filtered_params ) {
77
78                 # Apply the mapping function to the passed params
79                 $filtered_params = $to_model->($filtered_params)
80                   if defined $to_model;
81                 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
82             }
83
84             # Perform search
85             my $objects = $objects_set->search( $filtered_params, $attributes );
86
87             if ($objects->is_paged) {
88                 $c->add_pagination_headers({
89                     total => $objects->pager->total_entries,
90                     params => $args,
91                 });
92             }
93
94             my @objects_list = map {
95                 ( defined $to_api )
96                   ? $to_api->( $_->TO_JSON )
97                   : $_->TO_JSON
98             } $objects->as_list;
99
100             return \@objects_list;
101         }
102     );
103 }
104
105 1;