Bug 26635: Move expand syntax to x-koha-embed
[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 =cut
33
34 sub register {
35     my ( $self, $app ) = @_;
36
37 =head3 objects.find
38
39     my $patrons_rs = Koha::Patrons->new;
40     my $patrons = $c->objects->find( $patrons_rs, $id );
41
42 Performs a database search using given Koha::Objects object and the $id.
43
44 Returns I<undef> if no object is found Returns the I<API representation> of
45 the requested object. It passes through any embeds if specified.
46
47 =cut
48
49     $app->helper(
50         'objects.find' => sub {
51             my ( $c, $result_set, $id ) = @_;
52
53             my $attributes = {};
54
55             # Look for embeds
56             my $embed     = $c->stash('koha.embed');
57             my $av_expand = $c->stash('koha.av_expand');
58
59             # Generate prefetches for embedded stuff
60             $c->dbic_merge_prefetch(
61                 {
62                     attributes => $attributes,
63                     result_set => $result_set
64                 }
65             );
66
67             my $object = $result_set->find( $id, $attributes );
68
69             return unless $object;
70
71             return $object->to_api({ embed => $embed, av_expand => $av_expand });
72         }
73     );
74
75 =head3 objects.search
76
77     my $patrons_rs = Koha::Patrons->new;
78     my $patrons = $c->objects->search( $patrons_rs );
79
80 Performs a database search using given Koha::Objects object and query parameters.
81
82 Returns an arrayref of the hashrefs representing the resulting objects
83 for API rendering.
84
85 Warning: this helper adds pagination headers to the calling controller, and thus
86 shouldn't be called twice in it.
87
88 =cut
89
90     $app->helper(
91         'objects.search' => sub {
92             my ( $c, $result_set ) = @_;
93
94             my $args = $c->validation->output;
95             my $attributes = {};
96
97             # Extract reserved params
98             my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
99             # Privileged reques?
100             my $is_public = $c->stash('is_public');
101             # Look for embeds
102             my $embed     = $c->stash('koha.embed');
103             my $av_expand = $c->stash('koha.av_expand');
104
105             # Merge sorting into query attributes
106             $c->dbic_merge_sorting(
107                 {
108                     attributes => $attributes,
109                     params     => $reserved_params,
110                     result_set => $result_set
111                 }
112             );
113
114             # If no pagination parameters are passed, default
115             $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
116             $reserved_params->{_page}     //= 1;
117
118             unless ( $reserved_params->{_per_page} == -1 ) {
119                 # Merge pagination into query attributes
120                 $c->dbic_merge_pagination(
121                     {
122                         filter => $attributes,
123                         params => $reserved_params
124                     }
125                 );
126             }
127
128             # Generate prefetches for embedded stuff
129             $c->dbic_merge_prefetch(
130                 {
131                     attributes => $attributes,
132                     result_set => $result_set
133                 }
134             );
135
136             # Call the to_model function by reference, if defined
137             if ( defined $filtered_params ) {
138
139                 # Apply the mapping function to the passed params
140                 $filtered_params = $result_set->attributes_from_api($filtered_params);
141                 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
142             }
143
144             if (   defined $reserved_params->{q}
145                 || defined $reserved_params->{query}
146                 || defined $reserved_params->{'x-koha-query'} )
147             {
148                 $filtered_params //= {};
149
150                 my @query_params_array;
151
152                 # query in request body, JSON::Validator already decoded it
153                 push @query_params_array, $reserved_params->{query}
154                   if defined $reserved_params->{query};
155
156                 my $json = JSON->new;
157
158                 if ( ref($reserved_params->{q}) eq 'ARRAY' ) {
159                     # q is defined as multi => JSON::Validator generates an array
160                     foreach my $q ( @{ $reserved_params->{q} } ) {
161                         push @query_params_array, $json->decode($q)
162                         if $q; # skip if exists but is empty
163                     }
164                 }
165                 else {
166                     # objects.search called outside OpenAPI context
167                     # might be a hashref
168                     push @query_params_array, $json->decode($reserved_params->{q})
169                         if $reserved_params->{q};
170                 }
171
172                 push @query_params_array,
173                   $json->decode( $reserved_params->{'x-koha-query'} )
174                   if defined $reserved_params->{'x-koha-query'};
175
176                 my $query_params;
177
178                 if ( scalar(@query_params_array) > 1 ) {
179                     $query_params = { '-and' => \@query_params_array };
180                 }
181                 else {
182                     $query_params = $query_params_array[0];
183                 }
184
185                 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set );
186             }
187
188             # request sequence id (i.e. 'draw' Datatables parameter)
189             $c->res->headers->add( 'x-koha-request-id' => $reserved_params->{'x-koha-request-id'} )
190               if $reserved_params->{'x-koha-request-id'};
191
192             # If search_limited exists, use it
193             $result_set = $result_set->search_limited,
194                 if $result_set->can('search_limited');
195
196             # Perform search
197             my $objects = $result_set->search( $filtered_params, $attributes );
198             my $total   = $result_set->search->count;
199
200             $c->add_pagination_headers(
201                 {
202                     total      => ($objects->is_paged ? $objects->pager->total_entries : $objects->count),
203                     base_total => $total,
204                     params     => $args,
205                 }
206             );
207
208             return $objects->to_api({ embed => $embed, public => $is_public, av_expand => $av_expand });
209         }
210     );
211 }
212
213 1;