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

(-)a/Koha/REST/Plugin/Query.pm (-3 / +4 lines)
Lines 81-87 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
81
81
82
            if ( defined $args->{params}->{_order_by} ) {
82
            if ( defined $args->{params}->{_order_by} ) {
83
                my @order_by = map { _build_order_atom($_) }
83
                my @order_by = map { _build_order_atom($_) }
84
                                split( /\|/, $args->{params}->{_order_by} );
84
                               @{ $args->{params}->{_order_by} };
85
                $attributes->{order_by} = \@order_by;
85
                $attributes->{order_by} = \@order_by;
86
            }
86
            }
87
87
Lines 120-128 according to the following rules: Link Here
120
sub _build_order_atom {
120
sub _build_order_atom {
121
    my $string = shift;
121
    my $string = shift;
122
122
123
    if ( $string =~ m/^\+/ ) {
123
    if ( $string =~ m/^\+/ or
124
         $string =~ m/^\s/ ) {
124
        # asc order operator present
125
        # asc order operator present
125
        $string =~ s/^\+//;
126
        $string =~ s/^(\+|\s)//;
126
        return { -asc => $string };
127
        return { -asc => $string };
127
    }
128
    }
128
    elsif ( $string =~ m/^\-/ ) {
129
    elsif ( $string =~ m/^\-/ ) {
(-)a/t/Koha/REST/Plugin/Query.t (-6 / +47 lines)
Lines 73-79 get '/dbic_merge_sorting' => sub { Link Here
73
    $attributes = $c->dbic_merge_sorting(
73
    $attributes = $c->dbic_merge_sorting(
74
        {
74
        {
75
            attributes => $attributes,
75
            attributes => $attributes,
76
            params     => { _match => 'exact', _order_by => 'uno|-dos|+tres' }
76
            params     => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] }
77
        }
77
        }
78
    );
78
    );
79
    $c->render( json => $attributes, status => 200 );
79
    $c->render( json => $attributes, status => 200 );
Lines 117-125 subtest 'dbic_merge_sorting() tests' => sub { Link Here
117
117
118
    my $t = Test::Mojo->new;
118
    my $t = Test::Mojo->new;
119
119
120
    $t->get_ok('/dbic_merge_sorting')
120
    $t->get_ok('/dbic_merge_sorting')->status_is(200)
121
      ->status_is(200)
122
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
121
      ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
123
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )
122
      ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
124
      ->json_is( '/order_by' => [ 'uno', { -desc => 'dos' }, { -asc => 'tres' } ] );
123
        '/order_by' => [
124
            'uno',
125
            { -desc => 'dos' },
126
            { -asc  => 'tres' },
127
            { -asc  => 'cuatro' }
128
        ]
129
      );
130
};
131
132
subtest '_build_query_params_from_api' => sub {
133
134
    plan tests => 16;
135
136
    my $t = Test::Mojo->new;
137
138
    # _match => contains
139
    $t->get_ok('/build_query?_match=contains&title=Ender&author=Orson')
140
      ->status_is(200)
141
      ->json_is( '/query' =>
142
          { author => { like => '%Orson%' }, title => { like => '%Ender%' } } );
143
144
    # _match => starts_with
145
    $t->get_ok('/build_query?_match=starts_with&title=Ender&author=Orson')
146
      ->status_is(200)
147
      ->json_is( '/query' =>
148
          { author => { like => 'Orson%' }, title => { like => 'Ender%' } } );
149
150
    # _match => ends_with
151
    $t->get_ok('/build_query?_match=ends_with&title=Ender&author=Orson')
152
      ->status_is(200)
153
      ->json_is( '/query' =>
154
          { author => { like => '%Orson' }, title => { like => '%Ender' } } );
155
156
    # _match => exact
157
    $t->get_ok('/build_query?_match=exact&title=Ender&author=Orson')
158
      ->status_is(200)
159
      ->json_is( '/query' => { author => 'Orson', title => 'Ender' } );
160
161
    # _match => blah
162
    $t->get_ok('/build_query?_match=blah&title=Ender&author=Orson')
163
      ->status_is(400)
164
      ->json_is( '/exception_msg'  => 'Invalid value for _match param (blah)' )
165
      ->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
166
125
};
167
};
126
- 

Return to bug 19370