backend: handle paged /tags from API

This commit is contained in:
Lucas 2023-02-19 11:27:45 +00:00
parent 7e3fdadfc6
commit 5b8cbd37e5
2 changed files with 25 additions and 3 deletions

View File

@ -72,7 +72,12 @@ get "/" => sub {
};
get "/tags" => sub {
my $res = http_tiny()->get("$POORBOORU_API/tags");
my $page = query_parameters->get("page") // 1;
send_error("Invalid page number", 400) if $page !~ /^[1-9][0-9]*$/;
my $http = http_tiny();
my $params = $http->www_form_urlencode({ page => $page });
my $res = $http->get("$POORBOORU_API/tags?$params");
send_error("API error", 500) if !$res->{success};
my $data = decode_json($res->{content});
@ -80,11 +85,12 @@ get "/tags" => sub {
name => $_->{name},
count => $_->{count},
uri => uri_for("/tag/" . $_->{name}),
} ), @$data;
} ), @{$data->{tags}};
template "tags" => {
title => "Tags",
tags => \@tags,
pager => gallery_nav_pager($data->{pager}, "/tags"),
};
};

View File

@ -1,7 +1,23 @@
<h1>Tags</h1>
<h1>[% title %]</h1>
<p class="text-center">
[% FOREACH tag IN tags -%]
<a href="[% tag.uri %]">[% tag.name %] ([% tag.count %])</a>
[% END -%]
</p>
<nav class="text-center">
[% IF pager.first -%]
<a href="[% pager.first %]">&lt;&lt;</a>
[% END -%]
[% IF pager.prev -%]
<a href="[% pager.prev %]">&lt;</a>
[% END -%]
<span>[% pager.cur %]</span>
[% IF pager.next -%]
<a href="[% pager.next %]">&gt;</a>
[% END -%]
[% IF pager.last -%]
<a href="[% pager.last %]">&gt;&gt;</a>
[% END -%]
</nav>