From 56ee34eb7ca4a86953a7a0a356350a0d80d6fc96 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 20 Mar 2023 20:49:16 +0000 Subject: [PATCH] api: partially revert 5ce96a008748a72e8ec10b7c94cefefde435d819 --- 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, 37 insertions(+), 30 deletions(-) create mode 100644 lib/PoorBooru/API/V0/Controller/Random.pm diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index d10c605..02e4297 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -7,19 +7,6 @@ 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"); @@ -34,7 +21,6 @@ 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; @@ -42,11 +28,12 @@ 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("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; diff --git a/lib/PoorBooru/API/V0/Controller/Media.pm b/lib/PoorBooru/API/V0/Controller/Media.pm index 434701e..6aa40a9 100644 --- a/lib/PoorBooru/API/V0/Controller/Media.pm +++ b/lib/PoorBooru/API/V0/Controller/Media.pm @@ -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; diff --git a/lib/PoorBooru/API/V0/Controller/Random.pm b/lib/PoorBooru/API/V0/Controller/Random.pm new file mode 100644 index 0000000..88c9960 --- /dev/null +++ b/lib/PoorBooru/API/V0/Controller/Random.pm @@ -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; diff --git a/lib/PoorBooru/API/V0/Controller/Tags.pm b/lib/PoorBooru/API/V0/Controller/Tags.pm index 3648c35..4340140 100644 --- a/lib/PoorBooru/API/V0/Controller/Tags.pm +++ b/lib/PoorBooru/API/V0/Controller/Tags.pm @@ -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;