Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added /author/by_user/USER endpoint
This endpoint will replace an Elasticsearch query in WEB.
  • Loading branch information
mickeyn committed May 26, 2017
1 parent 7f8212a commit 4b61692
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/MetaCPAN/Document/Author.pm
Expand Up @@ -147,6 +147,39 @@ sub validate {

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Author::Set;

use strict;
use warnings;

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

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

sub by_user {
my ( $self, $users ) = @_;

my $authors = $self->es->search(
index => $self->index->name,
type => 'author',
body => {
query => { term => { user => $users } },
size => 100,
fields => [qw( user pauseid )],
}
);
return {} unless $authors->{hits}{total};

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

return { authors => \@authors };
}

__PACKAGE__->meta->make_immutable;
1;

=pod
Expand Down
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -61,4 +61,11 @@ sub get : Path('') : Args(1) {
['The requested field(s) could not be found'] );
}

sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;
my $data = $self->model($c)->raw->by_user($user);
$data or return;
$c->stash($data);
}

1;

0 comments on commit 4b61692

Please sign in to comment.