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

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Sun Jun 29 13:17:18 BST 2008


Author: lukes
Date: 2008-06-29 13:17:18 +0100 (Sun, 29 Jun 2008)
New Revision: 8048

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/REST/Producer.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/create.t
   Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t
Log:
improved docs on extending and made it possible for create to return the created object

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-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm	2008-06-29 12:17:18 UTC (rev 8048)
@@ -80,7 +80,7 @@
 	}
 
 	my $empty_object = $c->stash->{$self->rs_stash_key}->new_result({});
-	$self->validate_and_save_object($c, $empty_object);
+	$c->stash->{created_object} = $self->validate_and_save_object($c, $empty_object);
 }
 
 sub update :Private {
@@ -119,7 +119,7 @@
 		return;
 	}
 
-	$self->save_object($c, $object, $params);
+	return $self->save_object($c, $object, $params);
 }
 
 sub validate {
@@ -209,6 +209,7 @@
 		$object->set_columns($params);
 		$object->insert;
 	}
+  return $object;
 }
 
 sub end :Private {

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-06-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API.pm	2008-06-29 12:17:18 UTC (rev 8048)
@@ -97,7 +97,9 @@
   # or
 
   sub setup :Chained('/api/rpc_base') :CaptureArgs(0) :PathPart('track') {
-	$self->NEXT::setup($c);
+    my ($self, $c) = @_;
+
+    $self->NEXT::setup($c);
   }
 
 This action will populate $c->stash->{$self->rs_stash_key} with $c->model($self->class) for other actions in the chain to use.
@@ -152,8 +154,34 @@
 
 =head1 EXTENDING
 
-By default the create, delete and update actions will not return anything apart from the success parameter set in L</end>, often this is not ideal but the required behaviour varies from application to application. So normally it's sensible to write an intermediate class which your main controller classes subclass from.
+By default the create, delete and update actions will not return anything apart from the success parameter set in L</end>, often this is not ideal but the required behaviour varies from application to application. So normally it's sensible to write an intermediate class which your main controller classes subclass from. For example if you wanted create to return the JSON for the newly created object you might have something like:
 
+  package MyApp::ControllerBase::DBIC::API::RPC;
+  ...
+  use base qw/Catalyst::Controller::DBIC::API::RPC/;
+  ...
+  sub create :Chained('setup') :Args(0) :PathPart('create') {
+    my ($self, $c) = @_;
+
+    # will set $c->stash->{created_object} if successful
+    $self->NEXT::create($c);
+
+    if ($c->stash->{created_object}) {    
+      # $c->stash->{response} will be serialized in the end action
+      %{$c->stash->{response}->{new_object}} = $c->stash->{created_object}->get_columns;
+    }
+  }
+
+
+  package MyApp::Controller::API::RPC::Track;
+  ...
+  use base qw/MyApp::ControllerBase::DBIC::API::RPC/;
+  ...
+
+If you were using the RPC style. For REST the only difference besides the class names would be that create should be :Private rather than an endpoint.
+
+Similarly you might want create, update and delete to all forward to the list action once they are done so you can refresh your view. This should also be simple enough.
+
 =head1 AUTHOR
 
   Luke Saunders <luke.saunders at gmail.com>

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm	2008-06-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/REST/Producer.pm	2008-06-29 12:17:18 UTC (rev 8048)
@@ -13,4 +13,13 @@
       list_returns => ['name']
       );
 
+sub create :Private {
+  my ($self, $c) = @_;
+  $self->NEXT::create($c);
+
+  if ($c->stash->{created_object}) {
+    %{$c->stash->{response}->{new_producer}} = $c->stash->{created_object}->get_columns;
+  }
+}
+
 1;

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-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Producer.pm	2008-06-29 12:17:18 UTC (rev 8048)
@@ -13,4 +13,13 @@
       list_returns => ['name']
       );
 
+sub create :Chained('setup') :Args(0) :PathPart('create') {
+  my ($self, $c) = @_;
+  $self->NEXT::create($c);
+
+  if ($c->stash->{created_object}) {
+    %{$c->stash->{response}->{new_producer}} = $c->stash->{created_object}->get_columns;
+  }
+}
+
 1;

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/create.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/create.t	2008-06-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/create.t	2008-06-29 12:17:18 UTC (rev 8048)
@@ -9,7 +9,7 @@
 
 use RestTest;
 use DBICTest;
-use Test::More tests => 7;
+use Test::More tests => 8;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
 use JSON::Syck;
@@ -63,5 +63,9 @@
 	$mech->request($req);
 
 	cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
-	ok($schema->resultset('Producer')->find({ name => 'king luke' }), 'record created with specified name');
+        my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
+	ok($new_obj, 'record created with specified name');
+
+	my $response = JSON::Syck::Load( $mech->content);
+	is_deeply( $response->{new_producer}, { $new_obj->get_columns }, 'json for new producer returned' );
 }

Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t	2008-06-29 11:43:47 UTC (rev 8047)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/create.t	2008-06-29 12:17:18 UTC (rev 8048)
@@ -10,7 +10,7 @@
 
 use RestTest;
 use DBICTest;
-use Test::More tests => 7;
+use Test::More tests => 8;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
 use JSON::Syck;
@@ -51,5 +51,9 @@
   $mech->request($req, $content_type);
   cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
 
-  ok($schema->resultset('Producer')->find({ name => 'king luke' }), 'record created with specified name');
+  my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
+  ok($new_obj, 'record created with specified name');
+
+  my $response = JSON::Syck::Load( $mech->content);
+  is_deeply( $response->{new_producer}, { $new_obj->get_columns }, 'json for new producer returned' );
 }




More information about the Catalyst-commits mailing list