Lines 38-47
sub register {
Link Here
|
38 |
|
38 |
|
39 |
my $patrons_rs = Koha::Patrons->new; |
39 |
my $patrons_rs = Koha::Patrons->new; |
40 |
my $patrons = $c->objects->find( $patrons_rs, $id ); |
40 |
my $patrons = $c->objects->find( $patrons_rs, $id ); |
|
|
41 |
. . . |
42 |
return $c->render({ status => 200, openapi => $patron }); |
41 |
|
43 |
|
42 |
Performs a database search using given Koha::Objects object and the $id. |
44 |
Performs a database search using given Koha::Objects object and the $id. |
43 |
|
45 |
|
44 |
Returns I<undef> if no object is found Returns the I<API representation> of |
46 |
Returns I<undef> if no object is found or the I<API representation> of |
45 |
the requested object. It passes through any embeds if specified. |
47 |
the requested object. It passes through any embeds if specified. |
46 |
|
48 |
|
47 |
=cut |
49 |
=cut |
Lines 49-60
the requested object. It passes through any embeds if specified.
Link Here
|
49 |
$app->helper( |
51 |
$app->helper( |
50 |
'objects.find' => sub { |
52 |
'objects.find' => sub { |
51 |
my ( $c, $result_set, $id ) = @_; |
53 |
my ( $c, $result_set, $id ) = @_; |
|
|
54 |
my $object = $c->objects->find_rs( $result_set, $id ); |
55 |
return unless $object; |
56 |
return $c->objects->to_api($object); |
57 |
} |
58 |
); |
52 |
|
59 |
|
53 |
my $attributes = {}; |
|
|
54 |
|
60 |
|
55 |
# Look for embeds |
61 |
=head3 objects.find_rs |
56 |
my $embed = $c->stash('koha.embed'); |
62 |
|
57 |
my $strings = $c->stash('koha.strings'); |
63 |
my $patrons_rs = Koha::Patrons->new; |
|
|
64 |
my $patron_rs = $c->objects->find_rs( $patrons_rs, $id ); |
65 |
. . . |
66 |
return $c->render({ status => 200, openapi => $patron_rs->to_api }); |
67 |
|
68 |
Returns the passed Koha::Objects resultset filtered to the passed $id and |
69 |
with any embeds requested by the api applied. |
70 |
|
71 |
The resultset can then be used for further processing. |
72 |
|
73 |
=cut |
74 |
|
75 |
$app->helper( |
76 |
'objects.find_rs' => sub { |
77 |
my ( $c, $result_set, $id ) = @_; |
78 |
|
79 |
my $attributes = {}; |
58 |
|
80 |
|
59 |
# Generate prefetches for embedded stuff |
81 |
# Generate prefetches for embedded stuff |
60 |
$c->dbic_merge_prefetch( |
82 |
$c->dbic_merge_prefetch( |
Lines 66-74
the requested object. It passes through any embeds if specified.
Link Here
|
66 |
|
88 |
|
67 |
my $object = $result_set->find( $id, $attributes ); |
89 |
my $object = $result_set->find( $id, $attributes ); |
68 |
|
90 |
|
69 |
return unless $object; |
91 |
return $object; |
70 |
|
|
|
71 |
return $object->to_api({ embed => $embed, strings => $strings }); |
72 |
} |
92 |
} |
73 |
); |
93 |
); |
74 |
|
94 |
|
Lines 76-83
the requested object. It passes through any embeds if specified.
Link Here
|
76 |
|
96 |
|
77 |
my $patrons_rs = Koha::Patrons->new; |
97 |
my $patrons_rs = Koha::Patrons->new; |
78 |
my $patrons = $c->objects->search( $patrons_rs ); |
98 |
my $patrons = $c->objects->search( $patrons_rs ); |
|
|
99 |
. . . |
100 |
return $c->render({ status => 200, openapi => $patrons }); |
79 |
|
101 |
|
80 |
Performs a database search using given Koha::Objects object and query parameters. |
102 |
Performs a database search using given Koha::Objects object with any api |
|
|
103 |
query parameters applied. |
81 |
|
104 |
|
82 |
Returns an arrayref of the hashrefs representing the resulting objects |
105 |
Returns an arrayref of the hashrefs representing the resulting objects |
83 |
for API rendering. |
106 |
for API rendering. |
Lines 91-106
shouldn't be called twice in it.
Link Here
|
91 |
'objects.search' => sub { |
114 |
'objects.search' => sub { |
92 |
my ( $c, $result_set ) = @_; |
115 |
my ( $c, $result_set ) = @_; |
93 |
|
116 |
|
94 |
my $args = $c->validation->output; |
117 |
return $c->objects->to_api( $c->objects->search_rs($result_set) ); |
|
|
118 |
} |
119 |
); |
120 |
|
121 |
=head3 objects.search_rs |
122 |
|
123 |
my $patrons_rs = Koha::Patrons->new; |
124 |
my $patrons_rs = $c->objects->search_rs( $patrons_rs ); |
125 |
. . . |
126 |
return $c->render({ status => 200, openapi => $patrons_rs->to_api }); |
127 |
|
128 |
Returns the passed Koha::Objects resultset filtered as requested by the api query |
129 |
parameters and with requested embeds applied. |
130 |
|
131 |
Warning: this helper adds pagination headers to the calling controller, and thus |
132 |
shouldn't be called twice in it. |
133 |
|
134 |
=cut |
135 |
|
136 |
$app->helper( |
137 |
'objects.search_rs' => sub { |
138 |
my ( $c, $result_set ) = @_; |
139 |
|
140 |
my $args = $c->validation->output; |
95 |
my $attributes = {}; |
141 |
my $attributes = {}; |
96 |
|
142 |
|
97 |
# Extract reserved params |
143 |
# Extract reserved params |
98 |
my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); |
144 |
my ( $filtered_params, $reserved_params, $path_params ) = |
99 |
# Privileged reques? |
145 |
$c->extract_reserved_params($args); |
100 |
my $is_public = $c->stash('is_public'); |
|
|
101 |
# Look for embeds |
102 |
my $embed = $c->stash('koha.embed'); |
103 |
my $strings = $c->stash('koha.strings'); |
104 |
|
146 |
|
105 |
# Merge sorting into query attributes |
147 |
# Merge sorting into query attributes |
106 |
$c->dbic_merge_sorting( |
148 |
$c->dbic_merge_sorting( |
Lines 112-121
shouldn't be called twice in it.
Link Here
|
112 |
); |
154 |
); |
113 |
|
155 |
|
114 |
# If no pagination parameters are passed, default |
156 |
# If no pagination parameters are passed, default |
115 |
$reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize'); |
157 |
$reserved_params->{_per_page} //= |
116 |
$reserved_params->{_page} //= 1; |
158 |
C4::Context->preference('RESTdefaultPageSize'); |
|
|
159 |
$reserved_params->{_page} //= 1; |
117 |
|
160 |
|
118 |
unless ( $reserved_params->{_per_page} == -1 ) { |
161 |
unless ( $reserved_params->{_per_page} == -1 ) { |
|
|
162 |
|
119 |
# Merge pagination into query attributes |
163 |
# Merge pagination into query attributes |
120 |
$c->dbic_merge_pagination( |
164 |
$c->dbic_merge_pagination( |
121 |
{ |
165 |
{ |
Lines 137-144
shouldn't be called twice in it.
Link Here
|
137 |
if ( defined $filtered_params ) { |
181 |
if ( defined $filtered_params ) { |
138 |
|
182 |
|
139 |
# Apply the mapping function to the passed params |
183 |
# Apply the mapping function to the passed params |
140 |
$filtered_params = $result_set->attributes_from_api($filtered_params); |
184 |
$filtered_params = |
141 |
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); |
185 |
$result_set->attributes_from_api($filtered_params); |
|
|
186 |
$filtered_params = |
187 |
$c->build_query_params( $filtered_params, $reserved_params ); |
142 |
} |
188 |
} |
143 |
|
189 |
|
144 |
if ( defined $reserved_params->{q} |
190 |
if ( defined $reserved_params->{q} |
Lines 155-172
shouldn't be called twice in it.
Link Here
|
155 |
|
201 |
|
156 |
my $json = JSON->new; |
202 |
my $json = JSON->new; |
157 |
|
203 |
|
158 |
if ( ref($reserved_params->{q}) eq 'ARRAY' ) { |
204 |
if ( ref( $reserved_params->{q} ) eq 'ARRAY' ) { |
159 |
# q is defined as multi => JSON::Validator generates an array |
205 |
|
|
|
206 |
# q is defined as multi => JSON::Validator generates an array |
160 |
foreach my $q ( @{ $reserved_params->{q} } ) { |
207 |
foreach my $q ( @{ $reserved_params->{q} } ) { |
161 |
push @query_params_array, $json->decode($q) |
208 |
push @query_params_array, $json->decode($q) |
162 |
if $q; # skip if exists but is empty |
209 |
if $q; # skip if exists but is empty |
163 |
} |
210 |
} |
164 |
} |
211 |
} |
165 |
else { |
212 |
else { |
166 |
# objects.search called outside OpenAPI context |
213 |
# objects.search called outside OpenAPI context |
167 |
# might be a hashref |
214 |
# might be a hashref |
168 |
push @query_params_array, $json->decode($reserved_params->{q}) |
215 |
push @query_params_array, |
169 |
if $reserved_params->{q}; |
216 |
$json->decode( $reserved_params->{q} ) |
|
|
217 |
if $reserved_params->{q}; |
170 |
} |
218 |
} |
171 |
|
219 |
|
172 |
push @query_params_array, |
220 |
push @query_params_array, |
Lines 182-197
shouldn't be called twice in it.
Link Here
|
182 |
$query_params = $query_params_array[0]; |
230 |
$query_params = $query_params_array[0]; |
183 |
} |
231 |
} |
184 |
|
232 |
|
185 |
$filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set ); |
233 |
$filtered_params = |
|
|
234 |
$c->merge_q_params( $filtered_params, $query_params, |
235 |
$result_set ); |
186 |
} |
236 |
} |
187 |
|
237 |
|
188 |
# request sequence id (i.e. 'draw' Datatables parameter) |
238 |
# request sequence id (i.e. 'draw' Datatables parameter) |
189 |
$c->res->headers->add( 'x-koha-request-id' => $reserved_params->{'x-koha-request-id'} ) |
239 |
$c->res->headers->add( |
|
|
240 |
'x-koha-request-id' => $reserved_params->{'x-koha-request-id'} ) |
190 |
if $reserved_params->{'x-koha-request-id'}; |
241 |
if $reserved_params->{'x-koha-request-id'}; |
191 |
|
242 |
|
192 |
# If search_limited exists, use it |
243 |
# If search_limited exists, use it |
193 |
$result_set = $result_set->search_limited, |
244 |
$result_set = $result_set->search_limited, |
194 |
if $result_set->can('search_limited'); |
245 |
if $result_set->can('search_limited'); |
195 |
|
246 |
|
196 |
# Perform search |
247 |
# Perform search |
197 |
my $objects = $result_set->search( $filtered_params, $attributes ); |
248 |
my $objects = $result_set->search( $filtered_params, $attributes ); |
Lines 199-211
shouldn't be called twice in it.
Link Here
|
199 |
|
250 |
|
200 |
$c->add_pagination_headers( |
251 |
$c->add_pagination_headers( |
201 |
{ |
252 |
{ |
202 |
total => ($objects->is_paged ? $objects->pager->total_entries : $objects->count), |
253 |
total => ( |
|
|
254 |
$objects->is_paged |
255 |
? $objects->pager->total_entries |
256 |
: $objects->count |
257 |
), |
203 |
base_total => $total, |
258 |
base_total => $total, |
204 |
params => $args, |
259 |
params => $args, |
205 |
} |
260 |
} |
206 |
); |
261 |
); |
207 |
|
262 |
|
208 |
return $objects->to_api({ embed => $embed, public => $is_public, strings => $strings }); |
263 |
return $objects; |
|
|
264 |
} |
265 |
); |
266 |
|
267 |
=head3 objects.to_api |
268 |
|
269 |
my $patrons_rs = Koha::Patrons->new; |
270 |
my $api_representation = $c->objects->to_api( $patrons_rs ); |
271 |
|
272 |
Returns the API representation of the passed resultset. |
273 |
|
274 |
=cut |
275 |
|
276 |
$app->helper( |
277 |
'objects.to_api' => sub { |
278 |
my ( $c, $object ) = @_; |
279 |
|
280 |
# Privileged request? |
281 |
my $public = $c->stash('is_public'); |
282 |
|
283 |
# Look for embeds |
284 |
my $embed = $c->stash('koha.embed'); |
285 |
my $strings = $c->stash('koha.strings'); |
286 |
|
287 |
return $object->to_api( |
288 |
{ |
289 |
embed => $embed, |
290 |
public => $public, |
291 |
strings => $strings |
292 |
} |
293 |
); |
209 |
} |
294 |
} |
210 |
); |
295 |
); |
211 |
} |
296 |
} |