Working new post

This commit is contained in:
Nathan McCarty 2025-01-22 04:29:44 -05:00
parent 707b55d761
commit 0bd8a986e3
2 changed files with 84 additions and 13 deletions

46
blog
View file

@ -21,17 +21,23 @@ sub load-db(IO::Path $file --> DB::PostDB:D) {
}
}
#| Write the databse to the provided file
sub write-db(IO::Path $file, DB::PostDB $db) {
my $output = $db.to-json;
$file.spurt: $output;
}
#| Initalize the database
multi MAIN(
"db",
"init",
#| The path of the database file
IO::Path :$file = $blog-dir.add("db.json"),
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
#| Overwrite an already existing database file
Bool :$force
) {
die "Database file already exists, use --force to overwrite: {$file.Str}"
if $file.e && !$force;
die "Database file already exists, use --force to overwrite: {$db-file.Str}"
if $db-file.e && !$force;
print "Blog Title: ";
my $title = get;
@ -43,9 +49,9 @@ multi MAIN(
my $db = DB::PostDB.new: meta => $meta;
if $force {
$file.spurt: $db.to-json, :create-only;
$db-file.spurt: $db.to-json, :create-only;
} else {
$file.spurt: $db.to-json;
$db-file.spurt: $db.to-json;
}
}
@ -54,8 +60,34 @@ multi MAIN(
"db",
"verify",
#| The path of the database file
IO::Path :$file = $blog-dir.add("db.json"),
IO::Path(Str) :$db = $blog-dir.add("db.json"),
) {
load-db $file;
load-db $db;
say "Database OK";
}
#| Create a new markdown post
multi MAIN(
"new",
"markdown",
#| The path to the markdown file
IO::Path(Str) $source,
#| The path of the database file
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
#| The date/time the post should be recorded as posted at
DateTime(Str) :$posted-at = DateTime.now,
#| Should the post be hidden from the archive?
Bool :$hidden = False,
) {
my $db = load-db $db-file;
my $id =
$db.insert-post:
DB::MarkdownPost.new(
source => $source.absolute.IO,
posted-at => $posted-at,
hidden => $hidden,
);
write-db $db-file, $db;
say 'Post inserted with id ', $id;
}