Skip to content

Commit

Permalink
Added controller method for improving/simplifying data stashing
Browse files Browse the repository at this point in the history
To remove the need to copy-paste the same pattern of reading data
and then checking it before stashing (to avoid errors), the
pattern was made into a single reusable method available for
the controller.
  • Loading branch information
mickeyn committed Jul 25, 2017
1 parent 1a472ad commit e6b65b9
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 245 deletions.
10 changes: 10 additions & 0 deletions lib/MetaCPAN/Server.pm
Expand Up @@ -146,6 +146,16 @@ sub read_param {
return $params;
}

# a controller method to either stash given data or detach
# with a not_found message
sub stash_or_detach {
my ( $c, $data ) = @_;
$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
}

1;

__END__
7 changes: 2 additions & 5 deletions lib/MetaCPAN/Server/Controller/Activity.pm
Expand Up @@ -11,12 +11,9 @@ with 'MetaCPAN::Server::Role::JSONP';

sub get : Path('') : Args(0) {
my ( $self, $c ) = @_;
my $data = $c->model('CPAN::Release')->activity( $c->req->params );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach(
$c->model('CPAN::Release')->activity( $c->req->params ) );
}

__PACKAGE__->meta->make_immutable;
Expand Down
37 changes: 7 additions & 30 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -63,51 +63,28 @@ sub get : Path('') : Args(1) {

# /author/search?q=QUERY
sub qsearch : Path('search') : Args(0) {
my ( $self, $c ) = @_;
my ( $query, $from ) = @{ $c->req->params }{qw( q from )};

my $data = $self->model($c)->search( $query, $from );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
my ( $self, $c ) = @_;
$c->stash_or_detach(
$self->model($c)->search( @{ $c->req->params }{qw( q from )} ) );
}

# /author/by_ids?id=PAUSE_ID1&id=PAUSE_ID2...
sub by_ids : Path('by_ids') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)->by_ids( $c->read_param('id') );

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

# /author/by_user/USER_ID
sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;

my $data = $self->model($c)->by_user($user);

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

# /author/by_user?user=USER_ID1&user=USER_ID2...
sub by_users : Path('by_user') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)->by_user( $c->read_param('user') );

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

1;
18 changes: 4 additions & 14 deletions lib/MetaCPAN/Server/Controller/Contributor.pm
Expand Up @@ -12,24 +12,14 @@ with 'MetaCPAN::Server::Role::JSONP';

sub get : Path('') : Args(2) {
my ( $self, $c, $author, $name ) = @_;

my $data = $self->model($c)->find_release_contributors( $author, $name );

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

sub by_pauseid : Path('by_pauseid') : Args(1) {
my ( $self, $c, $pauseid ) = @_;

my $data = $self->model($c)->find_author_contributions($pauseid);

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

1;
58 changes: 17 additions & 41 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -30,63 +30,39 @@ sub find : Path('') : Args(2) {

sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;
my $size = $c->req->param('size') || 250;

my $data = $self->model($c)->by_user( $user, $size );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach(
$self->model($c)->by_user( $user, $c->req->param('size') || 250 ) );
}

sub users_by_distribution : Path('users_by_distribution') : Args(1) {
my ( $self, $c, $distribution ) = @_;

my $data = $self->model($c)->users_by_distribution($distribution);

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

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)->recent( $page, $size );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach(
$self->model($c)->recent(
$c->req->param('page') || 1,
$c->req->param('size') || 100
)
);
}

sub leaderboard : Path('leaderboard') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)->leaderboard();

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

sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {
my ( $self, $c ) = @_;

my $distributions = $c->read_param('distribution');
my $user = $c->read_param('user');
my $data
= $self->model($c)->agg_by_distributions( $distributions, $user );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach(
$self->model($c)->agg_by_distributions(
$c->read_param('distribution'),
$c->read_param('user')
)
);
}

__PACKAGE__->meta->make_immutable;
Expand Down
7 changes: 1 addition & 6 deletions lib/MetaCPAN/Server/Controller/File.pm
Expand Up @@ -51,12 +51,7 @@ sub find : Path('') {

sub dir : Path('dir') {
my ( $self, $c, @path ) = @_;
my $data = $self->model($c)->dir(@path);

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

1;
8 changes: 1 addition & 7 deletions lib/MetaCPAN/Server/Controller/Mirror.pm
Expand Up @@ -11,13 +11,7 @@ with 'MetaCPAN::Server::Role::JSONP';

sub search : Path('search') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)->search( $c->req->param('q') );

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach( $self->model($c)->search( $c->req->param('q') ) );
}

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

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

$data
? $c->stash($data)
: $c->detach( '/not_found',
['The requested info could not be found'] );
$c->stash_or_detach(
$self->model($c)->get_modules( $dist, $last->{version} ) );
}

__PACKAGE__->meta->make_immutable;
Expand Down
25 changes: 4 additions & 21 deletions lib/MetaCPAN/Server/Controller/Permission.pm
Expand Up @@ -9,35 +9,18 @@ with 'MetaCPAN::Server::Role::JSONP';

sub by_author : Path('by_author') : Args(1) {
my ( $self, $c, $pauseid ) = @_;

my $data = $self->model($c)->by_author($pauseid);

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

sub by_module : Path('by_module') : Args(1) {
my ( $self, $c, $module ) = @_;

my $data = $self->model($c)->by_modules($module);

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

sub by_modules : Path('by_module') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)->by_modules( $c->read_param('module') );

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

__PACKAGE__->meta->make_immutable;
Expand Down
11 changes: 3 additions & 8 deletions lib/MetaCPAN/Server/Controller/Rating.pm
Expand Up @@ -11,14 +11,9 @@ with 'MetaCPAN::Server::Role::JSONP';

sub by_distributions : Path('by_distributions') : Args(0) {
my ( $self, $c ) = @_;

my $data = $self->model($c)
->by_distributions( $c->read_param('distribution') );

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

1;

0 comments on commit e6b65b9

Please sign in to comment.