diff --git a/Koha/REST/V1/Acquisitions/Orders.pm b/Koha/REST/V1/Acquisitions/Orders.pm index d0bdb40fd2..0effcc5cac 100644 --- a/Koha/REST/V1/Acquisitions/Orders.pm +++ b/Koha/REST/V1/Acquisitions/Orders.pm @@ -61,132 +61,22 @@ sub list { $orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id }) if $order_id; - my $args = $c->validation->output; - my $attributes = {}; + my @query_fixers; - # Extract reserved params - my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); # Look for embeds my $embed = $c->stash('koha.embed'); - my $fixed_embed = clone($embed); - if ( exists $fixed_embed->{biblio} ) { + if ( exists $embed->{biblio} ) { # asked to embed biblio + my $fixed_embed = clone($embed); # Add biblioitems to prefetch # FIXME remove if we merge biblio + biblioitems $fixed_embed->{biblio}->{children}->{biblioitem} = {}; $c->stash('koha.embed', $fixed_embed); + push @query_fixers, (sub{ Koha::Biblios->new->api_query_fixer( $_[0], 'biblio', $_[1] ) }); } - if ( exists $reserved_params->{_order_by} ) { - # _order_by passed, fix if required - for my $p ( @{$reserved_params->{_order_by}} ) { - $p = $c->table_name_fixer($p); - } - } - - # Merge sorting into query attributes - $c->dbic_merge_sorting( - { - attributes => $attributes, - params => $reserved_params, - result_set => $orders_rs, - } - ); - - # If no pagination parameters are passed, default - $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize'); - $reserved_params->{_page} //= 1; - - unless ( $reserved_params->{_per_page} == -1 ) { - # Merge pagination into query attributes - $c->dbic_merge_pagination( - { - filter => $attributes, - params => $reserved_params - } - ); - } - - # Generate prefetches for embedded stuff - $c->dbic_merge_prefetch( - { - attributes => $attributes, - result_set => $orders_rs - } - ); - - # Call the to_model function by reference, if defined - if ( defined $filtered_params ) { - - # Apply the mapping function to the passed params - $filtered_params = $orders_rs->attributes_from_api($filtered_params); - $filtered_params = $c->build_query_params( $filtered_params, $reserved_params ); - } - - if ( defined $path_params ) { - - # Apply the mapping function to the passed params - $filtered_params //= {}; - $path_params = $orders_rs->attributes_from_api($path_params); - foreach my $param (keys %{$path_params}) { - $filtered_params->{$param} = $path_params->{$param}; - } - } - - if ( defined $reserved_params->{q} - || defined $reserved_params->{query} ) - { - - $filtered_params //={}; - - my @query_params_array; - - my $json = JSON->new; - - # q is defined as multi => JSON::Validator generates an array - # containing the string - foreach my $q ( @{ $reserved_params->{q} } ) { - push @query_params_array, - $json->decode( $c->table_name_fixer($q) ) - if $q; # skip if exists but is empty - } - - # query is already decoded by JSON::Validator at this point - push @query_params_array, - $json->decode( - $c->table_name_fixer( - $json->encode( $reserved_params->{query} ) - ) - ) if $reserved_params->{query}; - - my $query_params; - - if ( scalar(@query_params_array) > 1 ) { - $query_params = { '-and' => \@query_params_array }; - } - else { - $query_params = $query_params_array[0]; - } - - $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs ); - } - - # Perform search - my $orders = $orders_rs->search( $filtered_params, $attributes ); - my $total = $orders_rs->search->count; - - $c->add_pagination_headers( - { - base_total => $total, - page => $reserved_params->{_page}, - per_page => $reserved_params->{_per_page}, - query_params => $args, - total => ( $orders->is_paged ? $orders->pager->total_entries : $orders->count ), - } - ); - return $c->render( status => 200, - openapi => $c->objects->to_api($orders) + openapi => $c->objects->search( $orders_rs, \@query_fixers ), ); } catch {