Skip to content

Commit

Permalink
support paging
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Nov 24, 2016
1 parent 7f412bd commit 54edd9b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/MetaCPAN/Role/ES/Query.pm
Expand Up @@ -29,7 +29,9 @@ sub es_query_res {
my ( $req, $res, $cb ) = @args{qw< req res cb >};
my $params = $req->parameters;

my $size = $params->{size} || 5000;
my $size = $params->{size} // 5000;
my $from = 0;
$params->{page} and $from = ( $params->{page} - 1 ) * $size;

my @fields;
if ( $params->{fields} ) {
Expand All @@ -45,6 +47,7 @@ sub es_query_res {

$res = $res->fields( \@fields ) if @fields;
$res = $res->sort( \@sort ) if @sort;
$res = $res->from($from) if $from;
$res = $res->size($size)->all;
$res = $cb->($res) if $cb and is_coderef($cb);

Expand Down
16 changes: 12 additions & 4 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -59,29 +59,37 @@ sub get : Path('') : Args(1) {
['The requested field(s) could not be found'] );
}

# endpoint: /author/by_id?id=<pauseid>[&fields=<field>][&sort=<sort_key>][&size=N]
# endpoint: /author/by_id
# params: id=<pauseid>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub by_id : Path('by_id') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->by_id( $c->req );
$c->stash($data);
}

# endpoint: /author/by_user?user=<user_id>[&fields=<field>][&sort=<sort_key>][&size=N]
# endpoint: /author/by_user
# params: user=<user_id>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub by_user : Path('by_user') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->by_user( $c->req );
$c->stash($data);
}

# endpoint: /author/by_key?key=<key>[&fields=<field>][&sort=<sort_key>][&size=N]
# endpoint: /author/by_key
# params: key=<key>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub by_key : Path('by_key') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->by_key( $c->req );
$data or return;
$c->stash($data);
}

# endpoint: /author/top_uploaders?range=<range>[&fields=<field>][&sort=<sort_key>][&size=N]
# endpoint: /author/top_uploaders
# params: range=<range>
# [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub top_uploaders : Path('top_uploaders') : Args(0) {
my ( $self, $c ) = @_;
my $range = $c->req->parameters->{range};
Expand Down
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -26,7 +26,9 @@ sub find : Path('') : Args(2) {
} or $c->detach( '/not_found', [$@] );
}

# endpoint: /favorite/by_user?user=<id>[&user=<id2>][&fields=<field>][&sort=<sort_key>][&size=N]
# endpoint: /favorite/by_user
# params: user=<id>[&user=<id2>]...
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub by_user : Path('by_user') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->by_user( $c->req );
Expand Down
12 changes: 8 additions & 4 deletions lib/MetaCPAN/Server/Controller/Release.pm
Expand Up @@ -50,31 +50,35 @@ sub get : Path('') : Args(2) {
}

# endpoint: /release/latest_by_author
# params: author=<pauseid>[&fields=<field>][&sort=<sort_key>][&size=N]
# params: author=<pauseid>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub latest_by_author : Path('latest_by_author') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->latest_by_author($c->req);
$c->stash($data);
}

# endpoint: /release/all_by_author
# params: author=<pauseid>[&fields=<field>][&sort=<sort_key>][&size=N]
# params: author=<pauseid>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub all_by_author : Path('all_by_author') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->all_by_author($c->req);
$c->stash($data);
}

# endpoint: /release/by_name_and_author
# params: name=<name>&author=<author>[&fields=<field>][&sort=<sort_key>][&size=N]
# params: name=<name>&author=<author>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub by_name_and_author : Path('by_name_and_author') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->by_name_and_author( $c->req );
$c->stash($data);
}

# endpoint: /release/versions
# params: distribution=<distribution>[&fields=<field>][&sort=<sort_key>][&size=N]
# params: distribution=<distribution>
# optional: [&fields=<field>][&sort=<sort_key>][&size=N][&page=N]
sub versions : Path('versions') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->versions( $c->req );
Expand Down

0 comments on commit 54edd9b

Please sign in to comment.