diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index 8febbc1..517be80 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -85,21 +85,27 @@ post "/tags" => sub { }; get "/tag/:tag_id_or_name" => sub { - my ($tag, $tag_rset, @media); + my $page = query_parameters->get("page") // 1; + send_error("Invalid page number", 400) if $page !~ $NUMERIC_ID_RE; + my $tag_id_or_name = route_parameters->get("tag_id_or_name"); - $tag_rset = schema("default")->resultset("Tag"); - $tag = $tag_rset->single({ tag_id => $tag_id_or_name }) // - $tag_rset->single({ name => $tag_id_or_name }); - + my $query = [ + { tag_id => $tag_id_or_name }, + { name => $tag_id_or_name }, + ]; + my $tag = schema("default")->resultset("Tag")->single($query); send_error("Tag not found", 404) if !defined($tag); - @media = map { $_->media_id } schema("default")->resultset("MediaTag") - ->search({ tag_id => $tag->tag_id })->all; + my $paged_media = schema("default")->resultset("MediaTag") + ->search({ tag_id => $tag->tag_id }, $MEDIA_SEARCH_OPTS) + ->page($page); + my @media = map { $_->media_id } $paged_media->all; return { id => $tag->tag_id, name => $tag->name, + pager => mkpager($paged_media), media => \@media, }; }; @@ -110,7 +116,6 @@ get "/media" => sub { my $paged_media = schema("default")->resultset("Media") ->search({}, $MEDIA_SEARCH_OPTS)->page($page); - my @media = map { $_->media_id } $paged_media->all; return {