Bug 26143: Make the API handle per_page=-1
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 use JSON;
23
24 =head1 NAME
25
26 Koha::REST::Plugin::Objects
27
28 =head1 API
29
30 =head2 Helper methods
31
32 =head3 objects.search
33
34     my $patrons_rs = Koha::Patrons->new;
35     my $patrons = $c->objects->search( $patrons_rs );
36
37 Performs a database search using given Koha::Objects object and query parameters.
38
39 Returns an arrayref of the hashrefs representing the resulting objects
40 for API rendering.
41
42 =cut
43
44 sub register {
45     my ( $self, $app ) = @_;
46
47     $app->helper(
48         'objects.search' => sub {
49             my ( $c, $result_set ) = @_;
50
51             my $args = $c->validation->output;
52             my $attributes = {};
53
54             # Extract reserved params
55             my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
56             # Look for embeds
57             my $embed = $c->stash('koha.embed');
58
59             # Merge sorting into query attributes
60             $c->dbic_merge_sorting(
61                 {
62                     attributes => $attributes,
63                     params     => $reserved_params,
64                     result_set => $result_set
65                 }
66             );
67
68             # If no pagination parameters are passed, default
69             $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
70             $reserved_params->{_page}     //= 1;
71
72             unless ( $reserved_params->{_per_page} == -1 ) {
73                 # Merge pagination into query attributes
74                 $c->dbic_merge_pagination(
75                     {
76                         filter => $attributes,
77                         params => $reserved_params
78                     }
79                 );
80             }
81
82             # Generate prefetches for embedded stuff
83             $c->dbic_merge_prefetch(
84                 {
85                     attributes => $attributes,
86                     result_set => $result_set
87                 }
88             );
89
90             # Call the to_model function by reference, if defined
91             if ( defined $filtered_params ) {
92
93                 # Apply the mapping function to the passed params
94                 $filtered_params = $result_set->attributes_from_api($filtered_params);
95                 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
96             }
97
98             if ( defined $path_params ) {
99
100                 # Apply the mapping function to the passed params
101                 $filtered_params //= {};
102                 $path_params = $result_set->attributes_from_api($path_params);
103                 foreach my $param (keys %{$path_params}) {
104                     $filtered_params->{$param} = $path_params->{$param};
105                 }
106             }
107
108             if( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
109                 $filtered_params //={};
110                 my @query_params_array;
111                 my $query_params;
112                 push @query_params_array, $reserved_params->{query} if defined $reserved_params->{query};
113                 my $json = JSON->new;
114                 push @query_params_array, $json->decode($reserved_params->{q}) if defined $reserved_params->{q};
115                 push @query_params_array, $json->decode($reserved_params->{'x-koha-query'}) if defined $reserved_params->{'x-koha-query'};
116
117                 if(scalar(@query_params_array) > 1) {
118                     $query_params = {'-and' => \@query_params_array};
119                 } else {
120                     $query_params = $query_params_array[0];
121                 }
122
123                 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set );
124             }
125             # Perform search
126             my $objects = $result_set->search( $filtered_params, $attributes );
127
128             if ($objects->is_paged) {
129                 $c->add_pagination_headers({
130                     total => $objects->pager->total_entries,
131                     params => $args,
132                 });
133             }
134             else {
135                 $c->add_pagination_headers({
136                     total => $objects->count,
137                     params => $args,
138                 });
139             }
140
141             return $objects->to_api({ embed => $embed });
142         }
143     );
144 }
145
146 1;