Multitype posts in db

This commit is contained in:
Nathan McCarty 2025-01-22 05:00:58 -05:00
parent 0bd8a986e3
commit 8648ff038d

View file

@ -73,24 +73,36 @@ class MarkdownPost does Post is json {
}
}
# #| A literate idris post in markdown format
# class IdrisPost does Post {
# method title(--> Str:D) {
# markdown-title($!source)
# }
# }
#| A plain markdown post
class IdrisPost does Post is json {
#| Marker for disambiguation between post types in json representation, the
#| cheaty way
has Bool:D $.idris = True;
method title(--> Str:D) {
markdown-title($!source)
}
}
class Posts is json(:dictionary(:keyof(Int:D), MarkdownPost:D, IdrisPost:D)) {}
subset PostTypes where MarkdownPost:D | IdrisPost:D;
#| The top level posts database
class PostDB is json(:pretty) {
#| The metadat for the blog
has BlogMeta:D $.meta is required;
#| A mapping from post ids to posts
has %.posts{Int:D} of MarkdownPost:D = %();
has %.posts is Posts;
method TWEAK() {
%!posts := Posts.new unless %!posts;
}
#| Get the next unused post ID
method next-post-id(--> Int) {
if %!posts {
if %!posts.elems > 0 {
%!posts.keys.max + 1
} else {
0
@ -98,7 +110,7 @@ class PostDB is json(:pretty) {
}
#| Insert a new post to the DB, returning its id
method insert-post(Post:D $post --> Int) {
method insert-post(PostTypes $post --> Int) {
my $id = self.next-post-id;
%!posts{$id} = $post;
$id