From 5ce96a008748a72e8ec10b7c94cefefde435d819 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 17 Mar 2023 20:33:44 +0000 Subject: [PATCH] api: move Random controller into Tags and Media --- lib/PoorBooru/API/V0.pm | 19 +++++++++++-- lib/PoorBooru/API/V0/Controller/Media.pm | 7 +++++ lib/PoorBooru/API/V0/Controller/Random.pm | 34 ----------------------- lib/PoorBooru/API/V0/Controller/Tags.pm | 7 +++++ 4 files changed, 30 insertions(+), 37 deletions(-) delete mode 100644 lib/PoorBooru/API/V0/Controller/Random.pm diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index 1e73ae2..d10c605 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -7,6 +7,19 @@ use Mojo::Base "Mojolicious"; 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) { $self->moniker("poorbooru-api-v0"); @@ -21,6 +34,7 @@ sub startup ($self) map +( $_ => $dbp->$_ ), qw(first_page previous_page current_page next_page last_page) }); + $self->helper(random_entry => \&_random_entry); my $r = $self->routes; @@ -28,12 +42,11 @@ sub startup ($self) $r->get("/tags")->to("tags#list")->name("tags_list"); $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/:media_id")->to("media#show")->name("media_show"); - - $r->get("/random/media")->to("random#media")->name("media_random"); - $r->get("/random/tag")->to("random#tag")->name("tag_random"); + $r->get("/random/media")->to("media#random")->name("media_random"); } 1; diff --git a/lib/PoorBooru/API/V0/Controller/Media.pm b/lib/PoorBooru/API/V0/Controller/Media.pm index 57577d2..77a8d64 100644 --- a/lib/PoorBooru/API/V0/Controller/Media.pm +++ b/lib/PoorBooru/API/V0/Controller/Media.pm @@ -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; diff --git a/lib/PoorBooru/API/V0/Controller/Random.pm b/lib/PoorBooru/API/V0/Controller/Random.pm deleted file mode 100644 index 775ebaf..0000000 --- a/lib/PoorBooru/API/V0/Controller/Random.pm +++ /dev/null @@ -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; diff --git a/lib/PoorBooru/API/V0/Controller/Tags.pm b/lib/PoorBooru/API/V0/Controller/Tags.pm index 4340140..3648c35 100644 --- a/lib/PoorBooru/API/V0/Controller/Tags.pm +++ b/lib/PoorBooru/API/V0/Controller/Tags.pm @@ -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;