|
Lines 61-199
sub list {
Link Here
|
| 61 |
$orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id }) |
61 |
$orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id }) |
| 62 |
if $order_id; |
62 |
if $order_id; |
| 63 |
|
63 |
|
| 64 |
my $args = $c->validation->output; |
64 |
my @query_fixers; |
| 65 |
my $attributes = {}; |
|
|
| 66 |
|
65 |
|
| 67 |
# Extract reserved params |
|
|
| 68 |
my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); |
| 69 |
# Look for embeds |
66 |
# Look for embeds |
| 70 |
my $embed = $c->stash('koha.embed'); |
67 |
my $embed = $c->stash('koha.embed'); |
| 71 |
my $fixed_embed = clone($embed); |
68 |
if ( exists $embed->{biblio} ) { # asked to embed biblio |
| 72 |
if ( exists $fixed_embed->{biblio} ) { |
69 |
my $fixed_embed = clone($embed); |
| 73 |
# Add biblioitems to prefetch |
70 |
# Add biblioitems to prefetch |
| 74 |
# FIXME remove if we merge biblio + biblioitems |
71 |
# FIXME remove if we merge biblio + biblioitems |
| 75 |
$fixed_embed->{biblio}->{children}->{biblioitem} = {}; |
72 |
$fixed_embed->{biblio}->{children}->{biblioitem} = {}; |
| 76 |
$c->stash('koha.embed', $fixed_embed); |
73 |
$c->stash('koha.embed', $fixed_embed); |
|
|
74 |
push @query_fixers, (sub{ Koha::Biblios->new->api_query_fixer( $_[0], 'biblio', $_[1] ) }); |
| 77 |
} |
75 |
} |
| 78 |
|
76 |
|
| 79 |
if ( exists $reserved_params->{_order_by} ) { |
|
|
| 80 |
# _order_by passed, fix if required |
| 81 |
for my $p ( @{$reserved_params->{_order_by}} ) { |
| 82 |
$p = $c->table_name_fixer($p); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
# Merge sorting into query attributes |
| 87 |
$c->dbic_merge_sorting( |
| 88 |
{ |
| 89 |
attributes => $attributes, |
| 90 |
params => $reserved_params, |
| 91 |
result_set => $orders_rs, |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
# If no pagination parameters are passed, default |
| 96 |
$reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize'); |
| 97 |
$reserved_params->{_page} //= 1; |
| 98 |
|
| 99 |
unless ( $reserved_params->{_per_page} == -1 ) { |
| 100 |
# Merge pagination into query attributes |
| 101 |
$c->dbic_merge_pagination( |
| 102 |
{ |
| 103 |
filter => $attributes, |
| 104 |
params => $reserved_params |
| 105 |
} |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
# Generate prefetches for embedded stuff |
| 110 |
$c->dbic_merge_prefetch( |
| 111 |
{ |
| 112 |
attributes => $attributes, |
| 113 |
result_set => $orders_rs |
| 114 |
} |
| 115 |
); |
| 116 |
|
| 117 |
# Call the to_model function by reference, if defined |
| 118 |
if ( defined $filtered_params ) { |
| 119 |
|
| 120 |
# Apply the mapping function to the passed params |
| 121 |
$filtered_params = $orders_rs->attributes_from_api($filtered_params); |
| 122 |
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( defined $path_params ) { |
| 126 |
|
| 127 |
# Apply the mapping function to the passed params |
| 128 |
$filtered_params //= {}; |
| 129 |
$path_params = $orders_rs->attributes_from_api($path_params); |
| 130 |
foreach my $param (keys %{$path_params}) { |
| 131 |
$filtered_params->{$param} = $path_params->{$param}; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if ( defined $reserved_params->{q} |
| 136 |
|| defined $reserved_params->{query} |
| 137 |
|| defined $reserved_params->{'x-koha-query'} ) |
| 138 |
{ |
| 139 |
|
| 140 |
$filtered_params //={}; |
| 141 |
|
| 142 |
my @query_params_array; |
| 143 |
|
| 144 |
my $json = JSON->new; |
| 145 |
|
| 146 |
# q is defined as multi => JSON::Validator generates an array |
| 147 |
# containing the string |
| 148 |
foreach my $q ( @{ $reserved_params->{q} } ) { |
| 149 |
push @query_params_array, |
| 150 |
$json->decode( $c->table_name_fixer($q) ) |
| 151 |
if $q; # skip if exists but is empty |
| 152 |
} |
| 153 |
|
| 154 |
# x-koha-query contains a string |
| 155 |
push @query_params_array, |
| 156 |
$json->decode( |
| 157 |
$c->table_name_fixer( $reserved_params->{'x-koha-query'} ) ) |
| 158 |
if $reserved_params->{'x-koha-query'}; |
| 159 |
|
| 160 |
# query is already decoded by JSON::Validator at this point |
| 161 |
push @query_params_array, |
| 162 |
$json->decode( |
| 163 |
$c->table_name_fixer( |
| 164 |
$json->encode( $reserved_params->{query} ) |
| 165 |
) |
| 166 |
) if $reserved_params->{query}; |
| 167 |
|
| 168 |
my $query_params; |
| 169 |
|
| 170 |
if ( scalar(@query_params_array) > 1 ) { |
| 171 |
$query_params = { '-and' => \@query_params_array }; |
| 172 |
} |
| 173 |
else { |
| 174 |
$query_params = $query_params_array[0]; |
| 175 |
} |
| 176 |
|
| 177 |
$filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs ); |
| 178 |
} |
| 179 |
|
| 180 |
# Perform search |
| 181 |
my $orders = $orders_rs->search( $filtered_params, $attributes ); |
| 182 |
my $total = $orders_rs->search->count; |
| 183 |
|
| 184 |
$c->add_pagination_headers( |
| 185 |
{ |
| 186 |
base_total => $total, |
| 187 |
page => $reserved_params->{_page}, |
| 188 |
per_page => $reserved_params->{_per_page}, |
| 189 |
query_params => $args, |
| 190 |
total => ( $orders->is_paged ? $orders->pager->total_entries : $orders->count ), |
| 191 |
} |
| 192 |
); |
| 193 |
|
| 194 |
return $c->render( |
77 |
return $c->render( |
| 195 |
status => 200, |
78 |
status => 200, |
| 196 |
openapi => $c->objects->to_api($orders) |
79 |
openapi => $c->objects->search( $orders_rs, \@query_fixers ), |
| 197 |
); |
80 |
); |
| 198 |
} |
81 |
} |
| 199 |
catch { |
82 |
catch { |
| 200 |
- |
|
|