api: partially revert 5ce96a0087

This commit is contained in:
Lucas 2023-03-20 20:49:16 +00:00
parent 489c31414c
commit 56ee34eb7c
4 changed files with 37 additions and 30 deletions

View File

@ -7,19 +7,6 @@ use Mojo::Base "Mojolicious";
use PoorBooru::Schema; use PoorBooru::Schema;
sub _random_entry ($c, $rs, $cb)
{
my $entry = $c->schema->resultset($rs)
->search(undef, { order_by => \"random()", rows => 1 })->single;
return $c->render(
json => { error => "$rs not found" },
status => 404,
) if !defined($entry);
return $cb->($c, $entry);
}
sub startup ($self) sub startup ($self)
{ {
$self->moniker("poorbooru-api-v0"); $self->moniker("poorbooru-api-v0");
@ -34,7 +21,6 @@ sub startup ($self)
map +( $_ => $dbp->$_ ), qw(first_page previous_page map +( $_ => $dbp->$_ ), qw(first_page previous_page
current_page next_page last_page) current_page next_page last_page)
}); });
$self->helper(random_entry => \&_random_entry);
my $r = $self->routes; my $r = $self->routes;
@ -42,11 +28,12 @@ sub startup ($self)
$r->get("/tags")->to("tags#list")->name("tags_list"); $r->get("/tags")->to("tags#list")->name("tags_list");
$r->get("/tag/:tag_id_or_name")->to("tags#show")->name("tag_show"); $r->get("/tag/:tag_id_or_name")->to("tags#show")->name("tag_show");
$r->get("/random/tag")->to("tags#random")->name("tag_random");
$r->get("/media")->to("media#list")->name("media_list"); $r->get("/media")->to("media#list")->name("media_list");
$r->get("/media/:media_id")->to("media#show")->name("media_show"); $r->get("/media/:media_id")->to("media#show")->name("media_show");
$r->get("/random/media")->to("media#random")->name("media_random");
$r->get("/random/tag")->to("random#tag")->name("tag_random");
$r->get("/random/media")->to("random#media")->name("media_random");
} }
1; 1;

View File

@ -74,11 +74,4 @@ sub show ($self)
}); });
}; };
sub random ($self)
{
return $self->random_entry("Media", sub ($c, $m) {
$c->redirect_to("media_show", media_id => $m->media_id)
});
}
1; 1;

View File

@ -0,0 +1,34 @@
package PoorBooru::API::V0::Controller::Random;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
sub media ($self)
{
$self->_random_entry("Media", sub ($c, $m) {
$c->redirect_to("media_show", media_id => $m->media_id)
})
}
sub tag ($self)
{
$self->_random_entry("Tag", sub ($c, $t) {
$c->redirect_to("tag_show", tag_id_or_name => $t->name)
})
}
sub _random_entry ($self, $rs, $cb) {
my $entry = $self->schema->resultset($rs)
->search(undef, { order_by => \"random()", rows => 1 })->single;
return $self->render(
json => {error => "$rs not found"},
status => 404,
) if !defined($entry);
return $cb->($self, $entry);
}
1;

View File

@ -83,11 +83,4 @@ sub show ($self)
}); });
}; };
sub random ($self)
{
return $self->random_entry("Tag", sub ($c, $t) {
$c->redirect_to("tag_show", tag_id_or_name => $t->name)
});
}
1; 1;