api: only do random entry fetching in _random_entry
This commit is contained in:
parent
48f3445706
commit
724bfbc82a
@ -7,28 +7,31 @@ 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)
|
||||
})
|
||||
my $media = $self->_random_entry("Media");
|
||||
|
||||
return $self->render(
|
||||
json => {error => "Media not found"},
|
||||
status => 404,
|
||||
) if !defined($media);
|
||||
|
||||
return $self->redirect_to("media_show", media_id => $media->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;
|
||||
my $tag = $self->_random_entry("Tag");
|
||||
|
||||
return $self->render(
|
||||
json => {error => "$rs not found"},
|
||||
json => {error => "Tag not found"},
|
||||
status => 404,
|
||||
) if !defined($entry);
|
||||
) if !defined($tag);
|
||||
|
||||
return $cb->($self, $entry);
|
||||
return $self->redirect_to("tag_show", tag_id_or_name => $tag->name);
|
||||
}
|
||||
|
||||
sub _random_entry ($self, $rs) {
|
||||
$self->schema->resultset($rs)
|
||||
->search(undef, { order_by => \"random()", rows => 1 })->single
|
||||
}
|
||||
|
||||
1;
|
||||
|
Loading…
Reference in New Issue
Block a user