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

(-)a/Koha/Object.pm (+29 lines)
Lines 373-378 sub _numeric_column_type { Link Here
373
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
373
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
374
}
374
}
375
375
376
=head3 prefetch_whitelist
377
378
    my $whitelist = $object->prefetch_whitelist()
379
380
Returns a hash of prefetchable subs and the type they return.
381
382
=cut
383
384
sub prefetch_whitelist {
385
    my ( $self ) = @_;
386
387
    my $whitelist = {};
388
    my $relations = $self->_result->result_source->_relationships;
389
390
    foreach my $key (keys %{$relations}) {
391
        if($self->can($key)) {
392
            my $result_class = $relations->{$key}->{class};
393
            my $obj = $result_class->new;
394
            try {
395
                $whitelist->{$key} = $obj->koha_object_class;
396
            } catch {
397
                $whitelist->{$key} = undef;
398
            }
399
        }
400
    }
401
402
    return $whitelist;
403
}
404
376
=head3 to_api
405
=head3 to_api
377
406
378
    my $object_for_api = $object->to_api(
407
    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 105-110 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
105
        }
105
        }
106
    );
106
    );
107
107
108
=head3 dbic_merge_prefetch
109
110
    $attributes = $c->dbic_merge_prefetch({ attributes => $attributes, result_set => $result_set });
111
112
Generates the DBIC prefetch attribute based on embedded relations, and merges into I<$attributes>.
113
114
=cut
115
116
    $app->helper(
117
        'dbic_merge_prefetch' => sub {
118
            my ( $c, $args ) = @_;
119
            my $attributes = $args->{attributes};
120
            my $result_set = $args->{result_set};
121
            my $embed = $c->stash('koha.embed');
122
123
            return unless defined $embed;
124
125
            my @prefetches;
126
            foreach my $key (keys %{$embed}) {
127
                my $parsed = _parse_prefetch($key, $embed, $result_set);
128
                push @prefetches, $parsed if defined $parsed;
129
            }
130
131
            if(scalar(@prefetches)) {
132
                $attributes->{prefetch} = \@prefetches;
133
            }
134
        }
135
    );
136
108
=head3 _build_query_params_from_api
137
=head3 _build_query_params_from_api
109
138
110
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
139
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
Lines 298-301 sub _merge_embed { Link Here
298
    }
327
    }
299
}
328
}
300
329
330
sub _parse_prefetch {
331
    my ( $key, $embed, $result_set) = @_;
332
333
    return unless exists $result_set->prefetch_whitelist->{$key};
334
335
    my $ko_class = $result_set->prefetch_whitelist->{$key};
336
    return $key unless defined $embed->{$key}->{children} && defined $ko_class;
337
338
    my $prefetch = {};
339
    foreach my $child (keys %{$embed->{$key}->{children}}) {
340
        my $parsed = _parse_prefetch($child, $embed->{$key}->{children}, $ko_class->new);
341
        $prefetch->{$key} = $parsed if defined $parsed;
342
    }
343
344
    return unless scalar(keys %{$prefetch});
345
346
    return $prefetch;
347
}
348
301
1;
349
1;
(-)a/t/Koha/REST/Plugin/Query.t (-2 / +32 lines)
Lines 113-118 get '/dbic_merge_sorting_date' => sub { Link Here
113
    $c->render( json => $attributes, status => 200 );
113
    $c->render( json => $attributes, status => 200 );
114
};
114
};
115
115
116
get '/dbic_merge_prefetch' => sub {
117
    my $c = shift;
118
    my $attributes = {};
119
    my $result_set = Koha::Holds->new;
120
    $c->stash('koha.embed', {
121
            "item" => {},
122
            "biblio" => {
123
                children => {
124
                    "orders" => {}
125
                }
126
            }
127
        });
128
129
    $c->dbic_merge_prefetch({
130
        attributes => $attributes,
131
        result_set => $result_set
132
    });
133
134
    $c->render( json => $attributes, status => 200 );
135
};
136
116
get '/build_query' => sub {
137
get '/build_query' => sub {
117
    my $c = shift;
138
    my $c = shift;
118
    my ( $filtered_params, $reserved_params ) =
139
    my ( $filtered_params, $reserved_params ) =
Lines 188-194 sub to_model { Link Here
188
209
189
# The tests
210
# The tests
190
211
191
use Test::More tests => 4;
212
use Test::More tests => 5;
192
use Test::Mojo;
213
use Test::Mojo;
193
214
194
subtest 'extract_reserved_params() tests' => sub {
215
subtest 'extract_reserved_params() tests' => sub {
Lines 266-271 subtest 'dbic_merge_sorting() tests' => sub { Link Here
266
      );
287
      );
267
};
288
};
268
289
290
subtest '/dbic_merge_prefetch' => sub {
291
    plan tests => 4;
292
293
    my $t = Test::Mojo->new;
294
295
    $t->get_ok('/dbic_merge_prefetch')->status_is(200)
296
      ->json_like( '/prefetch/0' => qr/item|biblio/ )
297
      ->json_like( '/prefetch/1' => qr/item|biblio/ );
298
};
299
269
subtest '_build_query_params_from_api' => sub {
300
subtest '_build_query_params_from_api' => sub {
270
301
271
    plan tests => 16;
302
    plan tests => 16;
272
- 

Return to bug 24356