X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FDBIC%2FAPI.pm;h=aa75619a9d2cb215396734b9707fec04c9025e00;hb=4e5983f2a904f894762e61c2dff962a37da5ef92;hp=537bec24b31087083e66a4ee5b315c4c89cfb62b;hpb=0b0bf9111127c9fdad1e456169ec4cdd15b160f9;p=catagits%2FCatalyst-Controller-DBIC-API.git diff --git a/lib/Catalyst/Controller/DBIC/API.pm b/lib/Catalyst/Controller/DBIC/API.pm index 537bec2..aa75619 100644 --- a/lib/Catalyst/Controller/DBIC/API.pm +++ b/lib/Catalyst/Controller/DBIC/API.pm @@ -6,7 +6,7 @@ BEGIN { extends 'Catalyst::Controller::ActionRole'; } use CGI::Expand (); use DBIx::Class::ResultClass::HashRefInflator; -use JSON; +use JSON (); use Test::Deep::NoTest('eq_deeply'); use MooseX::Types::Moose(':all'); use Moose::Util; @@ -27,8 +27,9 @@ sub _build__json { } with 'Catalyst::Controller::DBIC::API::StoredResultSource', - 'Catalyst::Controller::DBIC::API::StaticArguments', - 'Catalyst::Controller::DBIC::API::RequestArguments' => { static => 1 }; + 'Catalyst::Controller::DBIC::API::StaticArguments'; + +with 'Catalyst::Controller::DBIC::API::RequestArguments' => { static => 1 }; __PACKAGE__->config(); @@ -41,7 +42,7 @@ __PACKAGE__->config(); __PACKAGE__->config ( action => { setup => { PathPart => 'artist', Chained => '/api/rpc/rpc_base' } }, # define parent chain action and partpath class => 'MyAppDB::Artist', - result_class => 'MyAppDB::ResultSet::Artist', + resultset_class => 'MyAppDB::ResultSet::Artist', create_requires => ['name', 'age'], create_allows => ['nickname'], update_allows => ['name', 'age', 'nickname'], @@ -867,34 +868,32 @@ sub end :Private { my ($self, $c) = @_; - # check for errors - my $default_status; + # don't change the http status code if already set elsewhere + unless ($c->res->status && $c->res->status != 200) { + if ($self->has_errors($c)) { + $c->res->status(400); + } + else { + $c->res->status(200); + } + } - # Check for errors caught elsewhere - if ( $c->res->status and $c->res->status != 200 ) { - $default_status = $c->res->status; - $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::false : 'false'; - } elsif ($self->get_errors($c)) { - $c->stash->{$self->stash_key}->{messages} = $self->get_errors($c); - $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::false : 'false'; - $default_status = 400; - } else { + if ($c->res->status == 200) { $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::true : 'true'; - $default_status = 200; + if($self->return_object && $c->req->has_objects) { + my $returned_objects = []; + push(@$returned_objects, $self->each_object_inflate($c, $_)) for map { $_->[0] } $c->req->all_objects; + $c->stash->{$self->stash_key}->{$self->data_root} = scalar(@$returned_objects) > 1 ? $returned_objects : $returned_objects->[0]; + } } - - unless ($default_status == 200) - { + else { + $c->stash->{$self->stash_key}->{success} = $self->use_json_boolean ? JSON::false : 'false'; + $c->stash->{$self->stash_key}->{messages} = $self->get_errors($c) + if $self->has_errors($c); + # don't return data for error responses delete $c->stash->{$self->stash_key}->{$self->data_root}; } - elsif($self->return_object && $c->req->has_objects) - { - my $returned_objects = []; - push(@$returned_objects, $self->each_object_inflate($c, $_)) for map { $_->[0] } $c->req->all_objects; - $c->stash->{$self->stash_key}->{$self->data_root} = scalar(@$returned_objects) > 1 ? $returned_objects : $returned_objects->[0]; - } - $c->res->status( $default_status || 200 ); $c->forward('serialize'); } @@ -932,6 +931,8 @@ push_error stores an error message into the stash to be later retrieved by L{message}) { $error = $params->{message}; @@ -951,9 +952,24 @@ get_errors returns all of the errors stored in the stash sub get_errors { my ( $self, $c ) = @_; + die 'Catalyst app object missing' + unless defined $c; return $c->stash->{_dbic_crud_errors}; } +=method_protected has_errors + +returns returns true if errors are stored in the stash + +=cut + +sub has_errors { + my ( $self, $c ) = @_; + die 'Catalyst app object missing' + unless defined $c; + return exists $c->stash->{_dbic_crud_errors}; +} + =head1 DESCRIPTION Easily provide common API endpoints based on your L schema classes. Module provides both RPC and REST interfaces to base functionality. Uses L and L to serialise response and/or deserialise request.