From a7f2381d81b4284350bbafa1b32fe6c85591ee4d Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 18 Feb 2023 19:01:35 +0000 Subject: [PATCH] api: provide a better paging object --- lib/PoorBooru/API/V0.pm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index 5caae23..3c81c05 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -21,10 +21,16 @@ use constant { ROWS_PER_PAGE => 20, }; -sub surrounding_pages ($) +sub mkpager ($) { 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 { @@ -79,18 +85,16 @@ get "/tag/:tag_id_or_name" => sub { get "/media" => sub { 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") ->search({}, { order_by => "upload_date", rows => ROWS_PER_PAGE }) ->page($page); - my ($prev, $next) = surrounding_pages($paged_media); my @media = map { $_->media_id } $paged_media->all; return { - next => ($next and "/media?page=$next"), - prev => ($prev and "/media?page=$prev"), + pager => mkpager($paged_media), media => \@media, }; };