diff --git a/lib/PoorBooru/Backend.pm b/lib/PoorBooru/Backend.pm index 5719a27..41328f1 100644 --- a/lib/PoorBooru/Backend.pm +++ b/lib/PoorBooru/Backend.pm @@ -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; diff --git a/lib/PoorBooru/Backend/Controller/Gallery.pm b/lib/PoorBooru/Backend/Controller/Gallery.pm index 3d20f44..ec2b4f6 100644 --- a/lib/PoorBooru/Backend/Controller/Gallery.pm +++ b/lib/PoorBooru/Backend/Controller/Gallery.pm @@ -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; diff --git a/lib/PoorBooru/Backend/Controller/Tags.pm b/lib/PoorBooru/Backend/Controller/Tags.pm index ef3da51..9750780 100644 --- a/lib/PoorBooru/Backend/Controller/Tags.pm +++ b/lib/PoorBooru/Backend/Controller/Tags.pm @@ -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; diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep index 019cb60..b9d64d4 100644 --- a/templates/layouts/main.html.ep +++ b/templates/layouts/main.html.ep @@ -20,6 +20,18 @@