api: implement /media/tags/:media_id

This commit is contained in:
Lucas 2023-03-26 16:40:45 +00:00
parent ed8f8306b9
commit 945770eb1b
3 changed files with 23 additions and 6 deletions

View File

@ -32,6 +32,7 @@ sub startup ($self)
$r->get("/media")->to("media#list")->name("media_list"); $r->get("/media")->to("media#list")->name("media_list");
$r->get("/media/:media_id")->to("media#show")->name("media_show"); $r->get("/media/:media_id")->to("media#show")->name("media_show");
$r->get("/media/tags/:media_id")->to("media#tags")->name("media_tags");
$r->get("/random/tag")->to("random#tag")->name("tag_random"); $r->get("/random/tag")->to("random#tag")->name("tag_random");
$r->get("/random/media")->to("random#media")->name("media_random"); $r->get("/random/media")->to("random#media")->name("media_random");

View File

@ -41,6 +41,25 @@ sub list ($self)
}; };
sub show ($self) sub show ($self)
{
my $media_id = $self->stash("media_id");
my $media = $self->schema->resultset("Media")
->single({ media_id => $media_id });
return $self->render(
json => {error => "Media not found"},
status => 404,
) if !defined($media);
return $self->render(json => {
id => $media->media_id,
name => $media->filename,
download => $media->seaweedfs_fid,
content_type => $media->content_type,
});
};
sub tags ($self)
{ {
my $v = $self->validation; my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1; my $page = $v->optional("page")->num(1, undef)->param // 1;
@ -65,10 +84,6 @@ sub show ($self)
my @tags = map { $_->tag_name } $paged_media_tags->all; my @tags = map { $_->tag_name } $paged_media_tags->all;
return $self->render(json => { return $self->render(json => {
id => $media->media_id,
name => $media->filename,
download => $media->seaweedfs_fid,
content_type => $media->content_type,
tags => [ @tags ], tags => [ @tags ],
pager => { $self->pager($paged_media_tags->pager) }, pager => { $self->pager($paged_media_tags->pager) },
}); });

View File

@ -13,8 +13,9 @@ sub index ($self)
{ path => "/tags", verb => "GET" }, { path => "/tags", verb => "GET" },
{ path => "/tag/:tag_id_or_name", verb => "GET" }, { path => "/tag/:tag_id_or_name", verb => "GET" },
{ path => "/media", verb => "GET" }, { path => "/media", verb => "GET" },
{ path => "/media/:media_id", verb => "GET" }, { path => "/media/:media_id", verb => "GET" },
{ path => "/media/tags/:media_id", verb => "GET" },
{ path => "/random/media", verb => "GET" }, { path => "/random/media", verb => "GET" },
{ path => "/random/tag", verb => "GET" }, { path => "/random/tag", verb => "GET" },