|
Lines 31-37
use Try::Tiny;
Link Here
|
| 31 |
|
31 |
|
| 32 |
=head1 API |
32 |
=head1 API |
| 33 |
|
33 |
|
| 34 |
=head2 Class methods |
34 |
=head2 Methods |
| 35 |
|
35 |
|
| 36 |
=head3 list |
36 |
=head3 list |
| 37 |
|
37 |
|
|
Lines 44-50
sub list {
Link Here
|
| 44 |
|
44 |
|
| 45 |
return try { |
45 |
return try { |
| 46 |
my $holds_set = Koha::Holds->new; |
46 |
my $holds_set = Koha::Holds->new; |
| 47 |
my $holds = $c->objects->search( $holds_set, \&_to_model, \&_to_api ); |
47 |
my $holds = $c->objects->search( $holds_set ); |
| 48 |
return $c->render( status => 200, openapi => $holds ); |
48 |
return $c->render( status => 200, openapi => $holds ); |
| 49 |
} |
49 |
} |
| 50 |
catch { |
50 |
catch { |
|
Lines 194-203
sub add {
Link Here
|
| 194 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
194 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
| 195 |
my $broken_fk = $_->broken_fk; |
195 |
my $broken_fk = $_->broken_fk; |
| 196 |
|
196 |
|
| 197 |
if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Holds::to_api_mapping} ) { |
197 |
if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) { |
| 198 |
$c->render( |
198 |
$c->render( |
| 199 |
status => 404, |
199 |
status => 404, |
| 200 |
openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.' |
200 |
openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.' |
| 201 |
); |
201 |
); |
| 202 |
} |
202 |
} |
| 203 |
else { |
203 |
else { |
|
Lines 403-549
sub update_priority {
Link Here
|
| 403 |
}; |
403 |
}; |
| 404 |
} |
404 |
} |
| 405 |
|
405 |
|
| 406 |
=head2 Internal methods |
|
|
| 407 |
|
| 408 |
=head3 _to_api |
| 409 |
|
| 410 |
Helper function that maps unblessed Koha::Hold objects into REST api |
| 411 |
attribute names. |
| 412 |
|
| 413 |
=cut |
| 414 |
|
| 415 |
sub _to_api { |
| 416 |
my $hold = shift; |
| 417 |
|
| 418 |
# Rename attributes |
| 419 |
foreach my $column ( keys %{ $Koha::REST::V1::Holds::to_api_mapping } ) { |
| 420 |
my $mapped_column = $Koha::REST::V1::Holds::to_api_mapping->{$column}; |
| 421 |
if ( exists $hold->{ $column } |
| 422 |
&& defined $mapped_column ) |
| 423 |
{ |
| 424 |
# key != undef |
| 425 |
$hold->{ $mapped_column } = delete $hold->{ $column }; |
| 426 |
} |
| 427 |
elsif ( exists $hold->{ $column } |
| 428 |
&& !defined $mapped_column ) |
| 429 |
{ |
| 430 |
# key == undef |
| 431 |
delete $hold->{ $column }; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return $hold; |
| 436 |
} |
| 437 |
|
| 438 |
=head3 _to_model |
| 439 |
|
| 440 |
Helper function that maps REST api objects into Koha::Hold |
| 441 |
attribute names. |
| 442 |
|
| 443 |
=cut |
| 444 |
|
| 445 |
sub _to_model { |
| 446 |
my $hold = shift; |
| 447 |
|
| 448 |
foreach my $attribute ( keys %{ $Koha::REST::V1::Holds::to_model_mapping } ) { |
| 449 |
my $mapped_attribute = $Koha::REST::V1::Holds::to_model_mapping->{$attribute}; |
| 450 |
if ( exists $hold->{ $attribute } |
| 451 |
&& defined $mapped_attribute ) |
| 452 |
{ |
| 453 |
# key => !undef |
| 454 |
$hold->{ $mapped_attribute } = delete $hold->{ $attribute }; |
| 455 |
} |
| 456 |
elsif ( exists $hold->{ $attribute } |
| 457 |
&& !defined $mapped_attribute ) |
| 458 |
{ |
| 459 |
# key => undef / to be deleted |
| 460 |
delete $hold->{ $attribute }; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
if ( exists $hold->{lowestPriority} ) { |
| 465 |
$hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0; |
| 466 |
} |
| 467 |
|
| 468 |
if ( exists $hold->{suspend} ) { |
| 469 |
$hold->{suspend} = ($hold->{suspend}) ? 1 : 0; |
| 470 |
} |
| 471 |
|
| 472 |
if ( exists $hold->{reservedate} ) { |
| 473 |
$hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' }); |
| 474 |
} |
| 475 |
|
| 476 |
if ( exists $hold->{cancellationdate} ) { |
| 477 |
$hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' }); |
| 478 |
} |
| 479 |
|
| 480 |
if ( exists $hold->{timestamp} ) { |
| 481 |
$hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' }); |
| 482 |
} |
| 483 |
|
| 484 |
if ( exists $hold->{waitingdate} ) { |
| 485 |
$hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' }); |
| 486 |
} |
| 487 |
|
| 488 |
if ( exists $hold->{expirationdate} ) { |
| 489 |
$hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' }); |
| 490 |
} |
| 491 |
|
| 492 |
if ( exists $hold->{suspend_until} ) { |
| 493 |
$hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' }); |
| 494 |
} |
| 495 |
|
| 496 |
return $hold; |
| 497 |
} |
| 498 |
|
| 499 |
=head2 Global variables |
| 500 |
|
| 501 |
=head3 $to_api_mapping |
| 502 |
|
| 503 |
=cut |
| 504 |
|
| 505 |
our $to_api_mapping = { |
| 506 |
reserve_id => 'hold_id', |
| 507 |
borrowernumber => 'patron_id', |
| 508 |
reservedate => 'hold_date', |
| 509 |
biblionumber => 'biblio_id', |
| 510 |
branchcode => 'pickup_library_id', |
| 511 |
notificationdate => undef, |
| 512 |
reminderdate => undef, |
| 513 |
cancellationdate => 'cancelation_date', |
| 514 |
reservenotes => 'notes', |
| 515 |
found => 'status', |
| 516 |
itemnumber => 'item_id', |
| 517 |
waitingdate => 'waiting_date', |
| 518 |
expirationdate => 'expiration_date', |
| 519 |
lowestPriority => 'lowest_priority', |
| 520 |
suspend => 'suspended', |
| 521 |
suspend_until => 'suspended_until', |
| 522 |
itemtype => 'item_type', |
| 523 |
item_level_hold => 'item_level', |
| 524 |
}; |
| 525 |
|
| 526 |
=head3 $to_model_mapping |
| 527 |
|
| 528 |
=cut |
| 529 |
|
| 530 |
our $to_model_mapping = { |
| 531 |
hold_id => 'reserve_id', |
| 532 |
patron_id => 'borrowernumber', |
| 533 |
hold_date => 'reservedate', |
| 534 |
biblio_id => 'biblionumber', |
| 535 |
pickup_library_id => 'branchcode', |
| 536 |
cancelation_date => 'cancellationdate', |
| 537 |
notes => 'reservenotes', |
| 538 |
status => 'found', |
| 539 |
item_id => 'itemnumber', |
| 540 |
waiting_date => 'waitingdate', |
| 541 |
expiration_date => 'expirationdate', |
| 542 |
lowest_priority => 'lowestPriority', |
| 543 |
suspended => 'suspend', |
| 544 |
suspended_until => 'suspend_until', |
| 545 |
item_type => 'itemtype', |
| 546 |
item_level => 'item_level_hold', |
| 547 |
}; |
| 548 |
|
| 549 |
1; |
406 |
1; |
| 550 |
- |
|
|