api: add /random/tag

This commit is contained in:
Lucas 2023-03-04 21:58:18 +00:00
parent 7ccbc3af12
commit 6ecf98f917
2 changed files with 23 additions and 0 deletions

View File

@ -24,6 +24,8 @@ sub startup ($self)
$r->get("/tags")->to("tags#list");
$r->get("/tag/:tag_id_or_name")->to("tags#show");
$r->get("/random/tag")->to("random#tag");
}
1;

View File

@ -0,0 +1,21 @@
package PoorBooru::API::V0::Controller::Random;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
sub tag ($self)
{
my $tag = $self->schema->resultset("Tag")
->search({}, { order_by => \"random()", limit => 1 })->single;
return $self->render(
json => {error => "Tag not found"},
status => 404,
) if !defined($tag);
return $self->redirect_to("/tag/" . $tag->name);
}
1;