[Catalyst-commits] r8013 - in
Catalyst-Controller-DBIC-API/1.000/trunk:
lib/Catalyst/Controller/DBIC/API t/rest t/rpc
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Fri Jun 27 15:31:33 BST 2008
Author: lukes
Date: 2008-06-27 15:31:32 +0100 (Fri, 27 Jun 2008)
New Revision: 8013
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/t/rest/list.t
Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
Log:
search related functionality added to list
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-27 12:06:54 UTC (rev 8012)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-27 14:31:32 UTC (rev 8013)
@@ -5,18 +5,18 @@
use base qw/Catalyst::Controller CGI::Expand/;
__PACKAGE__->mk_accessors(qw(
- class create_requires update_requires update_allows 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 rs_stash_key object_stash_key
));
__PACKAGE__->config(
class => undef,
- rs_stash_key => 'class_rs',
- object_stash_key => 'object',
create_requires => [],
create_allows => [],
update_requires => [],
update_allows => [],
list_returns => [],
+ object_stash_key => 'object',
+ rs_stash_key => 'class_rs'
);
@@ -66,17 +66,34 @@
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;
- }
+
+ my ($params, $join) = $self->_format_search({ params => $req_params->{search}, source => $source }) if ($req_params->{search});
-# use Data::Dumper; warn Dumper(\%search_params);
- $c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search(\%search_params, { select => \@columns });
+# use Data::Dumper; warn Dumper($params, $join);
+ $c->stash->{$self->rs_stash_key} = $c->stash->{$self->rs_stash_key}->search($params, { join => $join, select => \@columns });
$c->forward('format_list');
}
+sub _format_search {
+ my ($self, $p) = @_;
+ my $params = $p->{params};
+ my $source = $p->{source};
+ my $base = $p->{base};
+ my $join = {};
+ my %search_params = map { ($base) ? join('.', $base, $_) : $_ => $params->{$_} } grep { exists $params->{$_} } $source->columns;
+ 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 });
+ %search_params = ( %search_params, %{$rel_params} );
+ }
+ }
+
+ return (\%search_params, $join);
+}
+
=head2 list
override this to customise the format of list
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-27 12:06:54 UTC (rev 8012)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/REST.pm 2008-06-27 14:31:32 UTC (rev 8013)
@@ -69,8 +69,7 @@
unless ($object) {
$self->push_error($c, { message => "Invalid id" });
}
-
- $c->stash->{$self->object_stash_key} = $object if ($self->object_stash_key);
+ $c->stash->{$self->object_stash_key} = $object;
}
sub object_POST {
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t 2008-06-27 12:06:54 UTC (rev 8012)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rest/list.t 2008-06-27 14:31:32 UTC (rev 8013)
@@ -10,7 +10,7 @@
use RestTest;
use DBICTest;
use URI;
-use Test::More tests => 11;
+use Test::More tests => 13;
use Test::WWW::Mechanize::Catalyst 'RestTest';
use HTTP::Request::Common;
use JSON::Syck;
@@ -80,3 +80,16 @@
my $response = JSON::Syck::Load( $mech->content);
is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for class with list_returns specified' );
}
+
+{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
+ 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('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->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 list_returns specified' );
+}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t 2008-06-27 12:06:54 UTC (rev 8012)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/list.t 2008-06-27 14:31:32 UTC (rev 8013)
@@ -10,7 +10,7 @@
use RestTest;
use DBICTest;
use URI;
-use Test::More tests => 11;
+use Test::More tests => 13;
use Test::WWW::Mechanize::Catalyst 'RestTest';
use HTTP::Request::Common;
use JSON::Syck;
@@ -78,5 +78,20 @@
my @expected_response = map { { $_->get_columns } } $schema->resultset('Producer')->search({}, { select => ['name'] })->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 list_returns specified' );
}
+
+
+{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
+ 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('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { join => 'cds' })->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 list_returns specified' );
+}
More information about the Catalyst-commits
mailing list