Add palceholder post and update db init

This commit is contained in:
Nathan McCarty 2025-01-22 21:26:25 -05:00
parent a8e3b30668
commit fc04245162
3 changed files with 56 additions and 4 deletions

5
blog
View file

@ -2,6 +2,7 @@
use v6.e.PREVIEW; use v6.e.PREVIEW;
use DB; use DB;
use DB::BlogMeta;
use DB::MarkdownPost; use DB::MarkdownPost;
my %*SUB-MAIN-OPTS = my %*SUB-MAIN-OPTS =
@ -45,9 +46,9 @@ multi MAIN(
print "Tagline: "; print "Tagline: ";
my $tagline = get; my $tagline = get;
my $meta = DB::BlogMeta.new: title => $title, tagline => $tagline; my $meta = BlogMeta.new: title => $title, tagline => $tagline;
my $db = DB::PostDB.new: meta => $meta; my $db = DB::PostDB.init: $meta;
if $force { if $force {
$db-file.spurt: $db.to-json, :create-only; $db-file.spurt: $db.to-json, :create-only;

View file

@ -10,10 +10,17 @@ use DB::Post;
use DB::BlogMeta; use DB::BlogMeta;
use DB::MarkdownPost; use DB::MarkdownPost;
use DB::IdrisPost; use DB::IdrisPost;
use DB::PlaceholderPost;
class Posts is json(:dictionary(:keyof(Int:D), MarkdownPost:D, IdrisPost:D)) {} class Posts is json(
:dictionary()
:keyof(Int:D),
MarkdownPost:D,
IdrisPost:D,
PlaceholderPost:D,
)) {}
subset PostTypes where MarkdownPost:D | IdrisPost:D; subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D;
#| The top level posts database #| The top level posts database
class PostDB is json(:pretty) { class PostDB is json(:pretty) {
@ -21,6 +28,8 @@ class PostDB is json(:pretty) {
has BlogMeta:D $.meta is required; has BlogMeta:D $.meta is required;
#| A mapping from post ids to posts #| A mapping from post ids to posts
has %.posts is Posts; has %.posts is Posts;
#| The post id to use for placeholder posts
has Int $.placeholder-id = 0;
method TWEAK() { method TWEAK() {
%!posts := Posts.new unless %!posts; %!posts := Posts.new unless %!posts;
@ -41,4 +50,14 @@ class PostDB is json(:pretty) {
%!posts{$id} = $post; %!posts{$id} = $post;
$id $id
} }
#| Initialize a new database
method init(BlogMeta:D $meta --> PostDB:D) {
my %posts is Posts = Posts.new;
%posts{0} = PlaceholderPost.empty;
PostDB.new(
meta => $meta,
posts => %posts,
)
}
} }

View file

@ -0,0 +1,32 @@
use v6.e.PREVIEW
use Pandoc;
use JSON::Class:auth<zef:vrurg>;
use DB::Post;
#| An empty placeholder post
unit class PlaceholderPost does Post is json;
#| Marker for disambiguation between post types in json representation, the
#| cheaty way
has Bool:D $.placeholder = True;
method title(--> Str:D) {
"Placeholder Article"
}
#| Shortcut for creating an empty placeholder post
method empty(--> PlaceholderPost:D) {
PlaceholderPost.new(
source => "/dev/null".IO,
posted-at => DateTime.now,
hidden => True,
)
}
# Override the all-slugs method to return an empty array, we don't want the
# placeholder article to be refered to as anything but its id
method all-slugs(--> Array[Str:D]) {
Array[Str:D].new
}