Skip to content

Commit

Permalink
Merge pull request #719 from metacpan/mickey/wip_controllers_return_s…
Browse files Browse the repository at this point in the history
…tatus_fixup

Added 400/404s for Release endpoints
  • Loading branch information
oalders committed Jul 13, 2017
2 parents 6d73d3e + 5d8258b commit 6a4be47
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 70 deletions.
7 changes: 5 additions & 2 deletions lib/MetaCPAN/Server/Controller/Activity.pm
Expand Up @@ -12,8 +12,11 @@ with 'MetaCPAN::Server::Role::JSONP';
sub get : Path('') : Args(0) {
my ( $self, $c ) = @_;
my $data = $c->model('CPAN::Release')->raw->activity( $c->req->params );
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

__PACKAGE__->meta->make_immutable;
Expand Down
33 changes: 23 additions & 10 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -66,8 +66,11 @@ sub qsearch : Path('search') : Args(0) {
my ( $self, $c ) = @_;
my ( $query, $from ) = @{ $c->req->params }{qw( q from )};
my $data = $self->model($c)->raw->search( $query, $from );
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

# /author/by_ids?id=PAUSE_ID1&id=PAUSE_ID2...
Expand All @@ -78,28 +81,38 @@ sub by_ids : Path('by_ids') : Args(0) {
= $body_data
? $body_data->{id}
: [ $c->req->param('id') ];
return unless $ids and @{$ids};
$c->detach( '/bad_request', ['No ids requested'] )
unless $ids and @{$ids};
my $data = $self->model($c)->raw->by_ids($ids);
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

# /author/by_user/USER_ID
sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;
my $data = $self->model($c)->raw->by_user($user);
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

# /author/by_user?user=USER_ID1&user=USER_ID2...
sub by_users : Path('by_user') : Args(0) {
my ( $self, $c ) = @_;
my @users = $c->req->param('user');
return unless @users;
$c->detach( '/bad_request', ['No users requested'] ) unless @users;
my $data = $self->model($c)->raw->by_user( \@users );
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;
14 changes: 10 additions & 4 deletions lib/MetaCPAN/Server/Controller/Contributor.pm
Expand Up @@ -14,15 +14,21 @@ sub get : Path('') : Args(2) {
my ( $self, $c, $author, $name ) = @_;
my $data
= $self->model($c)->raw->find_release_contributors( $author, $name );
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub by_pauseid : Path('by_pauseid') : Args(1) {
my ( $self, $c, $pauseid ) = @_;
my $data = $self->model($c)->raw->find_author_contributions($pauseid);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;
38 changes: 27 additions & 11 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -32,31 +32,43 @@ sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;
my $size = $c->req->param('size') || 250;
my $data = $self->model($c)->raw->by_user( $user, $size );
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub users_by_distribution : Path('users_by_distribution') : Args(1) {
my ( $self, $c, $distribution ) = @_;
my $data = $self->model($c)->raw->users_by_distribution($distribution);
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub recent : Path('recent') : Args(0) {
my ( $self, $c ) = @_;
my $page = $c->req->param('page') || 1;
my $size = $c->req->param('size') || 100;
my $data = $self->model($c)->raw->recent( $page, $size );
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub leaderboard : Path('leaderboard') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->leaderboard();
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {
Expand All @@ -67,7 +79,8 @@ sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {
= $body_data
? $body_data->{distribution}
: [ $c->req->param('distribution') ];
return unless $distributions and @{$distributions};
$c->detach( '/bad_request', ['No distributions requested'] )
unless $distributions and @{$distributions};

my $user
= $body_data
Expand All @@ -76,8 +89,11 @@ sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {

my $data = $self->model($c)
->raw->agg_by_distributions( $distributions, $user );
$data or return;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

__PACKAGE__->meta->make_immutable;
Expand Down
7 changes: 5 additions & 2 deletions lib/MetaCPAN/Server/Controller/File.pm
Expand Up @@ -52,8 +52,11 @@ sub find : Path('') {
sub dir : Path('dir') {
my ( $self, $c, @path ) = @_;
my $data = $self->model($c)->dir(@path);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;
7 changes: 5 additions & 2 deletions lib/MetaCPAN/Server/Controller/Mirror.pm
Expand Up @@ -12,8 +12,11 @@ with 'MetaCPAN::Server::Role::JSONP';
sub search : Path('search') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->search( $c->req->param('q') );
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;
10 changes: 8 additions & 2 deletions lib/MetaCPAN/Server/Controller/Package.pm
Expand Up @@ -11,10 +11,16 @@ with 'MetaCPAN::Server::Role::JSONP';
sub modules : Path('modules') : Args(1) {
my ( $self, $c, $dist ) = @_;
my $last = $c->model('CPAN::Release')->raw->find($dist);
return unless $last;
$c->detach( '/not_found', ["Cannot find last release for $dist"] )
unless $last;

my $data
= $self->model($c)->get_modules( $dist, $last->{version} );
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

__PACKAGE__->meta->make_immutable;
Expand Down
25 changes: 18 additions & 7 deletions lib/MetaCPAN/Server/Controller/Permission.pm
Expand Up @@ -10,15 +10,21 @@ with 'MetaCPAN::Server::Role::JSONP';
sub by_author : Path('by_author') : Args(1) {
my ( $self, $c, $pauseid ) = @_;
my $data = $self->model($c)->raw->by_author($pauseid);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub by_module : Path('by_module') : Args(1) {
my ( $self, $c, $module ) = @_;
my $data = $self->model($c)->raw->by_modules($module);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

sub by_modules : Path('by_module') : Args(0) {
Expand All @@ -27,10 +33,15 @@ sub by_modules : Path('by_module') : Args(0) {
= $c->req->body_data
? $c->req->body_data->{module}
: [ $c->req->param('module') ];
return unless $modules and @{$modules};
$c->detach( '/bad_request', ['No modules requested'] )
unless $modules and @{$modules};

my $data = $self->model($c)->raw->by_modules($modules);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

__PACKAGE__->meta->make_immutable;
Expand Down
11 changes: 8 additions & 3 deletions lib/MetaCPAN/Server/Controller/Rating.pm
Expand Up @@ -15,10 +15,15 @@ sub by_distributions : Path('by_distributions') : Args(0) {
= $c->req->body_data
? $c->req->body_data->{distribution}
: [ $c->req->param('distribution') ];
return unless $distributions and @{$distributions};
$c->detach( '/bad_request', ['No distributions requested'] )
unless $distributions and @{$distributions};

my $data = $self->model($c)->raw->by_distributions($distributions);
return unless $data;
$c->stash($data);

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;

0 comments on commit 6a4be47

Please sign in to comment.