api: move Random controller into Tags and Media
This commit is contained in:
parent
fc32afcb79
commit
5ce96a0087
@ -7,6 +7,19 @@ 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");
|
||||||
@ -21,6 +34,7 @@ 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;
|
||||||
|
|
||||||
@ -28,12 +42,11 @@ 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/media")->to("random#media")->name("media_random");
|
|
||||||
$r->get("/random/tag")->to("random#tag")->name("tag_random");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -73,4 +73,11 @@ 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;
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
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/" . $m->media_id)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
sub tag ($self)
|
|
||||||
{
|
|
||||||
$self->_random_entry("Tag", sub ($c, $t) {
|
|
||||||
$c->redirect_to("/tag/" . $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;
|
|
@ -83,4 +83,11 @@ 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user