website/lib/DB.rakumod

64 lines
1.3 KiB
Raku
Raw Normal View History

2025-01-21 03:24:01 -05:00
use v6.e.PREVIEW;
2025-01-21 01:31:33 -05:00
#| Post database
unit module DB;
use Pandoc;
2025-01-21 03:24:01 -05:00
use JSON::Class:auth<zef:vrurg>;
2025-01-21 01:31:33 -05:00
2025-01-22 20:13:45 -05:00
use DB::Post;
use DB::BlogMeta;
2025-01-22 20:49:27 -05:00
use DB::MarkdownPost;
2025-01-22 20:53:52 -05:00
use DB::IdrisPost;
use DB::PlaceholderPost;
2025-01-22 05:00:58 -05:00
class Posts is json(
:dictionary( )
:keyof(Int:D),
MarkdownPost:D,
IdrisPost:D,
PlaceholderPost:D,
)) {}
2025-01-22 05:00:58 -05:00
subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D;
2025-01-21 03:24:01 -05:00
#| The top level posts database
class PostDB is json(:pretty) {
2025-01-21 22:08:10 -05:00
#| The metadat for the blog
has BlogMeta:D $.meta is required;
#| A mapping from post ids to posts
2025-01-22 05:00:58 -05:00
has %.posts is Posts;
#| The post id to use for placeholder posts
has Int $.placeholder-id = 0;
2025-01-22 05:00:58 -05:00
method TWEAK() {
%!posts := Posts.new unless %!posts;
}
2025-01-22 04:29:44 -05:00
#| Get the next unused post ID
method next-post-id(--> Int) {
2025-01-22 05:00:58 -05:00
if %!posts.elems > 0 {
2025-01-22 04:29:44 -05:00
%!posts.keys.max + 1
} else {
0
}
}
#| Insert a new post to the DB, returning its id
2025-01-22 05:00:58 -05:00
method insert-post(PostTypes $post --> Int) {
2025-01-22 04:29:44 -05:00
my $id = self.next-post-id;
%!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,
)
}
2025-01-21 03:24:01 -05:00
}