1 package Koha::REST::Plugin::Query;
3 # This file is part of Koha.
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
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.
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.
20 use Mojo::Base 'Mojolicious::Plugin';
21 use Scalar::Util qw(reftype);
27 Koha::REST::Plugin::Query
31 =head2 Mojolicious::Plugin methods
38 my ( $self, $app ) = @_;
42 =head3 extract_reserved_params
44 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
46 Generates the DBIC query from the query parameters.
51 'extract_reserved_params' => sub {
52 my ( $c, $params ) = @_;
57 my $reserved_words = _reserved_words();
59 foreach my $param ( keys %{$params} ) {
60 if ( grep { $param eq $_ } @{$reserved_words} ) {
61 $reserved_params->{$param} = $params->{$param};
64 $filtered_params->{$param} = $params->{$param};
68 return ( $filtered_params, $reserved_params );
72 =head3 dbic_merge_sorting
74 $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
76 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
81 'dbic_merge_sorting' => sub {
82 my ( $c, $args ) = @_;
83 my $attributes = $args->{attributes};
84 my $result_set = $args->{result_set};
86 if ( defined $args->{params}->{_order_by} ) {
87 my $order_by = $args->{params}->{_order_by};
88 if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
89 my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
90 @{ $args->{params}->{_order_by} };
91 $attributes->{order_by} = \@order_by;
94 $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
102 =head3 _build_query_params_from_api
104 my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
106 Builds the params for searching on DBIC based on the selected matching algorithm.
107 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
108 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
114 'build_query_params' => sub {
116 my ( $c, $filtered_params, $reserved_params ) = @_;
119 my $match = $reserved_params->{_match} // 'contains';
121 foreach my $param ( keys %{$filtered_params} ) {
122 if ( $match eq 'contains' ) {
124 { like => '%' . $filtered_params->{$param} . '%' };
126 elsif ( $match eq 'starts_with' ) {
127 $params->{$param} = { like => $filtered_params->{$param} . '%' };
129 elsif ( $match eq 'ends_with' ) {
130 $params->{$param} = { like => '%' . $filtered_params->{$param} };
132 elsif ( $match eq 'exact' ) {
133 $params->{$param} = $filtered_params->{$param};
136 # We should never reach here, because the OpenAPI plugin should
137 # prevent invalid params to be passed
138 Koha::Exceptions::WrongParameter->throw(
139 "Invalid value for _match param ($match)");
148 =head2 Internal methods
150 =head3 _reserved_words
152 my $reserved_words = _reserved_words();
156 sub _reserved_words {
158 my @reserved_words = qw( _match _order_by _page _per_page );
159 return \@reserved_words;
162 =head3 _build_order_atom
164 my $order_atom = _build_order_atom( $string );
166 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
167 according to the following rules:
170 +string -> I<{ -asc => string }>
171 -string -> I<{ -desc => string }>
175 sub _build_order_atom {
177 my $string = $args->{string};
178 my $result_set = $args->{result_set};
181 $param =~ s/^(\+|\-|\s)//;
183 $param = (keys %{$result_set->attributes_from_api({ $param => 1 })})[0];
186 if ( $string =~ m/^\+/ or
187 $string =~ m/^\s/ ) {
188 # asc order operator present
189 return { -asc => $param };
191 elsif ( $string =~ m/^\-/ ) {
192 # desc order operator present
193 return { -desc => $param };
196 # no order operator present