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

View File

@ -51,19 +51,22 @@ get "/tags" => sub {
}; };
post "/tags" => sub { post "/tags" => sub {
my $tag_name = body_parameters->get("name"); my @tag_names = body_parameters->get_all("name");
send_error("Invalid tag name", 404) if $tag_name !~ $TAG_NAME_RE; 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 { eval {
$tag = schema("default")->resultset("Tag") @tags = map +( {
->create({ name => $tag_name }); id => $_->tag_id,
name => $_->name,
} ), schema("default")->resultset("Tag")
->populate([map +({ name => $_ }), @tag_names]);
} or send_error("Tag exists", 409); } or send_error("Tag exists", 409);
return { return \@tags;
id => $tag->tag_id,
name => $tag->name,
};
}; };
get "/tag/:tag_id_or_name" => sub { get "/tag/:tag_id_or_name" => sub {