From 6dff6d11bc01cbfe1edb177895b42252f199496e Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Thu, 1 Jun 2017 11:30:41 +0100 Subject: [PATCH] Added /favorite/recent endpoint This endpoint will replace the query sent from WEB. --- lib/MetaCPAN/Document/Favorite.pm | 26 ++++++++++++++++++++++ lib/MetaCPAN/Server/Controller/Favorite.pm | 9 ++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/MetaCPAN/Document/Favorite.pm b/lib/MetaCPAN/Document/Favorite.pm index 7d2681243..1e4970006 100644 --- a/lib/MetaCPAN/Document/Favorite.pm +++ b/lib/MetaCPAN/Document/Favorite.pm @@ -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; diff --git a/lib/MetaCPAN/Server/Controller/Favorite.pm b/lib/MetaCPAN/Server/Controller/Favorite.pm index 2d46bed2d..6b50a1d99 100644 --- a/lib/MetaCPAN/Server/Controller/Favorite.pm +++ b/lib/MetaCPAN/Server/Controller/Favorite.pm @@ -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;