backend: implement paging

This commit is contained in:
Lucas 2023-03-10 20:34:00 +00:00
parent 79a9173574
commit fa07cf5645
4 changed files with 51 additions and 9 deletions

View File

@ -6,6 +6,26 @@ use warnings;
use Mojo::Base "Mojolicious";
use SeaweedFS::Client;
sub _pager_links ($c, $dbp)
{
my %pager;
my $page_url = sub ($p) { $c->url_with->query({page => $p}) };
$pager{first_page} = $page_url->($dbp->{first_page})
if $dbp->{first_page} != $dbp->{current_page};
$pager{previous_page} = $page_url->($dbp->{previous_page})
if defined($dbp->{previous_page}) &&
$dbp->{previous_page} != $dbp->{first_page};
$pager{current_page} = $dbp->{current_page};
$pager{next_page} = $page_url->($dbp->{next_page})
if defined($dbp->{next_page}) &&
$dbp->{next_page} != $dbp->{last_page};
$pager{last_page} = $page_url->($dbp->{last_page})
if $dbp->{last_page} != $dbp->{current_page};
return %pager;
}
sub startup ($self)
{
$self->moniker("poorbooru-backend");
@ -17,6 +37,7 @@ sub startup ($self)
state $client =
SeaweedFS::Client->new($config->{seaweedfs_master})
});
$self->helper(pager_links => \&_pager_links);
my $r = $self->routes;

View File

@ -19,8 +19,7 @@ sub index ($self)
) if $v->has_error;
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/media?page=$page")->result;
my $res = $self->ua->get("$poorbooru_api/media?page=$page")->result;
return $self->render(status => $res->code) if $res->is_error;
@ -30,7 +29,11 @@ sub index ($self)
),
}, @{$res->json->{media}};
return $self->render(template => "gallery", media => \@media);
return $self->render(
template => "gallery",
media => \@media,
pager => { $self->pager_links($res->json->{pager}) },
);
};
1;

View File

@ -17,8 +17,7 @@ sub list ($self)
return $self->render(status => 404) if $v->has_error;
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/tags?page=$page")->result;
my $res = $self->ua->get("$poorbooru_api/tags?page=$page")->result;
return $self->render(status => $res->code) if $res->is_error;
@ -28,7 +27,11 @@ sub list ($self)
uri => $self->url_for("/tag/$_->{name}"),
}, @{$res->json->{tags}};
return $self->render(template => "tags", tags => \@tags);
return $self->render(
template => "tags",
tags => \@tags,
pager => { $self->pager_links($res->json->{pager}) },
);
};
sub show ($self)
@ -42,8 +45,7 @@ sub show ($self)
my $tag_id_or_name = $self->stash("tag_id_or_name");
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/tag/$tag_id_or_name?page=$page")
my $res = $self->ua->get("$poorbooru_api/tag/$tag_id_or_name?page=$page")
->result;
return $self->render(status => $res->code) if $res->is_error;
@ -54,7 +56,11 @@ sub show ($self)
),
}, @{$res->json->{media}};
return $self->render(template => "gallery", media => \@media);
return $self->render(
template => "gallery",
media => \@media,
pager => { $self->pager_links($res->json->{pager}) },
);
};
1;

View File

@ -20,6 +20,18 @@
<div class="viewport flex-i-fullsize flex-c-vertical">
<h1><%= title %></h1>
<div class="viewport flex-i-fullsize"><%= content %></div>
<% if (defined $pager) { %>
<footer>
<!--<pre><code><%= dumper $pager %></code></pre>-->
<nav class="text-center">
<%= link_to "<<" => $pager->{first_page} if exists $pager->{first_page} %>
<%= link_to "<" => $pager->{previous_page} if exists $pager->{previous_page} %>
<span><%= $pager->{current_page} %></span>
<%= link_to ">" => $pager->{next_page} if exists $pager->{next_page} %>
<%= link_to ">>" => $pager->{last_page} if exists $pager->{last_page} %>
</nav>
</footer>
<% } %>
</div>
</main>
<footer class="text-center">