[Catalyst-commits] r8112 - in Catalyst-Controller-DBIC-API/1.000/trunk: lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API t/lib/RestTest/Controller/API/RPC t/rpc

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Sun Jul 13 19:56:57 BST 2008


Author: lukes
Date: 2008-07-13 19:56:56 +0100 (Sun, 13 Jul 2008)
New Revision: 8112

Modified:
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
Log:
added list_grouped_by and list_ordered_by attrs and made them and list_returns overridable in request

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-07-13 16:11:22 UTC (rev 8111)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-07-13 18:56:56 UTC (rev 8112)
@@ -6,7 +6,7 @@
 use base qw/Catalyst::Controller CGI::Expand/;
 
 __PACKAGE__->mk_accessors(qw(
-  class create_requires update_requires update_allows $self->rs_stash_key create_allows list_returns rs_stash_key object_stash_key
+  class create_requires update_requires update_allows $self->rs_stash_key create_allows list_returns list_grouped_by list_ordered_by rs_stash_key object_stash_key
 ));
 
 __PACKAGE__->config(
@@ -16,6 +16,8 @@
   update_requires => [],
   update_allows => [],
   list_returns => [],
+  list_grouped_by => [],
+  list_ordered_by => [],
   object_stash_key => 'object',
   rs_stash_key => 'class_rs'
 );
@@ -29,19 +31,35 @@
 sub list :Private {
   my ($self, $c) = @_;
 
+  my ($params, $args) = @{$c->forward('generate_dbic_search_args')};
+
+#  use Data::Dumper; warn Dumper($params, $args);
+  $c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search($params, $args);
+
+  $c->forward('format_list');
+}
+
+sub generate_dbic_search_args :Private {
+  my ($self, $c) = @_;
+
   my $req_params = (grep { ref $_ } values %{$c->req->params}) ? $c->req->params : $self->expand_hash($c->req->params);
-
+#  use Data::Dumper; warn Dumper($req_params);
   my $source = $c->stash->{$self->rs_stash_key}->result_source;
-  my @columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : ();
 
-  my %search_params;
-  
-  my ($params, $join) = $self->_format_search({ params => $req_params->{search}, source => $source }) if ($req_params->{search});
+  my @default_columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : ();
+  my @default_grouped = (scalar(@{$self->list_grouped_by})) ? @{$self->list_grouped_by} : ();
+  my @default_ordered = (scalar(@{$self->list_ordered_by})) ? @{$self->list_ordered_by} : ();
 
-#  use Data::Dumper; warn Dumper($params, $join);
-  $c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search($params, { join => $join, scalar(@columns) ? ( select => \@columns ) : ()  });
+  my ($params, $join);
+  ($params, $join) = $self->_format_search({ params => $req_params->{search}, source => $source }) if ($req_params->{search});
 
-  $c->forward('format_list');
+  my $args = {};
+  $args->{group_by} = $req_params->{list_grouped_by} || ((scalar(@{$self->list_grouped_by})) ? $self->list_grouped_by : undef);
+  $args->{order_by} = $req_params->{list_ordered_by} || ((scalar(@{$self->list_ordered_by})) ? $self->list_ordered_by : undef);
+  $args->{select} = $req_params->{list_returns} || ((scalar(@{$self->list_returns})) ? $self->list_returns : undef);
+  $args->{join} = $join;
+
+  return [$params, $args];
 }
 
 sub _format_search {
@@ -55,7 +73,7 @@
 	foreach my $rel ($source->relationships) {
 		if (exists $params->{$rel}) {
 			my $rel_params;
-			($rel_params, $join->{$rel}) = $self->_format_search({ params => $params->{$rel}, source => $source->related_source($rel), base => $rel });					   
+			($rel_params, $join->{$rel}) = $self->_format_search({ params => $params->{$rel}, source => $source->related_source($rel), base => $rel });
 			%search_params = ( %search_params, %{$rel_params} );
 		}
 	}

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm	2008-07-13 16:11:22 UTC (rev 8111)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm	2008-07-13 18:56:56 UTC (rev 8112)
@@ -68,8 +68,16 @@
 
 =head2 list_returns
 
-Arrayref listing columns that L</list> will return. Leave blank to return all columns.
+Arguments to pass to L<DBIx::Class::ResultSet/select> when performing search for L</list>.
 
+=head2 list_grouped_by
+
+Arguments to pass to L<DBIx::Class::ResultSet/group_by> when performing search for L</list>.
+
+=head2 list_ordered_by
+
+Arguments to pass to L<DBIx::Class::ResultSet/order_by> when performing search for L</list>.
+
 =head2 object_stash_key
 
 Object level methods such as delete and update stash the object in the stash. Specify the stash key you would like to use here. Defaults to 'object'.
@@ -116,7 +124,7 @@
 
 =head2 list
 
-List level action chained from L</setup>. By default populates $c->stash->{response}->{list} with a list of hashrefs representing each object in the class resultset. If the </list_returns> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned.
+List level action chained from L</setup>. By default populates $c->stash->{response}->{list} with a list of hashrefs representing each object in the class resultset. If the L</list_returns> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. Similarly L</list_grouped_by> and L</list_ordered_by> affect the ordering and grouping. Note that if list_returns, list_ordered_by or list_grouped_by request parameters are present then these will override the values set on the class.
 
 If not all objects in the resultset are required then it's possible to pass conditions to the method as request parameters. L</CGI::Expand> is used to expand the request parameters into a structure and then $c->req->params->{search} is used as the search condition.
 

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm	2008-07-13 16:11:22 UTC (rev 8111)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm	2008-07-13 18:56:56 UTC (rev 8112)
@@ -10,7 +10,10 @@
       class => 'RestTestDB::Track',
       create_requires => ['cd', 'title' ],
       create_allows => ['cd', 'title', 'position' ],
-      update_allows => ['title', 'position', { cd => ['*'] }]
+      update_allows => ['title', 'position', { cd => ['*'] }],
+      list_grouped_by => ['position'],
+      list_returns => ['position'],
+      list_ordered_by => ['position']
       );
 
 1;

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t	2008-07-13 16:11:22 UTC (rev 8111)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t	2008-07-13 18:56:56 UTC (rev 8112)
@@ -10,7 +10,7 @@
 use RestTest;
 use DBICTest;
 use URI;
-use Test::More tests => 13;
+use Test::More tests => 17;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
 use JSON::Syck;
@@ -20,6 +20,7 @@
 
 my $artist_list_url = "$base/api/rpc/artist/list";
 my $producer_list_url = "$base/api/rpc/producer/list";
+my $track_list_url = "$base/api/rpc/track/list";
 
 # test open request
 {
@@ -95,3 +96,29 @@
 #  use Data::Dumper; warn Dumper($response, \@expected_response);
   is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with list_returns specified' );
 }
+
+{
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'order_by' => 'position' });	
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position' })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+#  use Data::Dumper; warn Dumper($response, \@expected_response);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with everything specified in class' );
+}
+
+{
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_returns' => 'cd', 'list_grouped_by' => 'cd' });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'cd', order_by => 'cd ASC', select => 'cd' })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+#  use Data::Dumper; warn Dumper($response, \@expected_response);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned when everything overridden in query' );
+}




More information about the Catalyst-commits mailing list