diff --git a/blog b/blog index 9918182..759f669 100755 --- a/blog +++ b/blog @@ -254,3 +254,31 @@ multi MAIN( } } } + +#| Add or remove a tag to a post +multi MAIN( + "post", + "tag", + #| The id of the post to display information for + Int $id, + #| The tag to add/remove + Str $tag, + #| remove the tag instead of adding it + Bool :$remove, + #| The path of the database directory + IO::Path(Str) :$db-dir = $default-db-dir, +) { + my $db = read-db $db-dir; + my $post = $db.posts{$id.Int}; + if $remove { + die "Post did not have requested tag" + unless $tag ~~ any($post.tags); + my $index = $post.tags.first: $tag; + $post.tags.=grep(* ne $tag); + } else { + die "Post already had requested tag" + if $tag ~~ any($post.tags); + $post.tags.push: $tag; + } + $db.write: $db-dir; +} diff --git a/lib/DB/Post.rakumod b/lib/DB/Post.rakumod index 246753e..c1e98c7 100644 --- a/lib/DB/Post.rakumod +++ b/lib/DB/Post.rakumod @@ -37,7 +37,7 @@ DateTime:D @.edited-at #| An optional list of extra slugs to use for this post has Str:D @.slugs is json = []; #| An optional list of tags for this post -has Str:D @.tags is json = []; +has Str:D @.tags is rw is json = []; #| Should the post be hidden from the main list has Bool:D $.hidden is json is rw = False;