api: provide a better paging object
This commit is contained in:
parent
216422f50a
commit
a7f2381d81
@ -21,10 +21,16 @@ use constant {
|
|||||||
ROWS_PER_PAGE => 20,
|
ROWS_PER_PAGE => 20,
|
||||||
};
|
};
|
||||||
|
|
||||||
sub surrounding_pages ($)
|
sub mkpager ($)
|
||||||
{
|
{
|
||||||
my $pager = $_[0]->pager;
|
my $pager = $_[0]->pager;
|
||||||
return $pager->previous_page, $pager->next_page;
|
return {
|
||||||
|
first => $pager->first_page,
|
||||||
|
prev => $pager->previous_page,
|
||||||
|
cur => $pager->current_page,
|
||||||
|
next => $pager->next_page,
|
||||||
|
last => $pager->last_page,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
get "/meta" => sub {
|
get "/meta" => sub {
|
||||||
@ -79,18 +85,16 @@ get "/tag/:tag_id_or_name" => sub {
|
|||||||
|
|
||||||
get "/media" => sub {
|
get "/media" => sub {
|
||||||
my $page = query_parameters->get("page") // 1;
|
my $page = query_parameters->get("page") // 1;
|
||||||
send_error("Invalide page number", 400) if $page !~ /^[1-9][0-9]*$/;
|
send_error("Invalid page number", 400) if $page !~ /^[1-9][0-9]*$/;
|
||||||
|
|
||||||
my $paged_media = schema("default")->resultset("Media")
|
my $paged_media = schema("default")->resultset("Media")
|
||||||
->search({}, { order_by => "upload_date", rows => ROWS_PER_PAGE })
|
->search({}, { order_by => "upload_date", rows => ROWS_PER_PAGE })
|
||||||
->page($page);
|
->page($page);
|
||||||
|
|
||||||
my ($prev, $next) = surrounding_pages($paged_media);
|
|
||||||
my @media = map { $_->media_id } $paged_media->all;
|
my @media = map { $_->media_id } $paged_media->all;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
next => ($next and "/media?page=$next"),
|
pager => mkpager($paged_media),
|
||||||
prev => ($prev and "/media?page=$prev"),
|
|
||||||
media => \@media,
|
media => \@media,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user