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

(-)a/Koha/Objects.pm (-41 lines)
Lines 171-217 sub search_related { Link Here
171
    }
171
    }
172
}
172
}
173
173
174
=head3 search_for_api
175
176
    my $objects = Koha::Objects->search_for_api( $c );
177
178
Searches for objects given a controller object I<$c>.
179
180
=cut
181
182
sub search_for_api {
183
    my ( $self, $c ) = @_;
184
185
    my $args = $c->validation->output;
186
    my $attributes;
187
188
    # Extract reserved params
189
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
190
191
    # Merge sorting into query attributes
192
    $c->dbic_merge_sorting(
193
        {
194
            attributes => $attributes,
195
            params     => $reserved_params
196
        }
197
    );
198
199
    # Merge pagination into query attributes
200
    $c->dbic_merge_pagination(
201
        {
202
            attributes => $attributes,
203
            params     => $reserved_params
204
        }
205
    );
206
207
    # Perform search
208
    my $objects = $self->search( $filtered_params, $attributes );
209
    $c->add_pagination_headers({ total => $objects->count, params => $args })
210
        if $objects->is_paged;
211
212
    return $objects;
213
}
214
215
=head2 _build_query_params_from_api
174
=head2 _build_query_params_from_api
216
175
217
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
176
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
(-)a/Koha/REST/Plugin/Objects.pm (+85 lines)
Line 0 Link Here
1
package Koha::REST::Plugin::Objects;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Plugin';
21
22
=head1 NAME
23
24
Koha::REST::Plugin::Objects
25
26
=head1 API
27
28
=head2 Helper methods
29
30
=head3 objects.search
31
32
    my $patrons_set = Koha::Patrons->new;
33
    my $patrons = $c->objects->search($patrons_set);
34
35
Performs a database search using given Koha::Objects object and query parameters
36
37
Returns a Koha::Objects object
38
39
=cut
40
41
sub register {
42
    my ( $self, $app ) = @_;
43
44
    $app->helper(
45
        'objects.search' => sub {
46
            my ( $c, $objects_set ) = @_;
47
48
            my $args = $c->validation->output;
49
            my $attributes = {};
50
51
            # Extract reserved params
52
            my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
53
54
            # Merge sorting into query attributes
55
            $c->dbic_merge_sorting(
56
                {
57
                    attributes => $attributes,
58
                    params     => $reserved_params
59
                }
60
            );
61
62
            # Merge pagination into query attributes
63
            $c->dbic_merge_pagination(
64
                {
65
                    filter => $attributes,
66
                    params => $reserved_params
67
                }
68
            );
69
70
            # Perform search
71
            my $objects = $objects_set->search( $filtered_params, $attributes );
72
73
            if ($objects->is_paged) {
74
                $c->add_pagination_headers({
75
                    total => $objects->pager->total_entries,
76
                    params => $args,
77
                });
78
            }
79
80
            return $objects;
81
        }
82
    );
83
}
84
85
1;
(-)a/Koha/REST/V1.pm (+1 lines)
Lines 55-60 sub startup { Link Here
55
    });
55
    });
56
    $self->plugin( 'Koha::REST::Plugin::Pagination' );
56
    $self->plugin( 'Koha::REST::Plugin::Pagination' );
57
    $self->plugin( 'Koha::REST::Plugin::Query' );
57
    $self->plugin( 'Koha::REST::Plugin::Query' );
58
    $self->plugin( 'Koha::REST::Plugin::Objects' );
58
}
59
}
59
60
60
1;
61
1;
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-1 / +77 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Koha::Patrons;
20
21
# Dummy app for testing the plugin
22
use Mojolicious::Lite;
23
24
app->log->level('error');
25
26
plugin 'Koha::REST::Plugin::Objects';
27
plugin 'Koha::REST::Plugin::Query';
28
plugin 'Koha::REST::Plugin::Pagination';
29
30
get '/patrons' => sub {
31
    my $c = shift;
32
    $c->validation->output($c->req->params->to_hash);
33
    my $patrons = $c->objects->search(Koha::Patrons->new);
34
    $c->render( status => 200, json => $patrons->TO_JSON );
35
};
36
37
38
# The tests
39
40
use Test::More tests => 1;
41
use Test::Mojo;
42
43
use t::lib::TestBuilder;
44
use Koha::Database;
45
46
my $schema = Koha::Database->new()->schema();
47
$schema->storage->txn_begin();
48
49
my $builder = t::lib::TestBuilder->new;
50
$builder->build({
51
    source => 'Borrower',
52
    value => {
53
        firstname => 'Manuel',
54
    },
55
});
56
$builder->build({
57
    source => 'Borrower',
58
    value => {
59
        firstname => 'Manuel',
60
    },
61
});
62
63
subtest 'objects.search helper' => sub {
64
65
    plan tests => 6;
66
67
    my $t = Test::Mojo->new;
68
69
    $t->get_ok('/patrons?firstname=Manuel&_per_page=1&_page=1')
70
        ->status_is(200)
71
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
72
        ->json_has('/0')
73
        ->json_hasnt('/1')
74
        ->json_is('/0/firstname' => 'Manuel');
75
};
76
77
$schema->storage->txn_rollback();

Return to bug 19410