Skip to content

Commit

Permalink
Added /favorite/recent endpoint
Browse files Browse the repository at this point in the history
This endpoint will replace the query sent from WEB.
  • Loading branch information
mickeyn committed Jun 1, 2017
1 parent 2cf8eab commit 6dff6d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/MetaCPAN/Document/Favorite.pm
Expand Up @@ -101,5 +101,31 @@ sub by_user {
return { favorites => \@favs, took => $took };
}

sub recent {
my ( $self, $page, $size ) = @_;
$page //= 1;
$size //= 100;

my $favs = $self->es->search(
index => $self->index->name,
type => 'favorite',
body => {
size => $size,
from => ( $page - 1 ) * $size,
query => { match_all => {} },
sort => [ { 'date' => { order => 'desc' } } ]
}
);
return {} unless $favs->{hits}{total};

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

return +{
favorites => \@favs,
took => $favs->{took},
total => $favs->{total}
};
}

__PACKAGE__->meta->make_immutable;
1;
9 changes: 9 additions & 0 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -36,5 +36,14 @@ sub by_user : Path('by_user') : Args(1) {
$c->stash($data);
}

sub recent : Path('recent') : Args(0) {
my ( $self, $c ) = @_;
my $page = $c->req->param('page') || 1;
my $size = $c->req->param('size') || 100;
my $data = $self->model($c)->raw->recent( $page, $size );
$data or return;
$c->stash($data);
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit 6dff6d1

Please sign in to comment.