Permalink
Browse files

Merge pull request #707 from metacpan/mickey/author_by_ids_endpoint

Added /author/by_ids endpoint
  • Loading branch information...
2 parents 719440e + 5e85832 commit dfd83dd137852f3101c99ebfd084fbe972657ec9 @oalders oalders committed Jul 4, 2017
Showing with 43 additions and 0 deletions.
  1. +29 −0 lib/MetaCPAN/Document/Author.pm
  2. +14 −0 lib/MetaCPAN/Server/Controller/Author.pm
@@ -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);
@@ -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 ) = @_;

0 comments on commit dfd83dd

Please sign in to comment.