Add palceholder post and update db init
This commit is contained in:
parent
a8e3b30668
commit
fc04245162
3 changed files with 56 additions and 4 deletions
5
blog
5
blog
|
@ -2,6 +2,7 @@
|
|||
use v6.e.PREVIEW;
|
||||
|
||||
use DB;
|
||||
use DB::BlogMeta;
|
||||
use DB::MarkdownPost;
|
||||
|
||||
my %*SUB-MAIN-OPTS =
|
||||
|
@ -45,9 +46,9 @@ multi MAIN(
|
|||
print "Tagline: ";
|
||||
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 {
|
||||
$db-file.spurt: $db.to-json, :create-only;
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
32
lib/DB/PlaceholderPost.rakumod
Normal file
32
lib/DB/PlaceholderPost.rakumod
Normal 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
|
||||
}
|
Loading…
Add table
Reference in a new issue