backend: implement /tags and /tag/:tag_id_or_name
This commit is contained in:
parent
ff017f924b
commit
02c7c3c0db
@ -21,6 +21,9 @@ sub startup ($self)
|
||||
my $r = $self->routes;
|
||||
|
||||
$r->get("/")->to("gallery#index");
|
||||
|
||||
$r->get("/tags")->to("tags#list");
|
||||
$r->get("/tag/:tag_id_or_name")->to("tags#show");
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -5,6 +5,7 @@ use warnings;
|
||||
|
||||
use Mojo::Base "Mojolicious::Controller";
|
||||
use Mojo::JSON qw(decode_json);
|
||||
use SeaweedFS::FID;
|
||||
|
||||
sub index ($self)
|
||||
{
|
||||
|
60
lib/PoorBooru/Backend/Controller/Tags.pm
Normal file
60
lib/PoorBooru/Backend/Controller/Tags.pm
Normal file
@ -0,0 +1,60 @@
|
||||
package PoorBooru::Backend::Controller::Tags;
|
||||
use v5.36;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Mojo::Base "Mojolicious::Controller";
|
||||
use Mojo::JSON qw(decode_json);
|
||||
use SeaweedFS::FID;
|
||||
|
||||
sub list ($self)
|
||||
{
|
||||
$self->title("tags");
|
||||
|
||||
my $v = $self->validation;
|
||||
my $page = $v->optional("page")->num(1, undef)->param // 1;
|
||||
|
||||
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;
|
||||
|
||||
return $self->render(status => $res->code) if $res->is_error;
|
||||
|
||||
my @tags = map +{
|
||||
name => $_->{name},
|
||||
count => $_->{count},
|
||||
uri => $self->url_for("/tag/$_->{name}"),
|
||||
}, @{$res->json->{tags}};
|
||||
|
||||
return $self->render(template => "tags", tags => \@tags);
|
||||
};
|
||||
|
||||
sub show ($self)
|
||||
{
|
||||
$self->title("tags");
|
||||
|
||||
my $v = $self->validation;
|
||||
my $page = $v->optional("page")->num(1, undef)->param // 1;
|
||||
|
||||
return $self->render(status => 404) if $v->has_error;
|
||||
|
||||
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")
|
||||
->result;
|
||||
|
||||
return $self->render(status => $res->code) if $res->is_error;
|
||||
|
||||
my @media = map +{
|
||||
media_src => $self->seaweed->url_for(
|
||||
SeaweedFS::FID->from_string($_->{seaweedfs_fid})
|
||||
),
|
||||
}, @{$res->json->{media}};
|
||||
|
||||
return $self->render(template => "gallery", media => \@media);
|
||||
};
|
||||
|
||||
1;
|
7
templates/tags.html.ep
Normal file
7
templates/tags.html.ep
Normal file
@ -0,0 +1,7 @@
|
||||
% layout "main";
|
||||
|
||||
<p class="text-center">
|
||||
% for my $t (@$tags) {
|
||||
<%= link_to "$t->{name} ($t->{count})" => $t->{uri} %>
|
||||
% }
|
||||
</p>
|
Loading…
Reference in New Issue
Block a user