X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FDBIC%2FAPI.pm;h=229237d28ec7b7a470c5f8cd2be1c31ea8a0e854;hp=ea086b7862acf9ee20cac1e66c3d5d13088a1b08;hb=2abdb8310c404d431fd1f229cc231237b5b93bb8;hpb=406086f3da2f020cf98b01d994ffe2d1b8a478c4 diff --git a/lib/Catalyst/Controller/DBIC/API.pm b/lib/Catalyst/Controller/DBIC/API.pm index ea086b7..229237d 100644 --- a/lib/Catalyst/Controller/DBIC/API.pm +++ b/lib/Catalyst/Controller/DBIC/API.pm @@ -2,7 +2,7 @@ package Catalyst::Controller::DBIC::API; #ABSTRACT: Provides a DBIx::Class web service automagically use Moose; -BEGIN { extends 'Catalyst::Controller'; } +BEGIN { extends 'Catalyst::Controller::ActionRole'; } use CGI::Expand (); use DBIx::Class::ResultClass::HashRefInflator; @@ -374,6 +374,7 @@ sub list =method_protected list_munge_parameters list_munge_parameters is a noop by default. All arguments will be passed through without any manipulation. In order to successfully manipulate the parameters before the search is performed, simply access $c->req->search_parameters|search_attributes (ArrayRef and HashRef respectively), which correspond directly to ->search($parameters, $attributes). Parameter keys will be in already-aliased form. +To store the munged parameters call $c->req->_set_search_parameters($newparams) and $c->req->_set_search_attributes($newattrs). =cut @@ -556,7 +557,7 @@ sub validate_objects { my $err = $_; $c->log->error($err); - $err =~ s/\s+at\s+\/.+\n$//g; + $err =~ s/\s+at\s+.+\n$//g; $self->push_error($c, { message => $err }); $c->detach(); } @@ -611,7 +612,7 @@ sub validate_object foreach my $related_col (@{$allowed_related_cols}) { - if (my $related_col_value = $related_params->{$related_col}) { + if (defined(my $related_col_value = $related_params->{$related_col})) { $values{$key}{$related_col} = $related_col_value; } } @@ -748,7 +749,13 @@ sub update_object_relation { my ($self, $c, $object, $related_params, $relation) = @_; my $row = $object->find_related($relation, {} , {}); - $row->update($related_params); + + if ($row) { + $row->update($related_params); + } + else { + $object->create_related($relation, $related_params); + } } =method_protected insert_object_from_params @@ -760,8 +767,22 @@ insert_object_from_params sets the columns for the object, then calls ->insert sub insert_object_from_params { my ($self, $c, $object, $params) = @_; - $object->set_columns($params); + + my %rels; + while (my ($k, $v) = each %{ $params }) { + if (ref $v && !($v == JSON::Any::true || $v == JSON::Any::false)) { + $rels{$k} = $v; + } + else { + $object->set_column($k => $v); + } + } + $object->insert; + + while (my ($k, $v) = each %rels) { + $object->create_related($k, $v); + } } =method_protected delete_objects