[Bps-public-commit] rt-extension-rest2 branch, master, updated. 1.04-24-g8029ee5

? sunnavy sunnavy at bestpractical.com
Wed Apr 3 16:10:15 EDT 2019


The branch, master has been updated
       via  8029ee5070a88bcbe20a12ee44aef80ead34e76a (commit)
       via  d7828af709f5138dce5084da651fc3740cf25785 (commit)
       via  18981ed9dd2af8c8cac2679b12b22c99e437115e (commit)
       via  2a11ae8f2adb4ef71193d4011bb926d34c13dc57 (commit)
       via  f82b6d81c01c3f1435de9ba86f61dc840752e45b (commit)
       via  123f24d1a008ed4a7b364216d57c49171114941c (commit)
       via  dd04ef06e20f64b78fc43155f9453d1c8b1b4974 (commit)
      from  cd9ddaad763d30c95b724fc2e6ec85c07b1c3495 (commit)

Summary of changes:
 README                                             | 19 ++++++++++++
 lib/RT/Extension/REST2.pm                          | 20 ++++++++++++
 .../REST2/Resource/Collection/QueryByJSON.pm       | 12 ++++++++
 lib/RT/Extension/REST2/Resource/Tickets.pm         | 13 ++++++++
 t/search-json.t                                    | 35 +++++++++++++++++++--
 t/tickets.t                                        | 36 ++++++++++++++++++++++
 6 files changed, 132 insertions(+), 3 deletions(-)

- Log -----------------------------------------------------------------
commit dd04ef06e20f64b78fc43155f9453d1c8b1b4974
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 18:48:57 2018 +0200

    Add 'orderby' and 'order' multiple query parameters to JSON search

diff --git a/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm b/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
index a544735..a834042 100644
--- a/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/Extension/REST2/Resource/Collection/QueryByJSON.pm
@@ -59,6 +59,18 @@ sub limit_collection {
             CASESENSITIVE => ($limit->{case_sensitive} || 0),
         );
     }
+
+    my @orderby_cols;
+    my @orders = $self->request->param('order');
+    foreach my $orderby ($self->request->param('orderby')) {
+        my $order = shift @orders || 'ASC';
+        $order = uc($order);
+        $order = 'ASC' unless $order eq 'DESC';
+        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
+    }
+    $self->collection->OrderByCols(@orderby_cols)
+        if @orderby_cols;
+
     return 1;
 }
 

commit 123f24d1a008ed4a7b364216d57c49171114941c
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 18:49:53 2018 +0200

    Add 'orderby' and 'order' multiple query parameters to TicketSQL search

diff --git a/lib/RT/Extension/REST2/Resource/Tickets.pm b/lib/RT/Extension/REST2/Resource/Tickets.pm
index deff2fb..ee8eb58 100644
--- a/lib/RT/Extension/REST2/Resource/Tickets.pm
+++ b/lib/RT/Extension/REST2/Resource/Tickets.pm
@@ -50,6 +50,19 @@ sub limit_collection {
     my $self = shift;
     my ($ok, $msg) = $self->collection->FromSQL( $self->query );
     return error_as_json( $self->response, 0, $msg ) unless $ok;
+
+    my @orderby_cols;
+    my @orders = $self->request->param('order');
+    foreach my $orderby ($self->request->param('orderby')) {
+        $orderby = decode_utf8($orderby);
+        my $order = shift @orders || 'ASC';
+        $order = uc(decode_utf8($order));
+        $order = 'ASC' unless $order eq 'DESC';
+        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
+    }
+    $self->collection->OrderByCols(@orderby_cols)
+        if @orderby_cols;
+
     return 1;
 }
 

commit f82b6d81c01c3f1435de9ba86f61dc840752e45b
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 18:50:21 2018 +0200

    Add tests for 'orderby' and 'order' multiple query parameters to JSON search

diff --git a/t/search-json.t b/t/search-json.t
index 5efd849..6cac523 100644
--- a/t/search-json.t
+++ b/t/search-json.t
@@ -8,9 +8,9 @@ my $auth = RT::Extension::REST2::Test->authorization_header;
 my $rest_base_path = '/REST/2.0';
 my $user = RT::Extension::REST2::Test->user;
 
-my $alpha = RT::Test->load_or_create_queue( Name => 'Alpha' );
-my $beta  = RT::Test->load_or_create_queue( Name => 'Beta' );
-my $bravo = RT::Test->load_or_create_queue( Name => 'Bravo' );
+my $alpha = RT::Test->load_or_create_queue( Name => 'Alpha', Description => 'Queue for test' );
+my $beta  = RT::Test->load_or_create_queue( Name => 'Beta', Description => 'Queue for test' );
+my $bravo = RT::Test->load_or_create_queue( Name => 'Bravo', Description => 'Queue to test sorted search' );
 $user->PrincipalObj->GrantRight( Right => 'SuperUser' );
 
 my $alpha_id = $alpha->Id;
@@ -138,5 +138,34 @@ my $bravo_id = $bravo->Id;
     is($content->{message}, 'JSON object must be a ARRAY');
 }
 
+# Sorted search
+{
+    my $res = $mech->post_json("$rest_base_path/queues?orderby=Description&order=DESC&orderby=id",
+        [{ field => 'Description', operator => 'LIKE', value => 'test' }],
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    is($content->{count}, 3);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 3);
+    is(scalar @{$content->{items}}, 3);
+
+    my ($first, $second, $third) = @{ $content->{items} };
+    is($first->{type}, 'queue');
+    is($first->{id}, $bravo_id);
+    like($first->{_url}, qr{$rest_base_path/queue/$bravo_id$});
+
+    is($second->{type}, 'queue');
+    is($second->{id}, $alpha_id);
+    like($second->{_url}, qr{$rest_base_path/queue/$alpha_id$});
+
+    is($third->{type}, 'queue');
+    is($third->{id}, $beta_id);
+    like($third->{_url}, qr{$rest_base_path/queue/$beta_id$});
+}
+
 done_testing;
 

commit 2a11ae8f2adb4ef71193d4011bb926d34c13dc57
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 18:50:37 2018 +0200

    Add tests for 'orderby' and 'order' multiple query parameters to TicketSQL search

diff --git a/t/tickets.t b/t/tickets.t
index ae151c1..56bbf7d 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -357,4 +357,40 @@ my ($ticket_url, $ticket_id);
     is($content->{ContentType}, 'text/html');
 }
 
+# Ticket Sorted Search
+{
+    my $ticket2 = RT::Ticket->new($RT::SystemUser);
+    ok(my ($ticket2_id) = $ticket2->Create(Queue => 'General', Subject => 'Ticket for test'));
+    my $ticket3 = RT::Ticket->new($RT::SystemUser);
+    ok(my ($ticket3_id) = $ticket3->Create(Queue => 'General', Subject => 'Ticket for test'));
+    my $ticket4 = RT::Ticket->new($RT::SystemUser);
+    ok(my ($ticket4_id) = $ticket4->Create(Queue => 'General', Subject => 'Ticket to test sorted search'));
+
+    my $res = $mech->get("$rest_base_path/tickets?query=Subject LIKE 'test'&orderby=Subject&order=DESC&orderby=id",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    my $content = $mech->json_response;
+    is($content->{count}, 3);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 3);
+    is(scalar @{$content->{items}}, 3);
+
+    my $first_ticket = $content->{items}->[0];
+    is($first_ticket->{type}, 'ticket');
+    is($first_ticket->{id}, $ticket4_id);
+    like($first_ticket->{_url}, qr{$rest_base_path/ticket/$ticket4_id$});
+
+    my $second_ticket = $content->{items}->[1];
+    is($second_ticket->{type}, 'ticket');
+    is($second_ticket->{id}, $ticket2_id);
+    like($second_ticket->{_url}, qr{$rest_base_path/ticket/$ticket2_id$});
+
+    my $third_ticket = $content->{items}->[2];
+    is($third_ticket->{type}, 'ticket');
+    is($third_ticket->{id}, $ticket3_id);
+    like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
+}
+
 done_testing;

commit 18981ed9dd2af8c8cac2679b12b22c99e437115e
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 18:52:37 2018 +0200

    Add documentation for 'orderby' and 'order' multiple query parameters

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index d5dba67..c71734a 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -568,6 +568,26 @@ values).  An example:
 The JSON payload must be an array of hashes with the keys C<field> and C<value>
 and optionally C<operator>.
 
+Results can be sorted by using multiple query parameter arguments
+C<orderby> and C<order>. Each C<orderby> query parameter specify a field
+to be used for sorting results. If the request includes more than one
+C<orderby> query parameter, results are sorted according to
+corresponding fields in the same order than they are specified. For
+instance, if you want to sort results according to creation date and
+then by id (in case of some items have the same creation date), your
+request should specify C<?orderby=Created&orderby=id>. By default,
+results are sorted in ascending order. To sort results in descending
+order, you should use C<order=DESC> query parameter. Any other value for
+C<order> query parameter will be treated as C<order=ASC>, for ascending
+order. The order of the C<order> query parameters should be the same as
+the C<orderby> query parameters. Therefore, if you specify two fields to
+sort the results (with two C<orderby> parameters) and you want to sort
+the second field by descending order, you should also explicitely
+specify C<order=ASC> for the first field:
+C<orderby=Created&order=ASC&orderby=id&order=DESC>. C<orderby> and
+C<order> query parameters are supported in both JSON and TicketSQL
+searches.
+
 Results are returned in
 L<the format described below|/"Example of plural resources (collections)">.
 

commit d7828af709f5138dce5084da651fc3740cf25785
Author: gibus <gibus at easter-eggs.com>
Date:   Sat Oct 20 23:25:33 2018 +0200

    Sync README with Module::Install::ReadmeFromPod

diff --git a/README b/README
index c955661..2bf9d1a 100644
--- a/README
+++ b/README
@@ -519,6 +519,25 @@ USAGE
     The JSON payload must be an array of hashes with the keys field and
     value and optionally operator.
 
+    Results can be sorted by using multiple query parameter arguments
+    orderby and order. Each orderby query parameter specify a field to be
+    used for sorting results. If the request includes more than one orderby
+    query parameter, results are sorted according to corresponding fields in
+    the same order than they are specified. For instance, if you want to
+    sort results according to creation date and then by id (in case of some
+    items have the same creation date), your request should specify
+    ?orderby=Created&orderby=id. By default, results are sorted in ascending
+    order. To sort results in descending order, you should use order=DESC
+    query parameter. Any other value for order query parameter will be
+    treated as order=ASC, for ascending order. The order of the order query
+    parameters should be the same as the orderby query parameters.
+    Therefore, if you specify two fields to sort the results (with two
+    orderby parameters) and you want to sort the second field by descending
+    order, you should also explicitely specify order=ASC for the first
+    field: orderby=Created&order=ASC&orderby=id&order=DESC. orderby and
+    order query parameters are supported in both JSON and TicketSQL
+    searches.
+
     Results are returned in the format described below.
 
   Example of plural resources (collections)

commit 8029ee5070a88bcbe20a12ee44aef80ead34e76a
Merge: cd9ddaa d7828af
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Apr 4 04:05:49 2019 +0800

    Merge branch 'orderby-searches'


-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list