From 6ecf98f917fbbe20a0763a6eecbf5d760917a699 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 4 Mar 2023 21:58:18 +0000 Subject: [PATCH] api: add /random/tag --- lib/PoorBooru/API/V0.pm | 2 ++ lib/PoorBooru/API/V0/Controller/Random.pm | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) 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 a9f8e09..656a3db 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -24,6 +24,8 @@ sub startup ($self) $r->get("/tags")->to("tags#list"); $r->get("/tag/:tag_id_or_name")->to("tags#show"); + + $r->get("/random/tag")->to("random#tag"); } 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..6ccfe8c --- /dev/null +++ b/lib/PoorBooru/API/V0/Controller/Random.pm @@ -0,0 +1,21 @@ +package PoorBooru::API::V0::Controller::Random; +use v5.36; +use strict; +use warnings; + +use Mojo::Base "Mojolicious::Controller"; + +sub tag ($self) +{ + my $tag = $self->schema->resultset("Tag") + ->search({}, { order_by => \"random()", limit => 1 })->single; + + return $self->render( + json => {error => "Tag not found"}, + status => 404, + ) if !defined($tag); + + return $self->redirect_to("/tag/" . $tag->name); +} + +1;