[Catalyst-commits] r9479 - in Catalyst-Controller-DBIC-API/1.001/trunk: . lib/Catalyst/Controller/DBIC lib/Catalyst/Controller/DBIC/API t/rpc

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Mon Mar 9 22:22:24 GMT 2009


Author: lukes
Date: 2009-03-09 22:22:24 +0000 (Mon, 09 Mar 2009)
New Revision: 9479

Modified:
   Catalyst-Controller-DBIC-API/1.001/trunk/
   Catalyst-Controller-DBIC-API/1.001/trunk/Changes
   Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
   Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t
Log:
 r60 at luke-mbp (orig r9293):  lukes | 2009-02-13 16:28:08 +0000
 new branches
 r65 at luke-mbp (orig r9298):  abraxxa | 2009-02-13 16:51:25 +0000
 Added pagination support + docs
 
 r68 at luke-mbp (orig r9478):  lukes | 2009-03-09 22:21:17 +0000
 pagination adapted for consistency and working with test



Property changes on: Catalyst-Controller-DBIC-API/1.001/trunk
___________________________________________________________________
Name: svk:merge
   - 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Controller-DBIC-API/1.000/trunk:8157
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Controller-DBIC-API/1.001/branches/json_search:9294
   + 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Controller-DBIC-API/1.000/trunk:8157
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Controller-DBIC-API/1.001/branches/json_search:9294
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Controller-DBIC-API/1.001/branches/pagination:9478

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/Changes
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/Changes	2009-03-09 22:21:17 UTC (rev 9478)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/Changes	2009-03-09 22:22:24 UTC (rev 9479)
@@ -4,7 +4,8 @@
 - Removed duplicate tests in t/rpc/list.t and t/rest/list.t
 - Fixed searches on columns which have a rel with the same name
   and vice versa + tests
-- Added search by json + test
+- Added search by json + docs + test
+- Added pagination support + docs
 1.001000
 - Added setup_list_method configuration flag (jshirley)
 - Added support for setting config params in stash

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-09 22:21:17 UTC (rev 9478)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2009-03-09 22:22:24 UTC (rev 9479)
@@ -69,6 +69,10 @@
   $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->{rows} = $req_params->{list_count} || $self->list_count;
+  $args->{page} = $req_params->{list_page};
+  if ($args->{page} && !$args->{rows}) {
+    $self->push_error($c, { message => "list_page can only be used with list_count" });
+  }
   $args->{select} = $req_params->{list_returns} || ((scalar(@{$self->list_returns})) ? $self->list_returns : undef);
   $args->{join} = $join;
 
@@ -76,12 +80,12 @@
 }
 
 sub _format_search {
-	my ($self, $p) = @_;
-	my $params = $p->{params};
-	my $source = $p->{source};
-	my $base = $p->{base} || 'me';
+  my ($self, $p) = @_;
+  my $params = $p->{params};
+  my $source = $p->{source};
+  my $base = $p->{base} || 'me';
 
-	my $join = {};
+  my $join = {};
   my %relname_map = map { $_ => 1 } $source->relationships;
   my %search_params;
 
@@ -272,11 +276,11 @@
 	# check for errors
 	my $default_status;
 
-    # Check for errors caught elsewhere
-    if ( $c->res->status and $c->res->status != 200 ) {
+  # Check for errors caught elsewhere
+	if ( $c->res->status and $c->res->status != 200 ) {
 		$default_status = $c->res->status;
 		$c->stash->{response}->{success} = 'false';
-    }
+  }
 	elsif ($self->get_errors($c)) {
 		$c->stash->{response}->{messages} = $self->get_errors($c);
 		$c->stash->{response}->{success} = 'false';
@@ -286,6 +290,7 @@
 		$default_status = 200;
 	}
 	
+	delete $c->stash->{response}->{list} unless ($default_status == 200);
 	$c->res->status( $default_status || 200 );
 	$c->forward('serialize');
 }

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm	2009-03-09 22:21:17 UTC (rev 9478)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/lib/Catalyst/Controller/DBIC/API.pm	2009-03-09 22:22:24 UTC (rev 9479)
@@ -101,6 +101,10 @@
 
 Arguments to pass to L<DBIx::Class::ResultSet/rows> when performing search for L</list>.
 
+=head2 list_page
+
+Arguments to pass to L<DBIx::Class::ResultSet/rows> 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'.
@@ -164,7 +168,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 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_count>, L</list_grouped_by> and L</list_ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if list_returns, list_count, list_ordered_by or list_grouped_by request parameters are present then these will override the values set on the class.
+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_count>, L</list_page>, L</list_grouped_by> and L</list_ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if list_returns, list_count, 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. You can use a JSON string as the 'search' parameter for maximum flexibility or use L</CGI::Expand> syntax. In the second case the request parameters are expanded into a structure and then $c->req->params->{search} is used as the search condition.
 
@@ -178,6 +182,14 @@
 
  $rs->search({ name => 'fred', 'cd.artist' => 'luke' }, { join => ['cd'] })
 
+Note that if pagination is needed, this can be achieved using a combination of the L</list_count> and L</list_page> parameters. For example:
+
+  ?list_page=2&list_count=20
+
+Would result in this search:
+ 
+ $rs->search({}, { page => 2, rows => 20 })
+
 The L</format_list> method is used to format the results, so override that as required.
 
 =head2 format_list

Modified: Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t	2009-03-09 22:21:17 UTC (rev 9478)
+++ Catalyst-Controller-DBIC-API/1.001/trunk/t/rpc/list.t	2009-03-09 22:22:24 UTC (rev 9479)
@@ -126,31 +126,55 @@
 
 {
   my $uri = URI->new( $track_list_url );
-  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 1 });
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2 });
   my $req = GET( $uri, 'Accept' => 'text/x-json' );
   $mech->request($req);
-  cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+  cmp_ok( $mech->status, '==', 200, 'list count request okay' );
 
-  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 1 })->all;
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2 })->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' );
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
 }
 
 {
-    my $uri = URI->new( $cd_list_url );
-    $uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' });
-    my $req = GET( $uri, 'Accept' => 'text/x-json' );
-    $mech->request($req);
-    if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) {
-        my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->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 search on rel with same name column' );
-    }
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_count' => 2, 'list_page' => 2 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'list count with page request okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Track')->search({}, { group_by => 'position', order_by => 'position ASC', select => 'position', rows => 2, page => 2 })->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' );
 }
 
 {
+  my $uri = URI->new( $track_list_url );
+  $uri->query_form({ 'list_ordered_by' => 'cd', 'list_page' => 2 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 400, 'list page without count returns error' );
+  my $response = JSON::Syck::Load( $mech->content);
+#  use Data::Dumper; warn Dumper($response);
+  is_deeply( { messages => ['list_page can only be used with list_count'], success => 'false' }, $response, 'correct data returned' );
+}
+
+{
+	my $uri = URI->new( $cd_list_url );
+	$uri->query_form({ 'search.artist.name' => 'Caterwauler McCrae' });
+	my $req = GET( $uri, 'Accept' => 'text/x-json' );
+	$mech->request($req);
+	if (cmp_ok( $mech->status, '==', 200, 'search on rel with same name column request okay' )) {
+		my @expected_response = map { { $_->get_columns } } $schema->resultset('CD')->search({'me.artist' => 1})->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 search on rel with same name column' );
+	}
+}
+
+{
   my $uri = URI->new( $cd_list_url );
   $uri->query_form({ 'search.artist' => 1 });
   my $req = GET( $uri, 'Accept' => 'text/x-json' );




More information about the Catalyst-commits mailing list