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

(-)a/Koha/REST/Plugin/Query.pm (-9 / +9 lines)
Lines 81-97 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
81
        'dbic_merge_sorting' => sub {
81
        'dbic_merge_sorting' => sub {
82
            my ( $c, $args ) = @_;
82
            my ( $c, $args ) = @_;
83
            my $attributes = $args->{attributes};
83
            my $attributes = $args->{attributes};
84
            my $to_model   = $args->{to_model};
84
            my $result_set = $args->{result_set};
85
85
86
            if ( defined $args->{params}->{_order_by} ) {
86
            if ( defined $args->{params}->{_order_by} ) {
87
                my $order_by = $args->{params}->{_order_by};
87
                my $order_by = $args->{params}->{_order_by};
88
                if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
88
                if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
89
                    my @order_by = map { _build_order_atom( $_, $to_model) }
89
                    my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
90
                                @{ $args->{params}->{_order_by} };
90
                                @{ $args->{params}->{_order_by} };
91
                    $attributes->{order_by} = \@order_by;
91
                    $attributes->{order_by} = \@order_by;
92
                }
92
                }
93
                else {
93
                else {
94
                    $attributes->{order_by} = _build_order_atom( $order_by, $to_model );
94
                    $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
95
                }
95
                }
96
            }
96
            }
97
97
Lines 173-187 according to the following rules: Link Here
173
=cut
173
=cut
174
174
175
sub _build_order_atom {
175
sub _build_order_atom {
176
    my $string = shift;
176
    my ( $args )   = @_;
177
    my $to_model = shift;
177
    my $string     = $args->{string};
178
    my $result_set = $args->{result_set};
178
179
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;
180
    my $param = $string;
182
    $param =~ s/^(\+|\-|\s)//;
181
    $param =~ s/^(\+|\-|\s)//;
183
    $param = (keys %{$to_model->({ $param => 1 })})[0]
182
    if ( $result_set ) {
184
        if $to_model;
183
        $param = (keys %{$result_set->attributes_from_api({ $param => 1 })})[0];
184
    }
185
185
186
    if ( $string =~ m/^\+/ or
186
    if ( $string =~ m/^\+/ or
187
         $string =~ m/^\s/ ) {
187
         $string =~ m/^\s/ ) {
(-)a/t/Koha/REST/Plugin/Query.t (-16 / +11 lines)
Lines 21-26 use Modern::Perl; Link Here
21
use Mojolicious::Lite;
21
use Mojolicious::Lite;
22
use Try::Tiny;
22
use Try::Tiny;
23
23
24
use Koha::Cities;
25
24
app->log->level('error');
26
app->log->level('error');
25
27
26
plugin 'Koha::REST::Plugin::Query';
28
plugin 'Koha::REST::Plugin::Query';
Lines 92-105 get '/dbic_merge_sorting_single' => sub { Link Here
92
    $c->render( json => $attributes, status => 200 );
94
    $c->render( json => $attributes, status => 200 );
93
};
95
};
94
96
95
get '/dbic_merge_sorting_to_model' => sub {
97
get '/dbic_merge_sorting_result_set' => sub {
96
    my $c = shift;
98
    my $c = shift;
97
    my $attributes = { a => 'a', b => 'b' };
99
    my $attributes = { a => 'a', b => 'b' };
100
    my $result_set = Koha::Cities->new;
98
    $attributes = $c->dbic_merge_sorting(
101
    $attributes = $c->dbic_merge_sorting(
99
        {
102
        {
100
            attributes => $attributes,
103
            attributes => $attributes,
101
            params     => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] },
104
            params     => { _match => 'exact', _order_by => [ 'name', '-postal_code', '+country', ' state' ] },
102
            to_model   => \&to_model
105
            result_set => $result_set
103
        }
106
        }
104
    );
107
    );
105
    $c->render( json => $attributes, status => 200 );
108
    $c->render( json => $attributes, status => 200 );
Lines 122-134 get '/build_query' => sub { Link Here
122
    };
125
    };
123
};
126
};
124
127
125
sub to_model {
126
    my ($args) = @_;
127
    $args->{three} = delete $args->{tres}
128
        if exists $args->{tres};
129
    return $args;
130
}
131
132
# The tests
128
# The tests
133
129
134
use Test::More tests => 3;
130
use Test::More tests => 3;
Lines 178-191 subtest 'dbic_merge_sorting() tests' => sub { Link Here
178
        ]
174
        ]
179
      );
175
      );
180
176
181
    $t->get_ok('/dbic_merge_sorting_to_model')->status_is(200)
177
    $t->get_ok('/dbic_merge_sorting_result_set')->status_is(200)
182
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
178
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
183
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
179
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
184
        '/order_by' => [
180
        '/order_by' => [
185
            'uno',
181
            'city_name',
186
            { -desc => 'dos' },
182
            { -desc => 'city_zipcode' },
187
            { -asc  => 'three' },
183
            { -asc  => 'city_country' },
188
            { -asc  => 'cuatro' }
184
            { -asc  => 'city_state' }
189
        ]
185
        ]
190
      );
186
      );
191
187
192
- 

Return to bug 24321