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

(-)a/Koha/Object.pm (+29 lines)
Lines 372-377 sub _numeric_column_type { Link Here
372
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
372
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
373
}
373
}
374
374
375
=head3 prefetch_whitelist
376
377
    my $whitelist = $object->prefetch_whitelist()
378
379
Returns a hash of prefetchable subs and the type they return.
380
381
=cut
382
383
sub prefetch_whitelist {
384
    my ( $self ) = @_;
385
386
    my $whitelist = {};
387
    my $relations = $self->_result->result_source->_relationships;
388
389
    foreach my $key (keys %{$relations}) {
390
        if($self->can($key)) {
391
            my $result_class = $relations->{$key}->{class};
392
            my $obj = $result_class->new;
393
            try {
394
                $whitelist->{$key} = $obj->koha_object_class;
395
            } catch {
396
                $whitelist->{$key} = undef;
397
            }
398
        }
399
    }
400
401
    return $whitelist;
402
}
403
375
=head3 to_api
404
=head3 to_api
376
405
377
    my $object_for_api = $object->to_api(
406
    my $object_for_api = $object->to_api(
(-)a/Koha/Objects.pm (+16 lines)
Lines 351-356 sub from_api_mapping { Link Here
351
    return $self->{_singular_object}->from_api_mapping;
351
    return $self->{_singular_object}->from_api_mapping;
352
}
352
}
353
353
354
=head3 prefetch_whitelist
355
356
    my $whitelist = $object->prefetch_whitelist()
357
358
Returns a hash of prefetchable subs and the type it returns
359
360
=cut
361
362
sub prefetch_whitelist {
363
    my ( $self ) = @_;
364
365
    $self->{_singular_object} ||= $self->object_class->new();
366
367
    $self->{_singular_object}->prefetch_whitelist;
368
}
369
354
=head3 Koha::Objects->_wrap
370
=head3 Koha::Objects->_wrap
355
371
356
wraps the DBIC object in a corresponding Koha object
372
wraps the DBIC object in a corresponding Koha object
(-)a/Koha/REST/Plugin/Objects.pm (+8 lines)
Lines 71-76 sub register { Link Here
71
                }
71
                }
72
            );
72
            );
73
73
74
            # Generate prefetches for embedded stuff
75
            $c->dbic_merge_prefetch(
76
                {
77
                    attributes => $attributes,
78
                    result_set => $result_set
79
                }
80
            );
81
74
            # Call the to_model function by reference, if defined
82
            # Call the to_model function by reference, if defined
75
            if ( defined $filtered_params ) {
83
            if ( defined $filtered_params ) {
76
84
(-)a/Koha/REST/Plugin/Query.pm (+48 lines)
Lines 100-105 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
100
        }
100
        }
101
    );
101
    );
102
102
103
=head3 dbic_merge_prefetch
104
105
    $attributes = $c->dbic_merge_prefetch({ attributes => $attributes, result_set => $result_set });
106
107
Generates the DBIC prefetch attribute based on embedded relations, and merges into I<$attributes>.
108
109
=cut
110
111
    $app->helper(
112
        'dbic_merge_prefetch' => sub {
113
            my ( $c, $args ) = @_;
114
            my $attributes = $args->{attributes};
115
            my $result_set = $args->{result_set};
116
            my $embed = $c->stash('koha.embed');
117
118
            return unless defined $embed;
119
120
            my @prefetches;
121
            foreach my $key (keys %{$embed}) {
122
                my $parsed = _parse_prefetch($key, $embed, $result_set);
123
                push @prefetches, $parsed if defined $parsed;
124
            }
125
126
            if(scalar(@prefetches)) {
127
                $attributes->{prefetch} = \@prefetches;
128
            }
129
        }
130
    );
131
103
=head3 _build_query_params_from_api
132
=head3 _build_query_params_from_api
104
133
105
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
134
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
Lines 287-290 sub _merge_embed { Link Here
287
    }
316
    }
288
}
317
}
289
318
319
sub _parse_prefetch {
320
    my ( $key, $embed, $result_set) = @_;
321
322
    return unless exists $result_set->prefetch_whitelist->{$key};
323
324
    my $ko_class = $result_set->prefetch_whitelist->{$key};
325
    return $key unless defined $embed->{$key}->{children} && defined $ko_class;
326
327
    my $prefetch = {};
328
    foreach my $child (keys %{$embed->{$key}->{children}}) {
329
        my $parsed = _parse_prefetch($child, $embed->{$key}->{children}, $ko_class->new);
330
        $prefetch->{$key} = $parsed if defined $parsed;
331
    }
332
333
    return unless scalar(keys %{$prefetch});
334
335
    return $prefetch;
336
}
337
290
1;
338
1;
(-)a/t/Koha/REST/Plugin/Query.t (-2 / +32 lines)
Lines 123-128 get '/dbic_merge_sorting_date' => sub { Link Here
123
    $c->render( json => $attributes, status => 200 );
123
    $c->render( json => $attributes, status => 200 );
124
};
124
};
125
125
126
get '/dbic_merge_prefetch' => sub {
127
    my $c = shift;
128
    my $attributes = {};
129
    my $result_set = Koha::Holds->new;
130
    $c->stash('koha.embed', {
131
            "item" => {},
132
            "biblio" => {
133
                children => {
134
                    "orders" => {}
135
                }
136
            }
137
        });
138
139
    $c->dbic_merge_prefetch({
140
        attributes => $attributes,
141
        result_set => $result_set
142
    });
143
144
    $c->render( json => $attributes, status => 200 );
145
};
146
126
get '/build_query' => sub {
147
get '/build_query' => sub {
127
    my $c = shift;
148
    my $c = shift;
128
    my ( $filtered_params, $reserved_params ) =
149
    my ( $filtered_params, $reserved_params ) =
Lines 197-203 sub to_model { Link Here
197
218
198
# The tests
219
# The tests
199
220
200
use Test::More tests => 4;
221
use Test::More tests => 5;
201
use Test::Mojo;
222
use Test::Mojo;
202
223
203
subtest 'extract_reserved_params() tests' => sub {
224
subtest 'extract_reserved_params() tests' => sub {
Lines 270-275 subtest 'dbic_merge_sorting() tests' => sub { Link Here
270
      );
291
      );
271
};
292
};
272
293
294
subtest '/dbic_merge_prefetch' => sub {
295
    plan tests => 4;
296
297
    my $t = Test::Mojo->new;
298
299
    $t->get_ok('/dbic_merge_prefetch')->status_is(200)
300
      ->json_like( '/prefetch/0' => qr/item|biblio/ )
301
      ->json_like( '/prefetch/1' => qr/item|biblio/ );
302
};
303
273
subtest '_build_query_params_from_api' => sub {
304
subtest '_build_query_params_from_api' => sub {
274
305
275
    plan tests => 16;
306
    plan tests => 16;
276
- 

Return to bug 24356