Line 0
Link Here
|
0 |
- |
1 |
package Koha::REST::Plugin::Query; |
|
|
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::Query |
25 |
|
26 |
=head1 API |
27 |
|
28 |
=head2 Mojolicious::Plugin methods |
29 |
|
30 |
=head3 register |
31 |
|
32 |
=cut |
33 |
|
34 |
sub register { |
35 |
my ( $self, $app ) = @_; |
36 |
|
37 |
=head2 Helper methods |
38 |
|
39 |
=head3 generate_dbic_query |
40 |
|
41 |
my ( $filter, $attributes ) = $c->generate_dbic_query($params); |
42 |
|
43 |
Generates the DBIC query from the query parameters. |
44 |
|
45 |
=cut |
46 |
|
47 |
$app->helper( |
48 |
'generate_dbic_query' => sub { |
49 |
my ( $c, $params ) = @_; |
50 |
|
51 |
my $attributes = _build_dbic_attributes( $params->{page}, $params->{per_page} ); |
52 |
my $filter = _build_dbic_filter( $params ); |
53 |
|
54 |
return ( $filter, $attributes ); |
55 |
} |
56 |
); |
57 |
} |
58 |
|
59 |
=head2 Internal methods |
60 |
|
61 |
=head3 _build_dbic_attributes |
62 |
|
63 |
my $attributes = _build_dbic_attributes( $page, $per_page ); |
64 |
|
65 |
=cut |
66 |
|
67 |
sub _build_dbic_attributes { |
68 |
my ( $page, $per_page ) = @_; |
69 |
|
70 |
my $attributes = { |
71 |
rows => $per_page, |
72 |
page => $page |
73 |
}; |
74 |
|
75 |
return $attributes; |
76 |
} |
77 |
|
78 |
=head3 _build_dbic_filter |
79 |
|
80 |
my $filter = _build_dbic_filter( $params ); |
81 |
|
82 |
=cut |
83 |
|
84 |
sub _build_dbic_filter { |
85 |
my ( $params ) = @_; |
86 |
|
87 |
my $filter; |
88 |
my $exact_match = $params->{_exact_match}; |
89 |
|
90 |
for my $param ( keys %{ $params } ) { |
91 |
|
92 |
next if $param eq 'page' # reserved |
93 |
or $param eq 'per_page' # reserved |
94 |
or $param eq '_exact_match' ; # reserved |
95 |
|
96 |
if ( $exact_match ) { |
97 |
$filter->{$param} = $params->{$param}; |
98 |
} |
99 |
else { |
100 |
$filter->{$param} = { like => $params->{$param} . "%" } |
101 |
} |
102 |
} |
103 |
|
104 |
return $filter; |
105 |
} |
106 |
|
107 |
|
108 |
1; |