Skip to content

Commit

Permalink
Added /favorite/users_by_distribution/DISTRIBUTION API endpoint
Browse files Browse the repository at this point in the history
The new endpoint will be used to replace the query being sent
from WEB.
  • Loading branch information
mickeyn committed Jun 7, 2017
1 parent 2ae2de4 commit 7982086
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/MetaCPAN/Document/Favorite.pm
Expand Up @@ -101,6 +101,27 @@ sub by_user {
return { favorites => \@favs, took => $took };
}

sub users_by_distribution {
my ( $self, $distribution ) = @_;

my $favs = $self->es->search(
index => $self->index->name,
type => 'favorite',
body => {
query => { term => { distribution => $distribution } },
_source => ['user'],
size => 1000,
}
);
return {} unless $favs->{hits}{total};

my @plusser_users = map { $_->{_source}{user} } @{ $favs->{hits}{hits} };

single_valued_arrayref_to_scalar( \@plusser_users );

return { users => \@plusser_users };
}

sub recent {
my ( $self, $page, $size ) = @_;
$page //= 1;
Expand Down
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -36,6 +36,13 @@ sub by_user : Path('by_user') : Args(1) {
$c->stash($data);
}

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

sub recent : Path('recent') : Args(0) {
my ( $self, $c ) = @_;
my $page = $c->req->param('page') || 1;
Expand Down

0 comments on commit 7982086

Please sign in to comment.