From 7e3fdadfc6eeb9590582f3af7882423e7de0b9c6 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 19 Feb 2023 10:42:15 +0000 Subject: [PATCH] api: page /tags results While at it, move /media search opts to a variable too. --- lib/PoorBooru/API/V0.pm | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index 1c5d658..5eea422 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -18,12 +18,21 @@ use constant { ], DEFAULT_CONTENT_TYPE => "application/json", - ROWS_PER_PAGE => 20, }; my $TAG_NAME_RE = qr/^[0-9]*[A-Z_a-z][0-9A-Z_a-z]*$/; my $NUMERIC_ID_RE = qr/^[1-9][0-9]*$/; +my $MEDIA_SEARCH_OPTS = { + order_by => { -desc => "upload_date" }, + rows => 20, +}; +my $TAGS_COUNT_VIEW_SEARCH_OPTS = { + order_by => { -desc => "count" }, + rows => 100, +}; + + sub mkpager ($) { my $pager = $_[0]->pager; @@ -41,13 +50,21 @@ get "/meta" => sub { }; get "/tags" => sub { + my $page = query_parameters->get("page") // 1; + send_error("Invalid page number", 400) if $page !~ $NUMERIC_ID_RE; + + my $paged_tags = schema("default")->resultset("TagsCountView") + ->search({}, $TAGS_COUNT_VIEW_SEARCH_OPTS)->page($page); my @tags = map +( { id => $_->tag_id, name => $_->name, count => $_->count, - } ), schema("default")->resultset("TagsCountView")->all; + } ), $paged_tags->all; - return \@tags; + return { + pager => mkpager($paged_tags), + tags => \@tags, + } }; post "/tags" => sub { @@ -94,8 +111,7 @@ get "/media" => sub { send_error("Invalid page number", 400) if $page !~ $NUMERIC_ID_RE; my $paged_media = schema("default")->resultset("Media") - ->search({}, { order_by => "upload_date", rows => ROWS_PER_PAGE }) - ->page($page); + ->search({}, $MEDIA_SEARCH_OPTS)->page($page); my @media = map { $_->media_id } $paged_media->all;