api/v0: improve error conditions

Return an HTTP 404 when no media is found and an HTTP 400 when some of
the requested tags aren't found.
This commit is contained in:
Lucas Gabriel Vuotto 2025-05-03 17:05:11 +00:00
parent fa6f642ca2
commit 9d8b300d11

View file

@ -20,6 +20,10 @@ sub _list_with_tags ($self, $page, @tags)
my $paged_media_ids = $self->media_model->with_all_tags(@tags)
->page($page);
my @media_ids = map {$_->{media_id}} $paged_media_ids->fetchall->@*;
return $self->render(
json => {error => "No matching media found."},
status => 404,
) if @media_ids == 0;
return $self->render(json => {
media => $self->media_model->get(@media_ids)->page($page)
@ -55,6 +59,11 @@ sub list ($self)
@tag_ids = map {$_->{id}} $self->tags_model
->get(display => [@tags])->fetchall->@* if @tags > 0;
return $self->render(
json => {error => "Some of the requested tags don't exist."},
status => 400,
) if @tag_ids != @tags && @tags > 0;
return @tags == 0 ?
$self->_list_no_tags($page) :
$self->_list_with_tags($page, @tag_ids);