Lines 22-27
use Mojo::Base 'Mojolicious::Controller';
Link Here
|
22 |
use Koha::Acquisition::Orders; |
22 |
use Koha::Acquisition::Orders; |
23 |
use Koha::DateUtils; |
23 |
use Koha::DateUtils; |
24 |
|
24 |
|
|
|
25 |
use Clone 'clone'; |
26 |
use JSON qw(decode_json); |
25 |
use Scalar::Util qw( blessed ); |
27 |
use Scalar::Util qw( blessed ); |
26 |
use Try::Tiny; |
28 |
use Try::Tiny; |
27 |
|
29 |
|
Lines 44-54
sub list {
Link Here
|
44 |
my $c = shift->openapi->valid_input or return; |
46 |
my $c = shift->openapi->valid_input or return; |
45 |
|
47 |
|
46 |
return try { |
48 |
return try { |
47 |
my $orders = $c->objects->search( Koha::Acquisition::Orders->new ); |
49 |
|
|
|
50 |
my $only_active = delete $c->validation->output->{only_active}; |
51 |
my $order_id = delete $c->validation->output->{order_id}; |
52 |
|
53 |
my $orders_rs; |
54 |
|
55 |
if ( $only_active ) { |
56 |
$orders_rs = Koha::Acquisition::Orders->filter_by_active; |
57 |
} |
58 |
else { |
59 |
$orders_rs = Koha::Acquisition::Orders->new; |
60 |
} |
61 |
|
62 |
$orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id }) |
63 |
if $order_id; |
64 |
|
65 |
my $args = $c->validation->output; |
66 |
my $attributes = {}; |
67 |
|
68 |
# Extract reserved params |
69 |
my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); |
70 |
# Look for embeds |
71 |
my $embed = $c->stash('koha.embed'); |
72 |
my $fixed_embed = clone($embed); |
73 |
if ( exists $fixed_embed->{biblio} ) { |
74 |
# Add biblioitems to prefetch |
75 |
# FIXME remove if we merge biblio + biblioitems |
76 |
$fixed_embed->{biblio}->{children}->{biblioitem} = {}; |
77 |
$c->stash('koha.embed', $fixed_embed); |
78 |
} |
79 |
|
80 |
# If no pagination parameters are passed, default |
81 |
$reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize'); |
82 |
$reserved_params->{_page} //= 1; |
83 |
|
84 |
unless ( $reserved_params->{_per_page} == -1 ) { |
85 |
# Merge pagination into query attributes |
86 |
$c->dbic_merge_pagination( |
87 |
{ |
88 |
filter => $attributes, |
89 |
params => $reserved_params |
90 |
} |
91 |
); |
92 |
} |
93 |
|
94 |
# Generate prefetches for embedded stuff |
95 |
$c->dbic_merge_prefetch( |
96 |
{ |
97 |
attributes => $attributes, |
98 |
result_set => $orders_rs |
99 |
} |
100 |
); |
101 |
|
102 |
# Call the to_model function by reference, if defined |
103 |
if ( defined $filtered_params ) { |
104 |
|
105 |
# Apply the mapping function to the passed params |
106 |
$filtered_params = $orders_rs->attributes_from_api($filtered_params); |
107 |
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); |
108 |
} |
109 |
|
110 |
if ( defined $path_params ) { |
111 |
|
112 |
# Apply the mapping function to the passed params |
113 |
$filtered_params //= {}; |
114 |
$path_params = $orders_rs->attributes_from_api($path_params); |
115 |
foreach my $param (keys %{$path_params}) { |
116 |
$filtered_params->{$param} = $path_params->{$param}; |
117 |
} |
118 |
} |
119 |
|
120 |
if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) { |
121 |
$filtered_params //={}; |
122 |
my @query_params_array; |
123 |
my $query_params; |
124 |
if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) { |
125 |
push @query_params_array, fix_query({ query => $reserved_params->{query} }); |
126 |
} |
127 |
if ( exists $reserved_params->{q} and defined $reserved_params->{q}) { |
128 |
push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) }); |
129 |
} |
130 |
if ( exists $reserved_params->{'x-koha-query'} and defined $reserved_params->{'x-koha-query'} ) { |
131 |
push @query_params_array, fix_query({ query => decode_json($reserved_params->{'x-koha-query'}) });; |
132 |
} |
133 |
|
134 |
if(scalar(@query_params_array) > 1) { |
135 |
$query_params = {'-and' => \@query_params_array}; |
136 |
} |
137 |
else { |
138 |
$query_params = $query_params_array[0]; |
139 |
} |
140 |
|
141 |
$filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs ); |
142 |
} |
143 |
|
144 |
# Perform search |
145 |
my $orders = $orders_rs->search( $filtered_params, $attributes ); |
146 |
|
147 |
if ($orders->is_paged) { |
148 |
$c->add_pagination_headers({ |
149 |
total => $orders->pager->total_entries, |
150 |
params => $args, |
151 |
}); |
152 |
} |
153 |
else { |
154 |
$c->add_pagination_headers({ |
155 |
total => $orders->count, |
156 |
params => $args, |
157 |
}); |
158 |
} |
48 |
|
159 |
|
49 |
return $c->render( |
160 |
return $c->render( |
50 |
status => 200, |
161 |
status => 200, |
51 |
openapi => $orders |
162 |
openapi => $orders->to_api({ embed => $embed }) |
52 |
); |
163 |
); |
53 |
} |
164 |
} |
54 |
catch { |
165 |
catch { |
Lines 185-188
sub delete {
Link Here
|
185 |
}; |
296 |
}; |
186 |
} |
297 |
} |
187 |
|
298 |
|
|
|
299 |
=head2 Internal methods |
300 |
|
301 |
=head3 fix_query |
302 |
|
303 |
my $query = fix_query($query); |
304 |
|
305 |
This method takes care of recursively fixing queries that should be done |
306 |
against biblioitems (instead if biblio as exposed on the API) |
307 |
|
308 |
=cut |
309 |
|
310 |
sub fix_query { |
311 |
my ($args) = @_; |
312 |
|
313 |
my $query = $args->{query}; |
314 |
my $biblioitem_fields = { |
315 |
'biblio.isbn' => 'biblio.biblioitem.isbn', |
316 |
'biblio.ean' => 'biblio.biblioitem.ean' |
317 |
}; |
318 |
|
319 |
if ( ref($query) eq 'HASH' ) { |
320 |
foreach my $key (keys %{$query}) { |
321 |
if ( exists $biblioitem_fields->{$key}) { |
322 |
my $subq = delete $query->{$key}; |
323 |
$query->{$biblioitem_fields->{$key}} = (ref($subq) eq 'HASH') |
324 |
? fix_query({ query => $subq }) |
325 |
: $subq; |
326 |
} |
327 |
else { |
328 |
$query->{$key} = fix_query({ query => $query->{$key} }); |
329 |
} |
330 |
} |
331 |
} |
332 |
elsif ( ref($query) eq 'ARRAY' ) { |
333 |
my @accum; |
334 |
foreach my $item (@{$query}) { |
335 |
push @accum, fix_query({ query => $item }); |
336 |
} |
337 |
$query = \@accum; |
338 |
} |
339 |
else { # scalar |
340 |
$query = $biblioitem_fields->{$query} |
341 |
if exists $biblioitem_fields->{$query}; |
342 |
} |
343 |
|
344 |
return $query; |
345 |
} |
346 |
|
188 |
1; |
347 |
1; |