[Rt-commit] rt branch, 5.0/rest2-add-asset-and-transaction-sql-endpoints, created. rt-5.0.0-4-g07c9f268dc
Craig Kaiser
craig at bestpractical.com
Thu Mar 4 15:38:19 EST 2021
The branch, 5.0/rest2-add-asset-and-transaction-sql-endpoints has been created
at 07c9f268dc3d3ac9f83b574b62f6d760d2abf9e0 (commit)
- Log -----------------------------------------------------------------
commit 8e9addf313cd2838fcd95320baee6417540ce3bc
Author: craig kaiser <craig at bestpractical.com>
Date: Fri Feb 26 15:57:06 2021 -0500
Add RT::REST2::Collection::QueryBySQL role
Break our JSON and SQL search into two seperate methods that are called
by RT::REST2::Collection::limit_collection.
diff --git a/lib/RT/REST2/Resource/Assets.pm b/lib/RT/REST2/Resource/Assets.pm
index 907534e9c6..676b559a58 100644
--- a/lib/RT/REST2/Resource/Assets.pm
+++ b/lib/RT/REST2/Resource/Assets.pm
@@ -55,6 +55,7 @@ use namespace::autoclean;
extends 'RT::REST2::Resource::Collection';
with 'RT::REST2::Resource::Collection::QueryByJSON';
+with 'RT::REST2::Resource::Collection::QueryBySQL';
sub dispatch_rules {
Path::Dispatcher::Rule::Regex->new(
diff --git a/lib/RT/REST2/Resource/Collection.pm b/lib/RT/REST2/Resource/Collection.pm
index 39cfecc2e2..46f4943739 100644
--- a/lib/RT/REST2/Resource/Collection.pm
+++ b/lib/RT/REST2/Resource/Collection.pm
@@ -99,6 +99,24 @@ sub limit_collection {
$collection->{'find_disabled_rows'} = 1
if $self->request->param('find_disabled_rows');
+ 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;
+
+ if ( $self->can('query_json') && scalar @{$self->query_json} ) {
+ return $self->limit_collection_from_json();
+ }
+ elsif ( $self->can('query_sql') && $self->query_sql ) {
+ return $self->limit_collection_from_sql();
+ }
+
return 1;
}
diff --git a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
index 9b415d575f..dded940180 100644
--- a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
@@ -63,19 +63,22 @@ with (
requires 'collection';
-has 'query' => (
+has 'query_json' => (
is => 'ro',
isa => 'ArrayRef[HashRef]',
required => 1,
lazy_build => 1,
);
-sub _build_query {
+sub _build_query_json {
my $self = shift;
my $content = $self->request->method eq 'GET'
? $self->request->param('query')
: $self->request->content;
- return $content ? JSON::decode_json($content) : [];
+ return [] unless $content && $content =~ /\s*\[/;
+
+ $content = $content ? JSON::decode_json($content) : [];
+ return $content;
}
sub allowed_methods {
@@ -86,10 +89,10 @@ sub searchable_fields {
$_[0]->collection->RecordClass->ReadableAttributes
}
-sub limit_collection {
+sub limit_collection_from_json {
my $self = shift;
my $collection = $self->collection;
- my $query = $self->query;
+ my $query = $self->query_json;
my @fields = $self->searchable_fields;
my %searchable = map {; $_ => 1 } @fields;
@@ -114,17 +117,6 @@ sub limit_collection {
);
}
- 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;
}
diff --git a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm b/lib/RT/REST2/Resource/Collection/QueryBySQL.pm
similarity index 62%
copy from lib/RT/REST2/Resource/Collection/QueryByJSON.pm
copy to lib/RT/REST2/Resource/Collection/QueryBySQL.pm
index 9b415d575f..a0714bab56 100644
--- a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/REST2/Resource/Collection/QueryBySQL.pm
@@ -46,79 +46,62 @@
#
# END BPS TAGGED BLOCK }}}
-package RT::REST2::Resource::Collection::QueryByJSON;
+package RT::REST2::Resource::Collection::QueryBySQL;
use strict;
use warnings;
use Moose::Role;
use namespace::autoclean;
-use JSON ();
-
-with (
- 'RT::REST2::Resource::Collection::ProcessPOSTasGET',
- 'RT::REST2::Resource::Role::RequestBodyIsJSON'
- => { type => 'ARRAY' },
-);
+use Encode qw( decode_utf8 );
+use RT::REST2::Util qw( error_as_json );
requires 'collection';
-has 'query' => (
+has 'query_sql' => (
is => 'ro',
- isa => 'ArrayRef[HashRef]',
+ isa => 'Str|ArrayRef[HashRef]',
required => 1,
lazy_build => 1,
);
-sub _build_query {
- my $self = shift;
- my $content = $self->request->method eq 'GET'
- ? $self->request->param('query')
- : $self->request->content;
- return $content ? JSON::decode_json($content) : [];
-}
+sub _build_query_sql {
+ my $self = shift;
-sub allowed_methods {
- [ 'GET', 'POST' ]
-}
+ my $query_sql = "";
+ if ( $self->request->method eq 'GET' && $self->request->param('query') ) {
+ $query_sql = decode_utf8($self->request->param('query') || "");
+ }
+ elsif ( $self->request->method eq 'GET' ) {
+ my $content = $self->request->param('query');
+ return "" unless $content;
-sub searchable_fields {
- $_[0]->collection->RecordClass->ReadableAttributes
-}
+ eval { $query_sql = JSON::decode_json($content); };
+ return "" if $@;
+ }
+ elsif ( $self->request->method eq 'POST' ) {
+ my $content = $self->request->content;
+ return "" unless $content;
-sub limit_collection {
- my $self = shift;
- my $collection = $self->collection;
- my $query = $self->query;
- my @fields = $self->searchable_fields;
- my %searchable = map {; $_ => 1 } @fields;
+ eval { $query_sql = JSON::decode_json($content); };
+ return "" if $@;
+ }
- $collection->{'find_disabled_rows'} = 1
- if $self->request->param('find_disabled_rows');
+ return $query_sql;
+}
- for my $limit (@$query) {
- next unless $limit->{field}
- and $searchable{$limit->{field}}
- and defined $limit->{value};
+sub limit_collection_from_sql {
+ my $self = shift;
- $collection->Limit(
- FIELD => $limit->{field},
- VALUE => $limit->{value},
- ( $limit->{operator}
- ? (OPERATOR => $limit->{operator})
- : () ),
- CASESENSITIVE => ($limit->{case_sensitive} || 0),
- ( $limit->{entry_aggregator}
- ? (ENTRYAGGREGATOR => $limit->{entry_aggregator})
- : () ),
- );
- }
+ my ($ok, $msg) = $self->collection->FromSQL( $self->query_sql );
+ 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($order);
+ $order = uc(decode_utf8($order));
$order = 'ASC' unless $order eq 'DESC';
push @orderby_cols, {FIELD => $orderby, ORDER => $order};
}
@@ -126,6 +109,6 @@ sub limit_collection {
if @orderby_cols;
return 1;
-}
+};
1;
diff --git a/lib/RT/REST2/Resource/Transactions.pm b/lib/RT/REST2/Resource/Transactions.pm
index 719a4e754a..fe0527c8ea 100644
--- a/lib/RT/REST2/Resource/Transactions.pm
+++ b/lib/RT/REST2/Resource/Transactions.pm
@@ -55,6 +55,7 @@ use namespace::autoclean;
extends 'RT::REST2::Resource::Collection';
with 'RT::REST2::Resource::Collection::QueryByJSON';
+with 'RT::REST2::Resource::Collection::QueryBySQL';
sub dispatch_rules {
Path::Dispatcher::Rule::Regex->new(
commit 258baf1819f1b4a5e11cfc5b8a88484620e3deec
Author: craig kaiser <craig at bestpractical.com>
Date: Mon Mar 1 09:55:57 2021 -0500
Preserve first_row and show_rows across the CleanSlate
diff --git a/lib/RT/Transactions.pm b/lib/RT/Transactions.pm
index 43458aaaf9..88ae9c21aa 100644
--- a/lib/RT/Transactions.pm
+++ b/lib/RT/Transactions.pm
@@ -1080,8 +1080,12 @@ sub _parser {
sub FromSQL {
my ($self,$query) = @_;
- $self->CleanSlate;
- $self->_InitSQL;
+ {
+ # preserve first_row and show_rows across the CleanSlate
+ local ($self->{'first_row'}, $self->{'show_rows'}, $self->{_sql_looking_at});
+ $self->CleanSlate;
+ $self->_InitSQL();
+ }
return (1, $self->loc("No Query")) unless $query;
commit 8064bb200a76744a5c63c88a30a640972f357ee0
Author: craig kaiser <craig at bestpractical.com>
Date: Mon Mar 1 13:07:00 2021 -0500
Add test for REST2 assetSQL search
diff --git a/t/rest2/assets.t b/t/rest2/assets.t
index 1f1712bfed..619564ae9c 100644
--- a/t/rest2/assets.t
+++ b/t/rest2/assets.t
@@ -142,6 +142,23 @@ my ($asset_url, $asset_id);
is($asset->{type}, 'asset');
is($asset->{id}, 1);
like($asset->{_url}, qr{$rest_base_path/asset/1$});
+
+ # Ensure our JSON search matches the assetSQL search
+ $res = $mech->get("$rest_base_path/assets?query=id>0",
+ 'Authorization' => $auth,
+ );
+ is($res->code, 200);
+ $content = $mech->json_response;
+ is($content->{count}, 1);
+ is($content->{page}, 1);
+ is($content->{per_page}, 20);
+ is($content->{total}, 1);
+ is(scalar @{$content->{items}}, 1);
+
+ $asset = $content->{items}->[0];
+ is($asset->{type}, 'asset');
+ is($asset->{id}, 1);
+ like($asset->{_url}, qr{$rest_base_path/asset/1$});
}
# Asset Update
commit 07c9f268dc3d3ac9f83b574b62f6d760d2abf9e0
Author: craig kaiser <craig at bestpractical.com>
Date: Mon Mar 1 13:09:28 2021 -0500
Add test for REST2 transactionSQL search
diff --git a/t/rest2/transactions.t b/t/rest2/transactions.t
index 65e705d8f9..6096b54c6c 100644
--- a/t/rest2/transactions.t
+++ b/t/rest2/transactions.t
@@ -59,6 +59,35 @@ my ($comment_txn_url, $comment_txn_id);
ok(($comment_txn_id) = $comment_txn_url =~ qr[/transaction/(\d+)]);
}
+# search transactions for a specific ticket using TransactionSQL
+{
+ my $res = $mech->get("$rest_base_path/transactions?query=ObjectType='RT::Ticket' AND ObjectId=".$ticket->Id,
+ 'Authorization' => $auth,
+ );
+ is($res->code, 200);
+
+ my $content = $mech->json_response;
+ is($content->{count}, 5);
+ is($content->{page}, 1);
+ is($content->{per_page}, 20);
+ is($content->{total}, 5);
+ is(scalar @{$content->{items}}, 5);
+
+ my ($create, $priority1, $subject, $priority2, $comment) = @{ $content->{items} };
+
+ is($create->{type}, 'transaction');
+ is($priority1->{type}, 'transaction');
+ is($subject->{type}, 'transaction');
+ is($priority2->{type}, 'transaction');
+ is($comment->{type}, 'transaction');
+
+ $create_txn_url = $create->{_url};
+ ok(($create_txn_id) = $create_txn_url =~ qr[/transaction/(\d+)]);
+
+ $comment_txn_url = $comment->{_url};
+ ok(($comment_txn_id) = $comment_txn_url =~ qr[/transaction/(\d+)]);
+}
+
# Transaction display
{
my $res = $mech->get($create_txn_url,
-----------------------------------------------------------------------
More information about the rt-commit
mailing list