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

(-)a/Koha/REST/Plugin/Query.pm (-8 / +22 lines)
Lines 18-23 package Koha::REST::Plugin::Query; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Mojo::Base 'Mojolicious::Plugin';
20
use Mojo::Base 'Mojolicious::Plugin';
21
use Scalar::Util qw(reftype);
21
22
22
use Koha::Exceptions;
23
use Koha::Exceptions;
23
24
Lines 80-90 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
80
        'dbic_merge_sorting' => sub {
81
        'dbic_merge_sorting' => sub {
81
            my ( $c, $args ) = @_;
82
            my ( $c, $args ) = @_;
82
            my $attributes = $args->{attributes};
83
            my $attributes = $args->{attributes};
84
            my $to_model   = $args->{to_model};
83
85
84
            if ( defined $args->{params}->{_order_by} ) {
86
            if ( defined $args->{params}->{_order_by} ) {
85
                my @order_by = map { _build_order_atom($_) }
87
                my $order_by = $args->{params}->{_order_by};
86
                               @{ $args->{params}->{_order_by} };
88
                if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
87
                $attributes->{order_by} = \@order_by;
89
                    my @order_by = map { _build_order_atom( $_, $to_model) }
90
                                @{ $args->{params}->{_order_by} };
91
                    $attributes->{order_by} = \@order_by;
92
                }
93
                else {
94
                    $attributes->{order_by} = _build_order_atom( $order_by, $to_model );
95
                }
88
            }
96
            }
89
97
90
            return $attributes;
98
            return $attributes;
Lines 166-186 according to the following rules: Link Here
166
174
167
sub _build_order_atom {
175
sub _build_order_atom {
168
    my $string = shift;
176
    my $string = shift;
177
    my $to_model = shift;
178
179
    # FIXME: This should be done differently once 23893 is pushed
180
    #        and we have access to the to_model_mapping hash
181
    my $param = $string;
182
    $param =~ s/^(\+|\-|\s)//;
183
    $param = (keys %{$to_model->({ $param => 1 })})[0]
184
        if $to_model;
169
185
170
    if ( $string =~ m/^\+/ or
186
    if ( $string =~ m/^\+/ or
171
         $string =~ m/^\s/ ) {
187
         $string =~ m/^\s/ ) {
172
        # asc order operator present
188
        # asc order operator present
173
        $string =~ s/^(\+|\s)//;
189
        return { -asc => $param };
174
        return { -asc => $string };
175
    }
190
    }
176
    elsif ( $string =~ m/^\-/ ) {
191
    elsif ( $string =~ m/^\-/ ) {
177
        # desc order operator present
192
        # desc order operator present
178
        $string =~ s/^\-//;
193
        return { -desc => $param };
179
        return { -desc => $string };
180
    }
194
    }
181
    else {
195
    else {
182
        # no order operator present
196
        # no order operator present
183
        return $string;
197
        return $param;
184
    }
198
    }
185
}
199
}
186
200
(-)a/t/Koha/REST/Plugin/Query.t (-2 / +50 lines)
Lines 80-85 get '/dbic_merge_sorting' => sub { Link Here
80
    $c->render( json => $attributes, status => 200 );
80
    $c->render( json => $attributes, status => 200 );
81
};
81
};
82
82
83
get '/dbic_merge_sorting_single' => sub {
84
    my $c = shift;
85
    my $attributes = { a => 'a', b => 'b' };
86
    $attributes = $c->dbic_merge_sorting(
87
        {
88
            attributes => $attributes,
89
            params     => { _match => 'exact', _order_by => '-uno' }
90
        }
91
    );
92
    $c->render( json => $attributes, status => 200 );
93
};
94
95
get '/dbic_merge_sorting_to_model' => sub {
96
    my $c = shift;
97
    my $attributes = { a => 'a', b => 'b' };
98
    $attributes = $c->dbic_merge_sorting(
99
        {
100
            attributes => $attributes,
101
            params     => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] },
102
            to_model   => \&to_model
103
        }
104
    );
105
    $c->render( json => $attributes, status => 200 );
106
};
107
83
get '/build_query' => sub {
108
get '/build_query' => sub {
84
    my $c = shift;
109
    my $c = shift;
85
    my ( $filtered_params, $reserved_params ) =
110
    my ( $filtered_params, $reserved_params ) =
Lines 97-102 get '/build_query' => sub { Link Here
97
    };
122
    };
98
};
123
};
99
124
125
sub to_model {
126
    my ($args) = @_;
127
    $args->{three} = delete $args->{tres}
128
        if exists $args->{tres};
129
    return $args;
130
}
131
100
# The tests
132
# The tests
101
133
102
use Test::More tests => 3;
134
use Test::More tests => 3;
Lines 131-137 subtest 'extract_reserved_params() tests' => sub { Link Here
131
163
132
subtest 'dbic_merge_sorting() tests' => sub {
164
subtest 'dbic_merge_sorting() tests' => sub {
133
165
134
    plan tests => 5;
166
    plan tests => 15;
135
167
136
    my $t = Test::Mojo->new;
168
    my $t = Test::Mojo->new;
137
169
Lines 145-150 subtest 'dbic_merge_sorting() tests' => sub { Link Here
145
            { -asc  => 'cuatro' }
177
            { -asc  => 'cuatro' }
146
        ]
178
        ]
147
      );
179
      );
180
181
    $t->get_ok('/dbic_merge_sorting_to_model')->status_is(200)
182
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
183
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
184
        '/order_by' => [
185
            'uno',
186
            { -desc => 'dos' },
187
            { -asc  => 'three' },
188
            { -asc  => 'cuatro' }
189
        ]
190
      );
191
192
    $t->get_ok('/dbic_merge_sorting_single')->status_is(200)
193
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
194
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
195
        '/order_by' => { '-desc' => 'uno' }
196
      );
148
};
197
};
149
198
150
subtest '_build_query_params_from_api' => sub {
199
subtest '_build_query_params_from_api' => sub {
151
- 

Return to bug 24191