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

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Sat Jun 14 01:46:46 BST 2008


Author: lukes
Date: 2008-06-14 01:46:46 +0100 (Sat, 14 Jun 2008)
New Revision: 7928

Added:
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
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/REST.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
Log:
added list functionality and an RPC test for it

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-06-13 18:29:46 UTC (rev 7927)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-06-14 00:46:46 UTC (rev 7928)
@@ -2,10 +2,10 @@
 
 use strict;
 use warnings;
-use base 'Catalyst::Controller';
+use base qw/Catalyst::Controller CGI::Expand/;
 
 __PACKAGE__->mk_accessors(qw(
-  class create_requires update_requires update_allows class_rs create_allows
+  class create_requires update_requires update_allows class_rs create_allows list_returns
 ));
 
 __PACKAGE__->config(
@@ -14,6 +14,7 @@
   create_allows => [],
   update_requires => [],
   update_allows => [],
+  list_returns => [],
 );
 
 
@@ -135,13 +136,6 @@
 	my ($self, $c, $object) = @_;
 	my $params = $c->req->params;
 
-	# operation specific hooks. pointless?
-# 	if ($object->in_storage) {
-# 		$self->validate_update($object, $params);
-# 	} else {
-# 		$self->validate_create($object, $params);
-# 	}
-
 	my %values;
 	my %requires_map = map { $_ => 1 } @{($object->in_storage) ? [] : $self->create_requires};
 	my %allows_map = map { $_ => 1 } (keys %requires_map, @{($object->in_storage) ? $self->update_allows : $self->create_allows});
@@ -201,6 +195,45 @@
 	}
 }
 
+sub split_name {
+    my ( $class, $name ) = @_;
+
+    if ( $name =~ /^ \w+ \[/x ) {
+        return grep { defined } ( $name =~ /
+            ^  (\w+)      # root param
+          | \[ (\w+) \] # nested
+                                  /gx );
+    } else {
+        return $class->SUPER::split_name( $name );
+    }
+}
+
+=head2 list
+
+=cut
+
+sub list {
+	my ($self, $c) = @_;
+
+  my $req_params = $self->expand_hash($c->req->params);
+
+  my $source = $c->stash->{class_rs}->result_source;
+  my @columns = (scalar(@{$self->list_returns})) ? @{$self->list_returns} : $source->columns;
+
+  my %search_params;
+  if ($req_params->{search}) {
+      %search_params = map { $_ => $req_params->{search}->{$_} } grep { exists $req_params->{search}->{$_} } $source->columns;
+  }
+
+  $c->stash->{class_rs} = $c->stash->{class_rs}->search(\%search_params, { select => \@columns });
+
+  my @ret = map { { $_->get_columns } } $c->stash->{class_rs}->all;
+  $c->stash->{response}->{list} = \@ret;
+}
+
+
+
+
 # =head2 add_to_rel
 
 # 	finds a related row and then creates the many_to_many linking row using ->add_to_$rel
@@ -241,18 +274,18 @@
 # 		$c->detach( 'error', [{ message => "Invalid relationship $rel" }]);		
 # 	}
 # 	my $source = $related_rs->result_source;
-	
+
 # 	my %related_args = map { $_ => $c->req->params->{$_} } grep { $c->req->params->{$_} } $source->columns;
 
 # 	unless (keys %related_args) {
 # 		$c->detach( 'error', [{ message => "No valid keys passed" }]);
 # 	}
-																 
+
 # 	my $related_row = $related_rs->result_source->resultset->find(\%related_args);
 # 	unless ($related_row) {
 # 		$c->detach( 'error', [{ message => "Invalid related row" }]);
 # 	}
-	
+
 # 	$c->stash->{object}->$accessor($related_row);
 # }
 
@@ -263,9 +296,9 @@
 }
 
 sub get_errors {
-	my ( $self, $c, $params ) = @_;
+  my ( $self, $c, $params ) = @_;
 
-	return $c->stash->{_dbic_crud_errors};
+  return $c->stash->{_dbic_crud_errors};
 }
 
 

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm	2008-06-13 18:29:46 UTC (rev 7927)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm	2008-06-14 00:46:46 UTC (rev 7928)
@@ -76,14 +76,12 @@
 sub object_POST {
 	my ($self, $c) = @_;
 
-	warn 'dsdsfdsf';
 	$c->forward('update');
 }
 
 sub object_PUT {
 	my ($self, $c) = @_;
 
-	warn 'dsdsfdsf';
 	$c->forward('update');
 }
 
@@ -111,6 +109,12 @@
 	$c->forward('create');
 }
 
+sub base_GET {
+	my ( $self, $c ) = @_;
+
+	$c->forward('list');
+}
+
 =head1 AUTHOR
 
 luke saunders

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm	2008-06-13 18:29:46 UTC (rev 7927)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/RPC.pm	2008-06-14 00:46:46 UTC (rev 7928)
@@ -10,6 +10,7 @@
 						'stash_key' => 'response',
 						'map'       => {
 							'application/x-www-form-urlencoded'        => 'JSON',
+							'text/x-json'        => 'JSON',
 						});
 =head1 NAME
 
@@ -79,12 +80,24 @@
 
 =cut
 
-sub create :Chained('setup') :PathPart('create') :Args(0) :Auth('read_write') {
+sub create :Chained('setup') :PathPart('create') :Args(0) {
 	my ($self, $c) = @_;
 
 	$self->NEXT::create($c);
 }
 
+=head2 list
+
+Returns a list of objects from the class resultset. The columns to be returned can be specified
+
+=cut
+
+sub list :Chained('setup') :PathPart('list') :Args(0) {
+	my ($self, $c) = @_;
+
+	$self->NEXT::list($c);
+}
+
 =head2 update
 
 Matches cols specified in update_allows to $c->req->params and $c->stash->{params} 
@@ -92,7 +105,7 @@
 
 =cut
 
-sub update :Chained('object') :PathPart('update') :Args(0) :Auth('read_write') {
+sub update :Chained('object') :PathPart('update') :Args(0) {
 	my ($self, $c) = @_;
 
 	$self->NEXT::update($c);
@@ -104,7 +117,7 @@
 
 =cut
 
-sub delete :Chained('object') :PathPart('delete') :Args(0) :Auth('read_write') {
+sub delete :Chained('object') :PathPart('delete') :Args(0) {
 	my ($self, $c) = @_;
 
 	$self->NEXT::delete($c);

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm	2008-06-13 18:29:46 UTC (rev 7927)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm	2008-06-14 00:46:46 UTC (rev 7928)
@@ -9,7 +9,8 @@
     ( action => { setup => { PathPart => 'producer', Chained => '/api/rpc/rpc_base' } },
       class => 'RestTestDB::Producer',
       create_requires => ['name'],
-      update_allows => ['name']
+      update_allows => ['name'],
+      list_returns => ['name']
       );
 
 1;

Added: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t	                        (rev 0)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t	2008-06-14 00:46:46 UTC (rev 7928)
@@ -0,0 +1,82 @@
+use 5.6.0;
+
+use strict;
+use warnings;
+
+use lib 't/lib';
+
+my $base = 'http://localhost';
+
+use RestTest;
+use DBICTest;
+use URI;
+use Test::More tests => 11;
+use Test::WWW::Mechanize::Catalyst 'RestTest';
+use HTTP::Request::Common;
+use JSON::Syck;
+
+my $mech = Test::WWW::Mechanize::Catalyst->new;
+ok(my $schema = DBICTest->init_schema(), 'got schema');
+
+my $artist_list_url = "$base/api/rpc/artist/list";
+my $producer_list_url = "$base/api/rpc/producer/list";
+
+# test open request
+{
+  my $req = GET( $artist_list_url, {
+
+  }, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct message returned' );
+}
+
+{
+  my $uri = URI->new( $artist_list_url );
+  $uri->query_form({ 'search.artistid' => 1 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ artistid => 1 })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
+}
+
+{
+  my $uri = URI->new( $artist_list_url );
+  $uri->query_form({ 'search.artistid' => 1 });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ artistid => 1 })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned' );
+}
+
+{
+  my $uri = URI->new( $artist_list_url );
+  $uri->query_form({ 'search.name.LIKE' => '%waul%' });
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }})->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for complex query' );
+}
+
+{
+  my $uri = URI->new( $producer_list_url );
+  my $req = GET( $uri, 'Accept' => 'text/x-json' );
+  $mech->request($req);
+  cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
+
+  my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->all;
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with list_returns specified' );
+}

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)




More information about the Catalyst-commits mailing list