[Catalyst-commits] r7681 - Catalyst-Controller-REST-DBIC-Item/trunk/0.01/lib/Catalyst/Controller/REST/DBIC

jshirley at dev.catalyst.perl.org jshirley at dev.catalyst.perl.org
Tue May 6 04:49:25 BST 2008


Author: jshirley
Date: 2008-05-06 04:49:24 +0100 (Tue, 06 May 2008)
New Revision: 7681

Modified:
   Catalyst-Controller-REST-DBIC-Item/trunk/0.01/lib/Catalyst/Controller/REST/DBIC/Item.pm
Log:
Fleshing out the get_item to be more useful and work with an RS rather than a ::Row object.  item_base is the better point to override now.

Modified: Catalyst-Controller-REST-DBIC-Item/trunk/0.01/lib/Catalyst/Controller/REST/DBIC/Item.pm
===================================================================
--- Catalyst-Controller-REST-DBIC-Item/trunk/0.01/lib/Catalyst/Controller/REST/DBIC/Item.pm	2008-05-05 20:50:59 UTC (rev 7680)
+++ Catalyst-Controller-REST-DBIC-Item/trunk/0.01/lib/Catalyst/Controller/REST/DBIC/Item.pm	2008-05-06 03:49:24 UTC (rev 7681)
@@ -82,7 +82,7 @@
 sub item_base : Chained('rest_base') PathPart('') CaptureArgs(1) {
     my ( $self, $c, $pk1 ) = @_;
 
-    $c->stash->{rest}->{item} = $self->get_item( $c, $pk1 );
+    $c->stash->{rest}->{item} = $self->get_item( $c, $pk1 )->first;
 }
 
 sub item : Chained('item_base') PathPart('') Args(0) ActionClass('REST') {
@@ -91,21 +91,41 @@
 
 sub item_GET {
     my ( $self, $c ) = @_;
-    unless ( $c->stash->{rest}->{item} ) {
+    my $item;
+    unless ( $item = $c->stash->{rest}->{item} ) {
         return $self->status_not_found( $c, 
             message => $c->localize('ITEM_NOT_FOUND') );
     }
+    
     return $self->status_ok( $c,
-        entity => $c->stash->{rest}->{item}
+        entity => $item->can('serialize') ? $item->serialize : $item
     );
 }
 
+=head1 get_item($c, $identifier)
+
+This method should return a result set that points to a single item that is
+identified by $identifier.
+
+If the configuration key C<item_key> is defined, it will do a search where
+C<item_key == $identifier>, otherwise just a simple pull of the result
+source primary keys, which is most likely what is expected but may not be
+what you want.
+
+=cut
+
 sub get_item {
     my ( $self, $c, $item ) = @_;
+    my $rs = $self->get_rs($c);
+
     if ( my $key = $self->{item_key} ) {
-        return $self->get_rs($c)->find({ $key => $item });
-    } else {            
-        return $self->get_rs($c)->find( $item );
+        return $rs->search({ "me.$key" => $item });
+    } else {
+        my @cols = $rs->primary_columns;
+        if ( @cols == 1 ) {
+            return $self->get_rs($c)->search({ "me.$cols[0]" => $item });
+        }
+        $c->log->error("Can't fetch item if schema has multiple primary keys and item_key is not defined.");
     }
 }
 




More information about the Catalyst-commits mailing list