Multitype posts in db
This commit is contained in:
parent
0bd8a986e3
commit
8648ff038d
1 changed files with 21 additions and 9 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue