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

View file

@ -10,10 +10,17 @@ use DB::Post;
use DB::BlogMeta;
use DB::MarkdownPost;
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
class PostDB is json(:pretty) {
@ -21,6 +28,8 @@ class PostDB is json(:pretty) {
has BlogMeta:D $.meta is required;
#| A mapping from post ids to posts
has %.posts is Posts;
#| The post id to use for placeholder posts
has Int $.placeholder-id = 0;
method TWEAK() {
%!posts := Posts.new unless %!posts;
@ -41,4 +50,14 @@ class PostDB is json(:pretty) {
%!posts{$id} = $post;
$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,
)
}
}