|
Lines 45-60
List Koha::Checkout objects
Link Here
|
| 45 |
|
45 |
|
| 46 |
sub list { |
46 |
sub list { |
| 47 |
my $c = shift->openapi->valid_input or return; |
47 |
my $c = shift->openapi->valid_input or return; |
|
|
48 |
|
| 48 |
my $checked_in = $c->validation->param('checked_in'); |
49 |
my $checked_in = $c->validation->param('checked_in'); |
|
|
50 |
|
| 49 |
try { |
51 |
try { |
| 50 |
my $checkouts_set; |
52 |
my $checkouts_set; |
|
|
53 |
|
| 51 |
if ( $checked_in ) { |
54 |
if ( $checked_in ) { |
| 52 |
$checkouts_set = Koha::Old::Checkouts->new; |
55 |
$checkouts_set = Koha::Old::Checkouts->new; |
| 53 |
} else { |
56 |
} else { |
| 54 |
$checkouts_set = Koha::Checkouts->new; |
57 |
$checkouts_set = Koha::Checkouts->new; |
| 55 |
} |
58 |
} |
| 56 |
my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api ); |
59 |
|
| 57 |
return $c->render( status => 200, openapi => $checkouts ); |
60 |
my $args = $c->validation->output; |
|
|
61 |
my $attributes = {}; |
| 62 |
|
| 63 |
# Extract reserved params |
| 64 |
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args); |
| 65 |
|
| 66 |
# Merge sorting into query attributes |
| 67 |
$c->dbic_merge_sorting( |
| 68 |
{ |
| 69 |
attributes => $attributes, |
| 70 |
params => $reserved_params, |
| 71 |
result_set => $checkouts_set |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
# Merge pagination into query attributes |
| 76 |
$c->dbic_merge_pagination( |
| 77 |
{ |
| 78 |
filter => $attributes, |
| 79 |
params => $reserved_params |
| 80 |
} |
| 81 |
); |
| 82 |
|
| 83 |
# Call the to_model function by reference, if defined |
| 84 |
if ( defined $filtered_params ) { |
| 85 |
# remove checked_in |
| 86 |
delete $filtered_params->{checked_in}; |
| 87 |
# Apply the mapping function to the passed params |
| 88 |
$filtered_params = $checkouts_set->attributes_from_api($filtered_params); |
| 89 |
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); |
| 90 |
} |
| 91 |
|
| 92 |
# Perform search |
| 93 |
my $checkouts = $checkouts_set->search( $filtered_params, $attributes ); |
| 94 |
|
| 95 |
if ($checkouts->is_paged) { |
| 96 |
$c->add_pagination_headers({ |
| 97 |
total => $checkouts->pager->total_entries, |
| 98 |
params => $args, |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
return $c->render( status => 200, openapi => $checkouts->to_api ); |
| 58 |
} catch { |
103 |
} catch { |
| 59 |
if ( $_->isa('DBIx::Class::Exception') ) { |
104 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 60 |
return $c->render( |
105 |
return $c->render( |
|
Lines 182-267
sub allows_renewal {
Link Here
|
| 182 |
); |
227 |
); |
| 183 |
} |
228 |
} |
| 184 |
|
229 |
|
| 185 |
=head3 _to_api |
|
|
| 186 |
|
| 187 |
Helper function that maps a hashref of Koha::Checkout attributes into REST api |
| 188 |
attribute names. |
| 189 |
|
| 190 |
=cut |
| 191 |
|
| 192 |
sub _to_api { |
| 193 |
my $checkout = shift; |
| 194 |
|
| 195 |
foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) { |
| 196 |
my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column}; |
| 197 |
if ( exists $checkout->{ $column } && defined $mapped_column ) |
| 198 |
{ |
| 199 |
$checkout->{ $mapped_column } = delete $checkout->{ $column }; |
| 200 |
} |
| 201 |
elsif ( exists $checkout->{ $column } && !defined $mapped_column ) { |
| 202 |
delete $checkout->{ $column }; |
| 203 |
} |
| 204 |
} |
| 205 |
return $checkout; |
| 206 |
} |
| 207 |
|
| 208 |
=head3 _to_model |
| 209 |
|
| 210 |
Helper function that maps REST api objects into Koha::Checkouts |
| 211 |
attribute names. |
| 212 |
|
| 213 |
=cut |
| 214 |
|
| 215 |
sub _to_model { |
| 216 |
my $checkout = shift; |
| 217 |
|
| 218 |
foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) { |
| 219 |
my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute}; |
| 220 |
if ( exists $checkout->{ $attribute } && defined $mapped_attribute ) |
| 221 |
{ |
| 222 |
$checkout->{ $mapped_attribute } = delete $checkout->{ $attribute }; |
| 223 |
} |
| 224 |
elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute ) |
| 225 |
{ |
| 226 |
delete $checkout->{ $attribute }; |
| 227 |
} |
| 228 |
} |
| 229 |
return $checkout; |
| 230 |
} |
| 231 |
|
| 232 |
=head2 Global variables |
| 233 |
|
| 234 |
=head3 $to_api_mapping |
| 235 |
|
| 236 |
=cut |
| 237 |
|
| 238 |
our $to_api_mapping = { |
| 239 |
issue_id => 'checkout_id', |
| 240 |
borrowernumber => 'patron_id', |
| 241 |
itemnumber => 'item_id', |
| 242 |
date_due => 'due_date', |
| 243 |
branchcode => 'library_id', |
| 244 |
returndate => 'checkin_date', |
| 245 |
lastreneweddate => 'last_renewed_date', |
| 246 |
issuedate => 'checkout_date', |
| 247 |
notedate => 'note_date', |
| 248 |
}; |
| 249 |
|
| 250 |
=head3 $to_model_mapping |
| 251 |
|
| 252 |
=cut |
| 253 |
|
| 254 |
our $to_model_mapping = { |
| 255 |
checkout_id => 'issue_id', |
| 256 |
patron_id => 'borrowernumber', |
| 257 |
item_id => 'itemnumber', |
| 258 |
due_date => 'date_due', |
| 259 |
library_id => 'branchcode', |
| 260 |
checkin_date => 'returndate', |
| 261 |
last_renewed_date => 'lastreneweddate', |
| 262 |
checkout_date => 'issuedate', |
| 263 |
note_date => 'notedate', |
| 264 |
checked_in => undef, |
| 265 |
}; |
| 266 |
|
| 267 |
1; |
230 |
1; |
| 268 |
- |
|
|