From c83c481c8224a017c2c66dcfd6dc67dc3ee2f2d8 Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Fri, 2 Jun 2017 14:51:08 +0100 Subject: [PATCH] Added /release/latest_by_author/PAUSEID This new endpoint will replace an Elasticsearch sent from WEB. --- lib/MetaCPAN/Document/Release.pm | 34 +++++++++++++++++++++++ lib/MetaCPAN/Server/Controller/Release.pm | 7 +++++ 2 files changed, 41 insertions(+) diff --git a/lib/MetaCPAN/Document/Release.pm b/lib/MetaCPAN/Document/Release.pm index 24cb6e668..ed9fd970d 100644 --- a/lib/MetaCPAN/Document/Release.pm +++ b/lib/MetaCPAN/Document/Release.pm @@ -285,6 +285,9 @@ use strict; use warnings; use Moose; + +use MetaCPAN::Util qw( single_valued_arrayref_to_scalar ); + extends 'ElasticSearchX::Model::Document::Set'; sub aggregate_status_by_author { @@ -664,5 +667,36 @@ sub activity { return { activity => $line }; } +sub latest_by_author { + my ( $self, $pauseid ) = @_; + + my $body = { + query => { + bool => { + must => [ + { term => { author => uc($pauseid) } }, + { term => { status => 'latest' } } + ] + } + }, + sort => + [ 'distribution', { 'version_numified' => { reverse => 1 } } ], + fields => [qw(author distribution name status abstract date)], + size => 1000, + }; + + my $ret = $self->es->search( + index => $self->index->name, + type => 'release', + body => $body, + ); + return unless $ret->{hits}{total}; + + my $data = [ map { $_->{fields} } @{ $ret->{hits}{hits} } ]; + single_valued_arrayref_to_scalar($data); + + return { took => $ret->{took}, releases => $data }; +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/MetaCPAN/Server/Controller/Release.pm b/lib/MetaCPAN/Server/Controller/Release.pm index f9a741945..1822d1aa7 100644 --- a/lib/MetaCPAN/Server/Controller/Release.pm +++ b/lib/MetaCPAN/Server/Controller/Release.pm @@ -75,5 +75,12 @@ sub requires : Path('requires') : Args(1) { $c->stash($data); } +sub latest_by_author : Path('latest_by_author') : Args(1) { + my ( $self, $c, $pauseid ) = @_; + my $data = $self->model($c)->raw->latest_by_author($pauseid); + return unless $data; + $c->stash($data); +} + __PACKAGE__->meta->make_immutable; 1;