api: allow creating multiple tags in one call

This commit is contained in:
Lucas 2023-02-19 10:28:22 +00:00
parent af1cac6d3f
commit 401202037f
1 changed files with 12 additions and 9 deletions

View File

@ -51,19 +51,22 @@ get "/tags" => sub {
};
post "/tags" => sub {
my $tag_name = body_parameters->get("name");
send_error("Invalid tag name", 404) if $tag_name !~ $TAG_NAME_RE;
my @tag_names = body_parameters->get_all("name");
send_error("No tags provided", 400) if @tag_names == 0;
send_error("Too many tags provided", 400) if @tag_names > 100;
send_error("Invalid tag names", 400) if
grep { $_ !~ $TAG_NAME_RE } @tag_names;
my $tag;
my @tags;
eval {
$tag = schema("default")->resultset("Tag")
->create({ name => $tag_name });
@tags = map +( {
id => $_->tag_id,
name => $_->name,
} ), schema("default")->resultset("Tag")
->populate([map +({ name => $_ }), @tag_names]);
} or send_error("Tag exists", 409);
return {
id => $tag->tag_id,
name => $tag->name,
};
return \@tags;
};
get "/tag/:tag_id_or_name" => sub {