View | Details | Raw Unified | Return to bug 20212
Collapse All | Expand All

(-)a/Koha/REST/V1/Acquisitions/Orders.pm (-3 / +143 lines)
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
    my $biblioitem_fields = {
338
        'biblio.isbn' => 'biblio.biblioitem.isbn',
339
        'biblio.ean'  => 'biblio.biblioitem.ean'
340
    };
341
342
    if ( ref($query) eq 'HASH' ) {
343
        foreach my $key (keys %{$query}) {
344
            if ( exists $biblioitem_fields->{$key}) {
345
                my $subq = delete $query->{$key};
346
                $query->{$biblioitem_fields->{$key}} = (ref($subq) eq 'HASH')
347
                        ? fix_query({ query => $subq })
348
                        : $subq;
349
            }
350
            else {
351
                $query->{$key} = fix_query({ query => $query->{$key} });
352
            }
353
        }
354
    }
355
    elsif ( ref($query) eq 'ARRAY' ) {
356
        my @accum;
357
        foreach my $item (@{$query}) {
358
            push @accum, fix_query({ query => $item });
359
        }
360
        $query = \@accum;
361
    }
362
    else { # scalar
363
        $query = $biblioitem_fields->{$query}
364
            if exists $biblioitem_fields->{$query};
365
    }
366
367
    return $query;
368
}
369
229
1;
370
1;
230
- 

Return to bug 20212