Bug 22026: Removed 'use Modern::Perl;' from Koha::REST::classes
[koha.git] / Koha / REST / Plugin / Query.pm
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 Mojo::Base 'Mojolicious::Plugin';
19
20 use Koha::Exceptions;
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 extract_reserved_params
40
41     my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
42
43 Generates the DBIC query from the query parameters.
44
45 =cut
46
47     $app->helper(
48         'extract_reserved_params' => sub {
49             my ( $c, $params ) = @_;
50
51             my $reserved_params;
52             my $filtered_params;
53
54             my $reserved_words = _reserved_words();
55
56             foreach my $param ( keys %{$params} ) {
57                 if ( grep { $param eq $_ } @{$reserved_words} ) {
58                     $reserved_params->{$param} = $params->{$param};
59                 }
60                 else {
61                     $filtered_params->{$param} = $params->{$param};
62                 }
63             }
64
65             return ( $filtered_params, $reserved_params );
66         }
67     );
68
69 =head3 dbic_merge_sorting
70
71     $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
72
73 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
74
75 =cut
76
77     $app->helper(
78         'dbic_merge_sorting' => sub {
79             my ( $c, $args ) = @_;
80             my $attributes = $args->{attributes};
81
82             if ( defined $args->{params}->{_order_by} ) {
83                 my @order_by = map { _build_order_atom($_) }
84                                @{ $args->{params}->{_order_by} };
85                 $attributes->{order_by} = \@order_by;
86             }
87
88             return $attributes;
89         }
90     );
91
92 =head3 _build_query_params_from_api
93
94     my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
95
96 Builds the params for searching on DBIC based on the selected matching algorithm.
97 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
98 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
99 is raised.
100
101 =cut
102
103     $app->helper(
104         'build_query_params' => sub {
105
106             my ( $c, $filtered_params, $reserved_params ) = @_;
107
108             my $params;
109             my $match = $reserved_params->{_match} // 'contains';
110
111             foreach my $param ( keys %{$filtered_params} ) {
112                 if ( $match eq 'contains' ) {
113                     $params->{$param} =
114                       { like => '%' . $filtered_params->{$param} . '%' };
115                 }
116                 elsif ( $match eq 'starts_with' ) {
117                     $params->{$param} = { like => $filtered_params->{$param} . '%' };
118                 }
119                 elsif ( $match eq 'ends_with' ) {
120                     $params->{$param} = { like => '%' . $filtered_params->{$param} };
121                 }
122                 elsif ( $match eq 'exact' ) {
123                     $params->{$param} = $filtered_params->{$param};
124                 }
125                 else {
126                     # We should never reach here, because the OpenAPI plugin should
127                     # prevent invalid params to be passed
128                     Koha::Exceptions::WrongParameter->throw(
129                         "Invalid value for _match param ($match)");
130                 }
131             }
132
133             return $params;
134         }
135     );
136 }
137
138 =head2 Internal methods
139
140 =head3 _reserved_words
141
142     my $reserved_words = _reserved_words();
143
144 =cut
145
146 sub _reserved_words {
147
148     my @reserved_words = qw( _match _order_by _page _per_page );
149     return \@reserved_words;
150 }
151
152 =head3 _build_order_atom
153
154     my $order_atom = _build_order_atom( $string );
155
156 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
157 according to the following rules:
158
159      string -> I<string>
160     +string -> I<{ -asc => string }>
161     -string -> I<{ -desc => string }>
162
163 =cut
164
165 sub _build_order_atom {
166     my $string = shift;
167
168     if ( $string =~ m/^\+/ or
169          $string =~ m/^\s/ ) {
170         # asc order operator present
171         $string =~ s/^(\+|\s)//;
172         return { -asc => $string };
173     }
174     elsif ( $string =~ m/^\-/ ) {
175         # desc order operator present
176         $string =~ s/^\-//;
177         return { -desc => $string };
178     }
179     else {
180         # no order operator present
181         return $string;
182     }
183 }
184
185 1;