From 944c6939251906542f2d8aeebb01a3af210173f6 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 16 Jun 2023 12:53:05 -0300 Subject: [PATCH] Bug 33974: (follow-up) Adapt the orders endpoint In order to reduce the technical debt carried on the orders controller, and to highlight the decisions made on the prior patch, I adapted the list() orders controller using the new tools in town. The result is this endpoint can now embed bibio, without needing to have a custom piece of code. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/*.t => SUCCESS: Tests pass :-D Signed-off-by: Sam Lau Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi (cherry picked from commit c07e43cf2afecfa6df0ba259187b90f567bc2603) Signed-off-by: Martin Renvoize --- Koha/REST/V1/Acquisitions/Orders.pm | 127 ++-------------------------- 1 file changed, 5 insertions(+), 122 deletions(-) diff --git a/Koha/REST/V1/Acquisitions/Orders.pm b/Koha/REST/V1/Acquisitions/Orders.pm index bb12ed0a53..0effcc5cac 100644 --- a/Koha/REST/V1/Acquisitions/Orders.pm +++ b/Koha/REST/V1/Acquisitions/Orders.pm @@ -61,139 +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} - || defined $reserved_params->{'x-koha-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 - } - - # x-koha-query contains a string - push @query_params_array, - $json->decode( - $c->table_name_fixer( $reserved_params->{'x-koha-query'} ) ) - if $reserved_params->{'x-koha-query'}; - - # 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 { -- 2.39.5