Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a /package/modules/DIST endpoint
This new endpoint will be used for the new perms page
  • Loading branch information
mickeyn committed May 11, 2017
1 parent 3bfaf96 commit c2a9ce8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/MetaCPAN/Document/Package.pm
Expand Up @@ -21,5 +21,45 @@ has file => (
isa => Str,
);

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Package::Set;

use strict;
use warnings;

use Moose;

extends 'ElasticSearchX::Model::Document::Set';

sub get_modules {
my ( $self, $dist, $ver ) = @_;

my $query = +{
query => {
bool => {
must => [
{ term => { distribution => $dist } },
{ term => { dist_version => $ver } },
],
}
}
};

my $res = $self->es->search(
index => $self->index->name,
type => 'package',
body => {
query => $query,
size => 999,
_source => [qw< module_name >],
}
);

my $hits = $res->{hits}{hits};
return [] unless @{$hits};
return +{ modules => [ map { $_->{_source}{module_name} } @{$hits} ] };
}

__PACKAGE__->meta->make_immutable;
1;
10 changes: 10 additions & 0 deletions lib/MetaCPAN/Server/Controller/Package.pm
Expand Up @@ -7,5 +7,15 @@ BEGIN { extends 'MetaCPAN::Server::Controller' }

with 'MetaCPAN::Server::Role::JSONP';

# https://fastapi.metacpan.org/v1/package/modules/Moose
sub modules : Path('modules') : Args(1) {
my ( $self, $c, $dist ) = @_;
my $last = $c->model('CPAN::Release')->raw->find($dist);
return unless $last;
my $data
= $self->model($c)->get_modules( $dist, $last->{_source}{version} );
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;
14 changes: 14 additions & 0 deletions t/server/controller/package.t
Expand Up @@ -32,6 +32,20 @@ test_psgi app, sub {
'Has the correct 02packages info'
);
}

{
my $dist = 'File-Changes-UTF8';
ok( my $res = $cb->( GET "/package/modules/$dist" ),
"GET modules/$dist" );
is( $res->code, 200, '200 OK' );
is_deeply(
decode_json( $res->content ),
{
modules => ['File::Changes::UTF8'],
},
'Can list modules of latest release'
);
}
};

done_testing;

0 comments on commit c2a9ce8

Please sign in to comment.