api: page /tags results

While at it, move /media search opts to a variable too.
This commit is contained in:
Lucas 2023-02-19 10:42:15 +00:00
parent 401202037f
commit 7e3fdadfc6

View File

@ -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;