backend: implement main section

This commit is contained in:
Lucas 2023-02-18 19:10:33 +00:00
parent a7f2381d81
commit 6eae4e0fe4
2 changed files with 52 additions and 2 deletions

View File

@ -21,6 +21,25 @@ sub http_tiny ()
);
}
sub gallery_nav_pager ($$)
{
my ($p, $uri) = @_;
my (%pager, %res);
%pager = %$p;
$res{first} = $pager{first} != $pager{cur} ?
uri_for($uri, { page => $pager{first} }) : undef;
$res{prev} = defined($pager{prev}) && $pager{prev} != $pager{first} ?
uri_for($uri, { page => $pager{prev} }) : undef;
$res{cur} = $pager{cur};
$res{next} = defined($pager{next}) && $pager{next} != $pager{last} ?
uri_for($uri, { page => $pager{next} }) : undef;
$res{last} = $pager{last} != $pager{cur} ?
uri_for($uri, { page => $pager{last} }) : undef;
return \%res;
}
hook before_template_render => sub {
my $tokens = shift;
@ -32,8 +51,23 @@ hook before_template_render => sub {
};
get "/" => sub {
template "index" => {
title => "main",
my $page = query_parameters->get("page") // 1;
send_error("Invalid page number", 400) if $page !~ /^[1-9][0-9]*$/;
my $res = http_tiny()->get("$POORBOORU_API/media?page=$page");
send_error("API error", 500) if !$res->{success};
my $data = decode_json($res->{content});
my @media = map +( {
# XXX point to a cache
image_src => "$POORBOORU_API/download/$_",
view_uri => uri_for("/view/$_"),
} ), @{$data->{media}};
template "gallery" => {
title => "PoorBooru",
media => \@media,
pager => gallery_nav_pager($data->{pager}, "/"),
};
};

View File

@ -7,3 +7,19 @@
</a>
[% END -%]
</div>
<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>