@@ -, +, @@ generation GET /api/v1/?order_by=+column_1|-column_2|column_3 - Apply this patches - Run: $ sudo koha-shell kohadev k$ cd kohaclone k$ prove t/Koha/REST/Plugin/Query.t - Sign off :-D --- Koha/REST/Plugin/Query.pm | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) --- a/Koha/REST/Plugin/Query.pm +++ a/Koha/REST/Plugin/Query.pm @@ -65,6 +65,28 @@ Generates the DBIC query from the query parameters. return ( $filtered_params, $reserved_params ); } ); + +=head3 dbic_merge_sorting + + $filter = $c->dbic_merge_sorting({ filter => $filter, params => $params }); + +Generates the DBIC order_by attributes based on I<$params>, and merges into I<$filter>. + +=cut + + $app->helper( + 'dbic_merge_sorting' => sub { + my ( $c, $args ) = @_; + my $filter = $args->{filter}; + + my @order_by = + map { _build_order_atom($_) } + split( /\|/, $args->{params}->{_order_by} ); + + $filter->{order_by} = \@order_by; + return $filter; + } + ); } =head2 Internal methods @@ -81,4 +103,36 @@ sub _reserved_words { return \@reserved_words; } +=head3 _build_order_atom + + my $order_atom = _build_order_atom( $string ); + +Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute +according to the following rules: + + string -> I + +string -> I<{ -asc => string }> + -string -> I<{ -desc => string }> + +=cut + +sub _build_order_atom { + my $string = shift; + + if ( $string =~ m/^\+/ ) { + # asc order operator present + $string =~ s/^\+//; + return { -asc => $string }; + } + elsif ( $string =~ m/^\-/ ) { + # desc order operator present + $string =~ s/^\-//; + return { -desc => $string }; + } + else { + # no order operator present + return $string; + } +} + 1; --