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

(-)a/Koha/REST/Plugin/Objects.pm (-7 / +14 lines)
Lines 30-44 Koha::REST::Plugin::Objects Link Here
30
=head3 objects.search
30
=head3 objects.search
31
31
32
    my $patrons_set = Koha::Patrons->new;
32
    my $patrons_set = Koha::Patrons->new;
33
    my $patrons = $c->objects->search( $patrons_set, [\&to_model] );
33
    my $patrons = $c->objects->search( $patrons_set, [\&to_model, \&to_api] );
34
34
35
Performs a database search using given Koha::Objects object and query parameters.
35
Performs a database search using given Koha::Objects object and query parameters.
36
Optionally, it applies the I<$to_model> function reference before building the
36
It (optionally) applies the I<$to_model> function reference before building the
37
query itself.
37
query itself, and (optionally) applies I<$to_api> to the result.
38
38
39
Note: Make sure I<$to_model> doesn't autovivify keys.
39
Returns an arrayref of the hashrefs representing the resulting objects
40
for JSON rendering.
40
41
41
Returns a Koha::Objects object
42
Note: Make sure I<$to_model> and I<$to_api> don't autovivify keys.
42
43
43
=cut
44
=cut
44
45
Lines 47-53 sub register { Link Here
47
48
48
    $app->helper(
49
    $app->helper(
49
        'objects.search' => sub {
50
        'objects.search' => sub {
50
            my ( $c, $objects_set, $to_model ) = @_;
51
            my ( $c, $objects_set, $to_model, $to_api ) = @_;
51
52
52
            my $args = $c->validation->output;
53
            my $args = $c->validation->output;
53
            my $attributes = {};
54
            my $attributes = {};
Lines 90-96 sub register { Link Here
90
                });
91
                });
91
            }
92
            }
92
93
93
            return $objects;
94
            my @objects_list = map {
95
                ( defined $to_api )
96
                  ? $to_api->( $_->TO_JSON )
97
                  : $_->TO_JSON
98
            } $objects->as_list;
99
100
            return \@objects_list;
94
        }
101
        }
95
    );
102
    );
96
}
103
}
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-4 / +58 lines)
Lines 38-48 get '/patrons_to_model' => sub { Link Here
38
    my $c = shift;
38
    my $c = shift;
39
    $c->validation->output($c->req->params->to_hash);
39
    $c->validation->output($c->req->params->to_hash);
40
    my $patrons_set = Koha::Patrons->new;
40
    my $patrons_set = Koha::Patrons->new;
41
    my $patrons = $c->objects->search( $patrons_set, \&_to_model );
41
    my $patrons = $c->objects->search( $patrons_set, \&to_model );
42
    $c->render( status => 200, json => $patrons );
42
    $c->render( status => 200, json => $patrons );
43
};
43
};
44
44
45
sub _to_model {
45
get '/patrons_to_model_to_api' => sub {
46
    my $c = shift;
47
    $c->validation->output($c->req->params->to_hash);
48
    my $patrons_set = Koha::Patrons->new;
49
    my $patrons = $c->objects->search( $patrons_set, \&to_model, \&to_api );
50
    $c->render( status => 200, json => $patrons );
51
};
52
53
sub to_model {
46
    my $params = shift;
54
    my $params = shift;
47
55
48
    if ( exists $params->{nombre} ) {
56
    if ( exists $params->{nombre} ) {
Lines 52-57 sub _to_model { Link Here
52
    return $params;
60
    return $params;
53
}
61
}
54
62
63
sub to_api {
64
    my $params = shift;
65
66
    if ( exists $params->{firstname} ) {
67
        $params->{nombre} = delete $params->{firstname};
68
    }
69
70
    return $params;
71
}
72
55
# The tests
73
# The tests
56
use Test::More tests => 1;
74
use Test::More tests => 1;
57
use Test::Mojo;
75
use Test::Mojo;
Lines 66-72 my $builder = t::lib::TestBuilder->new; Link Here
66
84
67
subtest 'objects.search helper' => sub {
85
subtest 'objects.search helper' => sub {
68
86
69
    plan tests => 62;
87
    plan tests => 90;
70
88
71
    my $t = Test::Mojo->new;
89
    my $t = Test::Mojo->new;
72
90
Lines 175-179 subtest 'objects.search helper' => sub { Link Here
175
        ->json_is('/1/firstname' => 'Manuela')
193
        ->json_is('/1/firstname' => 'Manuela')
176
        ->json_is('/2/firstname' => 'Emanuel');
194
        ->json_is('/2/firstname' => 'Emanuel');
177
195
196
    ## _to_model && _to_api tests
197
    # _match=starts_with
198
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
199
        ->status_is(200)
200
        ->json_has('/0')
201
        ->json_has('/1')
202
        ->json_hasnt('/2')
203
        ->json_is('/0/nombre' => 'Manuel')
204
        ->json_is('/1/nombre' => 'Manuela');
205
206
    # _match=ends_with
207
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
208
        ->status_is(200)
209
        ->json_has('/0')
210
        ->json_has('/1')
211
        ->json_hasnt('/2')
212
        ->json_is('/0/nombre' => 'Manuel')
213
        ->json_is('/1/nombre' => 'Emanuel');
214
215
    # _match=exact
216
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
217
        ->status_is(200)
218
        ->json_has('/0')
219
        ->json_hasnt('/1')
220
        ->json_is('/0/nombre' => 'Manuel');
221
222
    # _match=contains
223
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
224
        ->status_is(200)
225
        ->json_has('/0')
226
        ->json_has('/1')
227
        ->json_has('/2')
228
        ->json_hasnt('/3')
229
        ->json_is('/0/nombre' => 'Manuel')
230
        ->json_is('/1/nombre' => 'Manuela')
231
        ->json_is('/2/nombre' => 'Emanuel');
232
178
    $schema->storage->txn_rollback;
233
    $schema->storage->txn_rollback;
179
};
234
};
180
- 

Return to bug 19686