Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added /author/by_ids endpoint
This endpoint will replace query sending from WEB.
  • Loading branch information
mickeyn committed Jul 1, 2017
1 parent c2bd56c commit 5e85832
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/MetaCPAN/Document/Author.pm
Expand Up @@ -159,6 +159,35 @@ use Ref::Util qw( is_arrayref );

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

sub by_ids {
my ( $self, $ids ) = @_;

map {uc} @{$ids};

my $body = {
query => {
constant_score => {
filter => { ids => { values => $ids } }
}
},
size => scalar @{$ids},
};

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

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
$_->{_source}
} @{ $authors->{hits}{hits} };

return { authors => \@authors };
}

sub by_user {
my ( $self, $users ) = @_;
$users = [$users] unless is_arrayref($users);
Expand Down
14 changes: 14 additions & 0 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -70,6 +70,20 @@ sub qsearch : Path('search') : Args(0) {
$c->stash($data);
}

# /author/by_ids?id=PAUSE_ID1&id=PAUSE_ID2...
sub by_ids : Path('by_ids') : Args(0) {
my ( $self, $c ) = @_;
my $body_data = $c->req->body_data;
my $ids
= $body_data
? $body_data->{id}
: [ $c->req->param('id') ];
return unless $ids and @{$ids};
my $data = $self->model($c)->raw->by_ids($ids);
$data or return;
$c->stash($data);
}

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

0 comments on commit 5e85832

Please sign in to comment.