From 401202037fc67be1aeb622ca2d666834321b103c Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 19 Feb 2023 10:28:22 +0000 Subject: [PATCH] api: allow creating multiple tags in one call --- lib/PoorBooru/API/V0.pm | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/PoorBooru/API/V0.pm b/lib/PoorBooru/API/V0.pm index a121a12..1c5d658 100644 --- a/lib/PoorBooru/API/V0.pm +++ b/lib/PoorBooru/API/V0.pm @@ -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 {