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 |
my $orders_rs = Koha::Acquisition::Orders->new; |
|
|
50 |
my $args = $c->validation->output; |
51 |
my $attributes = {}; |
52 |
|
53 |
# Extract reserved params |
54 |
my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); |
55 |
# Look for embeds |
56 |
my $embed = $c->stash('koha.embed'); |
57 |
my $fixed_embed = clone($embed); |
58 |
if ( exists $fixed_embed->{biblio} ) { |
59 |
# Add biblioitems to prefetch |
60 |
# FIXME remove if we merge biblio + biblioitems |
61 |
$fixed_embed->{biblio}->{children}->{biblioitem} = {}; |
62 |
$c->stash('koha.embed', $fixed_embed); |
63 |
} |
64 |
|
65 |
# Merge sorting into query attributes |
66 |
$c->dbic_merge_sorting( |
67 |
{ |
68 |
attributes => $attributes, |
69 |
params => $reserved_params, |
70 |
result_set => $orders_rs |
71 |
} |
72 |
); |
73 |
|
74 |
# Merge pagination into query attributes |
75 |
$c->dbic_merge_pagination( |
76 |
{ |
77 |
filter => $attributes, |
78 |
params => $reserved_params |
79 |
} |
80 |
); |
81 |
|
82 |
# Generate prefetches for embedded stuff |
83 |
$c->dbic_merge_prefetch( |
84 |
{ |
85 |
attributes => $attributes, |
86 |
result_set => $orders_rs |
87 |
} |
88 |
); |
89 |
|
90 |
# Call the to_model function by reference, if defined |
91 |
if ( defined $filtered_params ) { |
92 |
|
93 |
# Apply the mapping function to the passed params |
94 |
$filtered_params = $orders_rs->attributes_from_api($filtered_params); |
95 |
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); |
96 |
} |
97 |
|
98 |
if ( defined $path_params ) { |
99 |
|
100 |
# Apply the mapping function to the passed params |
101 |
$filtered_params //= {}; |
102 |
$path_params = $orders_rs->attributes_from_api($path_params); |
103 |
foreach my $param (keys %{$path_params}) { |
104 |
$filtered_params->{$param} = $path_params->{$param}; |
105 |
} |
106 |
} |
107 |
|
108 |
if ( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) { |
109 |
$filtered_params //={}; |
110 |
my @query_params_array; |
111 |
my $query_params; |
112 |
if ( exists $reserved_params->{query} and defined $reserved_params->{query} ) { |
113 |
push @query_params_array, fix_query({ query => $reserved_params->{query} }); |
114 |
} |
115 |
if ( exists $reserved_params->{q} and defined $reserved_params->{q}) { |
116 |
push @query_params_array, fix_query({ query => decode_json($reserved_params->{q}) }); |
117 |
} |
118 |
if ( exists $reserved_params->{'x-koha-query'} and defined $reserved_params->{'x-koha-query'} ) { |
119 |
push @query_params_array, fix_query({ query => decode_json($reserved_params->{'x-koha-query'}) });; |
120 |
} |
121 |
|
122 |
if(scalar(@query_params_array) > 1) { |
123 |
$query_params = {'-and' => \@query_params_array}; |
124 |
} |
125 |
else { |
126 |
$query_params = $query_params_array[0]; |
127 |
} |
128 |
|
129 |
$filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs ); |
130 |
} |
131 |
|
132 |
# Perform search |
133 |
my $orders = $orders_rs->search( $filtered_params, $attributes ); |
134 |
|
135 |
if ($orders->is_paged) { |
136 |
$c->add_pagination_headers({ |
137 |
total => $orders->pager->total_entries, |
138 |
params => $args, |
139 |
}); |
140 |
} |
48 |
|
141 |
|
49 |
return $c->render( |
142 |
return $c->render( |
50 |
status => 200, |
143 |
status => 200, |
51 |
openapi => $orders |
144 |
openapi => $orders->to_api({ embed => $embed }) |
52 |
); |
145 |
); |
53 |
} |
146 |
} |
54 |
catch { |
147 |
catch { |
Lines 226-229
sub delete {
Link Here
|
226 |
}; |
319 |
}; |
227 |
} |
320 |
} |
228 |
|
321 |
|
|
|
322 |
=head2 Internal methods |
323 |
|
324 |
=head3 fix_query |
325 |
|
326 |
my $query = fix_query($query); |
327 |
|
328 |
This method takes care of recursively fixing queries that should be done |
329 |
against biblioitems (instead if biblio as exposed on the API) |
330 |
|
331 |
=cut |
332 |
|
333 |
sub fix_query { |
334 |
my ($args) = @_; |
335 |
|
336 |
my $query = $args->{query}; |
337 |
|
338 |
if ( ref($query) eq 'HASH' ) { |
339 |
foreach my $key (keys %{$query}) { |
340 |
if ($key eq 'biblio.isbn') { |
341 |
my $subq = delete $query->{$key}; |
342 |
$query->{'biblio.biblioitem.isbn'} = (ref($subq) eq 'HASH') |
343 |
? fix_query({ query => $subq }) |
344 |
: $subq; |
345 |
} |
346 |
else { |
347 |
$query->{$key} = fix_query({ query => $query->{$key} }); |
348 |
} |
349 |
} |
350 |
} |
351 |
elsif ( ref($query) eq 'ARRAY' ) { |
352 |
my @accum; |
353 |
foreach my $item (@{$query}) { |
354 |
push @accum, fix_query({ query => $item }); |
355 |
} |
356 |
$query = \@accum; |
357 |
} |
358 |
else { # scalar |
359 |
$query = 'biblio.biblioadmin.isbn' |
360 |
if $query eq 'biblio.isbn'; |
361 |
} |
362 |
|
363 |
return $query; |
364 |
} |
365 |
|
229 |
1; |
366 |
1; |
230 |
- |
|
|