Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #504 from metacpan/mickey/GH502
GET controller for Author, add release count info
  • Loading branch information
oalders committed Jul 10, 2016
2 parents 1f1f048 + a369827 commit 4b9dc42
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/MetaCPAN/Document/Release.pm
Expand Up @@ -285,6 +285,32 @@ use warnings;
use Moose;
extends 'ElasticSearchX::Model::Document::Set';

sub aggregate_status_by_author {
my ( $self, $pauseid ) = @_;
my $agg = $self->es->search(
{
index => $self->index->name,
type => 'release',
body => {
query => {
term => { author => $pauseid }
},
aggregations => {
count => { terms => { field => 'status' } }
},
size => 0,
}
}
);
my %ret = ( cpan => 0, latest => 0, backpan => 0 );
if ($agg) {
$ret{ $_->{'key'} } = $_->{'doc_count'}
for @{ $agg->{'aggregations'}{'count'}{'buckets'} };
}
$ret{'backpan-only'} = delete $ret{'backpan'};
return \%ret;
}

sub find_depending_on {
my ( $self, $modules ) = @_;
return $self->filter(
Expand Down
17 changes: 17 additions & 0 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -24,4 +24,21 @@ __PACKAGE__->config(
}
);

sub get : Path('') : Args(1) {
my ( $self, $c, $id ) = @_;
my $file = $self->model($c)->raw->get($id);
if ( !defined $file ) {
$c->detach( '/not_found', ['Not found'] );
}
my $st = $file->{_source} || $file->{fields};
if ( $st and $st->{pauseid} ) {
$st->{release_count}
= $c->model('CPAN::Release')
->aggregate_status_by_author( $st->{pauseid} );
}
$c->stash($st)
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}

1;
12 changes: 10 additions & 2 deletions t/server/controller/author.t
Expand Up @@ -111,8 +111,16 @@ test_psgi app, sub {
$json = decode_json_ok($res);

is( @{ $json->{hits}->{hits} }, 1, '1 hit' );
is_deeply( $json->{hits}->{hits}->[0]->{_source},
$doy, 'same result as direct get' );

my $release_count = delete $doy->{release_count};
is_deeply(
[ sort keys %{$release_count} ],
[qw< backpan-only cpan latest >],
'release_count has the correct keys'
);

my $source = $json->{hits}->{hits}->[0]->{_source};
is_deeply( $doy, $source, 'same result as direct get' );

{
ok( my $res = $cb->( GET '/author/_search?q=*&size=99999' ),
Expand Down

0 comments on commit 4b9dc42

Please sign in to comment.