api: rename /tags/new to just /tags and validate name

This commit is contained in:
Lucas 2023-02-19 09:54:52 +00:00
parent 420fc9ce97
commit 3a41ea6d0b

View File

@ -10,7 +10,7 @@ use constant {
ROUTES => [ ROUTES => [
{ path => "/meta", verb => "GET" }, { path => "/meta", verb => "GET" },
{ path => "/tags", verb => "GET" }, { path => "/tags", verb => "GET" },
{ path => "/tags/new", verb => "POST" }, { path => "/tags", verb => "POST" },
{ path => "/tag/:tag_id_or_name", verb => "GET" }, { path => "/tag/:tag_id_or_name", verb => "GET" },
{ path => "/media/:media_id", verb => "GET" }, { path => "/media/:media_id", verb => "GET" },
{ path => "/download/:media_id", verb => "GET" }, { path => "/download/:media_id", verb => "GET" },
@ -21,6 +21,8 @@ use constant {
ROWS_PER_PAGE => 20, ROWS_PER_PAGE => 20,
}; };
my $TAG_NAME_RE = qr/^[0-9]*[A-Z_a-z][0-9A-Z_a-z]*$/;
sub mkpager ($) sub mkpager ($)
{ {
my $pager = $_[0]->pager; my $pager = $_[0]->pager;
@ -47,12 +49,14 @@ get "/tags" => sub {
return \@tags; return \@tags;
}; };
post "/tags/new" => sub { post "/tags" => sub {
my $tag; my $tag_name = body_parameters->get("name");
send_error("Invalid tag name", 404) if $tag_name !~ $TAG_NAME_RE;
my $tag;
eval { eval {
$tag = schema("default")->resultset("Tag") $tag = schema("default")->resultset("Tag")
->create({ name => body_parameters->get("name") }); ->create({ name => $tag_name });
} or send_error("Tag exists", 409); } or send_error("Tag exists", 409);
return { return {