api: return tags for /media/:media_id

This commit is contained in:
Lucas 2023-03-17 19:58:12 +00:00
parent 37a2806f03
commit aa5e4ffe29
2 changed files with 24 additions and 0 deletions

View File

@ -9,6 +9,13 @@ my $MEDIA_SEARCH_OPTS = {
order_by => { -desc => "media_id" },
rows => 5,
};
my $TAGS_SEARCH_OPTS = {
rows => 5,
};
my $TAGS_COUNT_VIEW_SEARCH_OPTS = {
order_by => { -desc => "count" },
rows => 5,
};
sub list ($self)
{
@ -35,6 +42,14 @@ sub list ($self)
sub show ($self)
{
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(
json => {error => "Invalid page number"},
status => 400,
) if $v->has_error;
my $media_id = $self->stash("media_id");
my $media = $self->schema->resultset("Media")
->single({ media_id => $media_id });
@ -44,11 +59,17 @@ sub show ($self)
status => 404,
) if !defined($media);
my $paged_media_tags = $self->schema->resultset("TaggedMediaView")
->search({ media_id => $media_id }, { $TAGS_SEARCH_OPTS->%* })
->page($page);
my @tags = map { $_->tag_name } $paged_media_tags->all;
return $self->render(json => {
id => $media->media_id,
name => $media->filename,
download => $media->seaweedfs_fid,
content_type => $media->content_type,
tags => [ @tags ],
});
};

View File

@ -9,6 +9,9 @@ my $MEDIA_SEARCH_OPTS = {
order_by => { -desc => "media_id" },
rows => 5,
};
my $TAGS_SEARCH_OPTS = {
rows => 5,
};
my $TAGS_COUNT_VIEW_SEARCH_OPTS = {
order_by => { -desc => "count" },
rows => 5,