Lines 1-4
Link Here
|
1 |
package Koha::REST::V1::Library; |
1 |
package Koha::REST::V1::Libraries; |
2 |
|
2 |
|
3 |
# This file is part of Koha. |
3 |
# This file is part of Koha. |
4 |
# |
4 |
# |
Lines 45-51
sub list {
Link Here
|
45 |
|
45 |
|
46 |
return try { |
46 |
return try { |
47 |
my $libraries_set = Koha::Libraries->new; |
47 |
my $libraries_set = Koha::Libraries->new; |
48 |
my $libraries = $c->objects->search( $libraries_set, \&_to_model, \&_to_api ); |
48 |
my $libraries = $c->objects->search( $libraries_set ); |
49 |
return $c->render( status => 200, openapi => $libraries ); |
49 |
return $c->render( status => 200, openapi => $libraries ); |
50 |
} |
50 |
} |
51 |
catch { |
51 |
catch { |
Lines 95-101
sub add {
Link Here
|
95 |
my $c = shift->openapi->valid_input or return; |
95 |
my $c = shift->openapi->valid_input or return; |
96 |
|
96 |
|
97 |
return try { |
97 |
return try { |
98 |
my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) ); |
98 |
my $library = Koha::Library->new_from_api( $c->validation->param('body') ); |
99 |
$library->store; |
99 |
$library->store; |
100 |
$c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode ); |
100 |
$c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode ); |
101 |
|
101 |
|
Lines 146-152
sub update {
Link Here
|
146 |
|
146 |
|
147 |
return try { |
147 |
return try { |
148 |
my $params = $c->req->json; |
148 |
my $params = $c->req->json; |
149 |
$library->set( _to_model($params) ); |
149 |
$library->set_from_api( $params ); |
150 |
$library->store(); |
150 |
$library->store(); |
151 |
return $c->render( |
151 |
return $c->render( |
152 |
status => 200, |
152 |
status => 200, |
Lines 203-324
sub delete {
Link Here
|
203 |
}; |
203 |
}; |
204 |
} |
204 |
} |
205 |
|
205 |
|
206 |
=head3 _to_api |
|
|
207 |
|
208 |
Helper function that maps a hashref of Koha::Library attributes into REST api |
209 |
attribute names. |
210 |
|
211 |
=cut |
212 |
|
213 |
sub _to_api { |
214 |
my $library = shift; |
215 |
|
216 |
# Rename attributes |
217 |
foreach my $column ( keys %{ $Koha::REST::V1::Library::to_api_mapping } ) { |
218 |
my $mapped_column = $Koha::REST::V1::Library::to_api_mapping->{$column}; |
219 |
if ( exists $library->{ $column } |
220 |
&& defined $mapped_column ) |
221 |
{ |
222 |
# key /= undef |
223 |
$library->{ $mapped_column } = delete $library->{ $column }; |
224 |
} |
225 |
elsif ( exists $library->{ $column } |
226 |
&& !defined $mapped_column ) |
227 |
{ |
228 |
# key == undef => to be deleted |
229 |
delete $library->{ $column }; |
230 |
} |
231 |
} |
232 |
|
233 |
return $library; |
234 |
} |
235 |
|
236 |
=head3 _to_model |
237 |
|
238 |
Helper function that maps REST api objects into Koha::Library |
239 |
attribute names. |
240 |
|
241 |
=cut |
242 |
|
243 |
sub _to_model { |
244 |
my $library = shift; |
245 |
|
246 |
foreach my $attribute ( keys %{ $Koha::REST::V1::Library::to_model_mapping } ) { |
247 |
my $mapped_attribute = $Koha::REST::V1::Library::to_model_mapping->{$attribute}; |
248 |
if ( exists $library->{ $attribute } |
249 |
&& defined $mapped_attribute ) |
250 |
{ |
251 |
# key /= undef |
252 |
$library->{ $mapped_attribute } = delete $library->{ $attribute }; |
253 |
} |
254 |
elsif ( exists $library->{ $attribute } |
255 |
&& !defined $mapped_attribute ) |
256 |
{ |
257 |
# key == undef => to be deleted |
258 |
delete $library->{ $attribute }; |
259 |
} |
260 |
} |
261 |
|
262 |
if ( exists $library->{pickup_location} ) { |
263 |
$library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0; |
264 |
} |
265 |
|
266 |
return $library; |
267 |
} |
268 |
|
269 |
|
270 |
=head2 Global variables |
271 |
|
272 |
=head3 $to_api_mapping |
273 |
|
274 |
=cut |
275 |
|
276 |
our $to_api_mapping = { |
277 |
branchcode => 'library_id', |
278 |
branchname => 'name', |
279 |
branchaddress1 => 'address1', |
280 |
branchaddress2 => 'address2', |
281 |
branchaddress3 => 'address3', |
282 |
branchzip => 'postal_code', |
283 |
branchcity => 'city', |
284 |
branchstate => 'state', |
285 |
branchcountry => 'country', |
286 |
branchphone => 'phone', |
287 |
branchfax => 'fax', |
288 |
branchemail => 'email', |
289 |
branchreplyto => 'reply_to_email', |
290 |
branchreturnpath => 'return_path_email', |
291 |
branchurl => 'url', |
292 |
issuing => undef, |
293 |
branchip => 'ip', |
294 |
branchprinter => undef, |
295 |
branchnotes => 'notes', |
296 |
marcorgcode => 'marc_org_code', |
297 |
}; |
298 |
|
299 |
=head3 $to_model_mapping |
300 |
|
301 |
=cut |
302 |
|
303 |
our $to_model_mapping = { |
304 |
library_id => 'branchcode', |
305 |
name => 'branchname', |
306 |
address1 => 'branchaddress1', |
307 |
address2 => 'branchaddress2', |
308 |
address3 => 'branchaddress3', |
309 |
postal_code => 'branchzip', |
310 |
city => 'branchcity', |
311 |
state => 'branchstate', |
312 |
country => 'branchcountry', |
313 |
phone => 'branchphone', |
314 |
fax => 'branchfax', |
315 |
email => 'branchemail', |
316 |
reply_to_email => 'branchreplyto', |
317 |
return_path_email => 'branchreturnpath', |
318 |
url => 'branchurl', |
319 |
ip => 'branchip', |
320 |
notes => 'branchnotes', |
321 |
marc_org_code => 'marcorgcode', |
322 |
}; |
323 |
|
324 |
1; |
206 |
1; |