api: rewrite /tag/:tag_id_or_name
Commit 3a41ea6d0b
allows for using a
SELECT WHERE ... OR ... instead of two SELECT WHERE.
Page the media results.
This commit is contained in:
parent
a0d3601c57
commit
978f979d4d
@ -85,21 +85,27 @@ post "/tags" => sub {
|
|||||||
};
|
};
|
||||||
|
|
||||||
get "/tag/:tag_id_or_name" => 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");
|
my $tag_id_or_name = route_parameters->get("tag_id_or_name");
|
||||||
|
|
||||||
$tag_rset = schema("default")->resultset("Tag");
|
my $query = [
|
||||||
$tag = $tag_rset->single({ tag_id => $tag_id_or_name }) //
|
{ tag_id => $tag_id_or_name },
|
||||||
$tag_rset->single({ name => $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);
|
send_error("Tag not found", 404) if !defined($tag);
|
||||||
|
|
||||||
@media = map { $_->media_id } schema("default")->resultset("MediaTag")
|
my $paged_media = schema("default")->resultset("MediaTag")
|
||||||
->search({ tag_id => $tag->tag_id })->all;
|
->search({ tag_id => $tag->tag_id }, $MEDIA_SEARCH_OPTS)
|
||||||
|
->page($page);
|
||||||
|
my @media = map { $_->media_id } $paged_media->all;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id => $tag->tag_id,
|
id => $tag->tag_id,
|
||||||
name => $tag->name,
|
name => $tag->name,
|
||||||
|
pager => mkpager($paged_media),
|
||||||
media => \@media,
|
media => \@media,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -110,7 +116,6 @@ get "/media" => sub {
|
|||||||
|
|
||||||
my $paged_media = schema("default")->resultset("Media")
|
my $paged_media = schema("default")->resultset("Media")
|
||||||
->search({}, $MEDIA_SEARCH_OPTS)->page($page);
|
->search({}, $MEDIA_SEARCH_OPTS)->page($page);
|
||||||
|
|
||||||
my @media = map { $_->media_id } $paged_media->all;
|
my @media = map { $_->media_id } $paged_media->all;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user