[Catalyst-commits] r7933 - in
Catalyst-Controller-DBIC-API/1.000/trunk:
lib/Catalyst/Controller/DBIC/API
t/lib/RestTest/Controller/API/RPC t/lib/RestTest/Schema t/rpc t/var
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Thu Jun 19 14:44:52 BST 2008
Author: lukes
Date: 2008-06-19 14:44:52 +0100 (Thu, 19 Jun 2008)
New Revision: 7933
Modified:
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/RPC/Track.pm
Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Schema/Track.pm
Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t
Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
Log:
rough update related functionality included
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-17 05:21:41 UTC (rev 7932)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2008-06-19 13:44:52 UTC (rev 7933)
@@ -106,6 +106,8 @@
sub update :Private {
my ($self, $c) = @_;
+ my $req_params = $self->expand_hash($c->req->params);
+ $c->req->params($req_params);
return unless ($c->stash->{object});
unless (ref($self->update_allows) eq 'ARRAY') {
@@ -153,40 +155,68 @@
=cut
+# rewrite this
sub validate {
my ($self, $c, $object) = @_;
my $params = $c->req->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});
-
-# use Data::Dumper; warn Dumper(\%requires_map, \%allows_map);
+ my %allows_map = map { (ref $_) ? %{$_} : ($_ => 1) } (keys %requires_map, @{($object->in_storage) ? $self->update_allows : $self->create_allows});
+
+# use Data::Dumper; warn Dumper($params, \%requires_map, \%allows_map);
foreach my $key (keys %allows_map) {
# check value defined if key required
- my $value = $params->{$key};
- if ($requires_map{$key}) {
- unless (defined($value)) {
- # if not defined look for default
- $value = $object->result_source
- ->column_info($key)
+ my $allowed_fields = $allows_map{$key};
+ if (ref $allowed_fields) {
+ my $related_source = $object->result_source->related_source($key);
+ unless ($related_source) {
+ $self->push_error($c, { message => "${key} is not a valid relation" });
+ next;
+ }
+
+ my $related_params = $params->{$key};
+
+ # it's an error for $c->req->params->{$key} to be defined but not be an array
+ unless (ref $related_params) {
+ unless (!defined $related_params) {
+ $self->push_error($c, { message => "Value of ${key} must be a hash" });
+ }
+ next;
+ }
+
+ my %allowed_related_map = map { $_ => 1 } @{$allowed_fields};
+ my $allowed_related_cols = ($allowed_related_map{'*'}) ? [$related_source->columns] : $allowed_fields;
+ foreach my $related_col (@{$allowed_related_cols}) {
+ if (my $related_col_value = $related_params->{$related_col}) {
+ $values{$key}{$related_col} = $related_col_value;
+ }
+ }
+ } else {
+ my $value = $params->{$key};
+ if ($requires_map{$key}) {
+ unless (defined($value)) {
+ # if not defined look for default
+ $value = $object->result_source
+ ->column_info($key)
->{default_value};
- unless (defined $value) {
- $self->push_error($c, { message => "No value supplied for ${key} and no default" });
- }
- }
- }
+ unless (defined $value) {
+ $self->push_error($c, { message => "No value supplied for ${key} and no default" });
+ }
+ }
+ }
+
+ # TODO: do col type checking here
+
+ # check for multiple values
+ if (ref($value)) {
+ $self->push_error($c, { message => "Multiple values for '${key}'" });
+ }
- # TODO: do col type checking here
-
- # check for multiple values
- if (ref($value)) {
- $self->push_error($c, { message => "Multiple values for '${key}'" });
+ # check exists so we don't just end up with hash of undefs
+ # check defined to accound for default values being used
+ $values{$key} = $value if exists $params->{$key} || defined $value;
}
-
- # check exists so we don't just end up with hash of undefs
- # check defined to accound for default values being used
- $values{$key} = $value if exists $params->{$key} || defined $value;
}
# use Data::Dumper; $c->log->debug(Dumper(\%values));
@@ -208,10 +238,17 @@
sub save_object {
my ($self, $c, $object, $params) = @_;
- $object->set_columns($params);
if ($object->in_storage) {
- $object->update;
+ foreach my $key (keys %{$params}) {
+ if (ref $params->{$key}) {
+ my $related_params = delete $params->{$key};
+ my $row = $object->find_related($key, {} , {});
+ $row->update($related_params);
+ }
+ }
+ $object->update($params);
} else {
+ $object->set_columns($params);
$object->insert;
}
}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm 2008-06-17 05:21:41 UTC (rev 7932)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Controller/API/RPC/Track.pm 2008-06-19 13:44:52 UTC (rev 7933)
@@ -10,7 +10,7 @@
class => 'RestTestDB::Track',
create_requires => ['cd', 'title' ],
create_allows => ['cd', 'title', 'position' ],
- update_allows => ['title', 'position']
+ update_allows => ['title', 'position', { cd => ['*'] }]
);
1;
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Schema/Track.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Schema/Track.pm 2008-06-17 05:21:41 UTC (rev 7932)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/lib/RestTest/Schema/Track.pm 2008-06-19 13:44:52 UTC (rev 7933)
@@ -2,8 +2,6 @@
RestTest::Schema::Track;
use base 'DBIx::Class::Core';
-__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
-
__PACKAGE__->table('track');
__PACKAGE__->add_columns(
'trackid' => {
@@ -33,6 +31,6 @@
__PACKAGE__->add_unique_constraint([ qw/cd position/ ]);
__PACKAGE__->add_unique_constraint([ qw/cd title/ ]);
-__PACKAGE__->belongs_to( cd => 'RestTest::Schema::CD' );
+__PACKAGE__->belongs_to( cd => 'RestTest::Schema::CD');
1;
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t 2008-06-17 05:21:41 UTC (rev 7932)
+++ Catalyst-Controller-DBIC-API/1.000/trunk/t/rpc/update.t 2008-06-19 13:44:52 UTC (rev 7933)
@@ -10,7 +10,7 @@
use RestTest;
use DBICTest;
-use Test::More tests => 18;
+use Test::More tests => 21;
use Test::WWW::Mechanize::Catalyst 'RestTest';
use HTTP::Request::Common;
use JSON::Syck;
@@ -25,9 +25,9 @@
# test invalid track id caught
{
- foreach my $wrong_id ('sdsdsdsd', 3434234) {
- my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
- my $req = POST( $incorrect_url, {
+ foreach my $wrong_id ('sdsdsdsd', 3434234) {
+ my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
+ my $req = POST( $incorrect_url, {
title => 'value'
});
@@ -93,3 +93,16 @@
$track->discard_changes;
is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
}
+
+{
+ my $req = POST( $track_update_url, {
+ title => 'sheep sheep',
+ 'cd.year' => '2009'
+ });
+ $mech->request($req, $content_type);
+ cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
+
+ $track->discard_changes;
+ is($track->title, 'sheep sheep', 'Title changed');
+ is($track->cd->year, '2009', 'Related field changed"');
+}
Modified: Catalyst-Controller-DBIC-API/1.000/trunk/t/var/DBIxClass.db
===================================================================
(Binary files differ)
More information about the Catalyst-commits
mailing list