Skip to content

Commit

Permalink
Added /dir/PATH endpoint
Browse files Browse the repository at this point in the history
This new endpoint will be used to replace the query sent from
WEB.
  • Loading branch information
mickeyn committed Jun 2, 2017
1 parent e02f92c commit 9916345
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/MetaCPAN/Document/File/Set.pm
Expand Up @@ -2,6 +2,8 @@ package MetaCPAN::Document::File::Set;

use Moose;

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

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

my @ROGUE_DISTRIBUTIONS = qw(
Expand Down Expand Up @@ -562,5 +564,44 @@ sub autocomplete_using_suggester {
};
}

sub dir {
my ( $self, $author, $release, @path ) = @_;

my $body = {
query => {
bool => {
must => [
{ term => { 'level' => scalar @path } },
{ term => { 'author' => $author } },
{ term => { 'release' => $release } },
{
prefix => {
'path' => join( q{/}, @path, q{} )
}
},
]
},
},
size => 999,
fields => [
qw(name stat.mtime path stat.size directory slop documentation mime)
],
};

my $data = $self->es->search(
{
index => $self->index->name,
type => 'file',
body => $body,
}
);
return unless $data->{hits}{total};

my $dir = [ map { $_->{fields} } @{ $data->{hits}{hits} } ];
single_valued_arrayref_to_scalar($dir);

return { dir => $dir };
}

__PACKAGE__->meta->make_immutable;
1;
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/File.pm
Expand Up @@ -49,4 +49,11 @@ sub find : Path('') {
} or $c->detach( '/not_found', [$@] );
}

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

1;

0 comments on commit 9916345

Please sign in to comment.