use v6.e.PREVIEW; #| Post database unit module DB; use Pandoc; use JSON::Class:auth; use DB::Post; use DB::BlogMeta; use DB::MarkdownPost; #| 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 is Posts; method TWEAK() { %!posts := Posts.new unless %!posts; } #| Get the next unused post ID method next-post-id(--> Int) { if %!posts.elems > 0 { %!posts.keys.max + 1 } else { 0 } } #| Insert a new post to the DB, returning its id method insert-post(PostTypes $post --> Int) { my $id = self.next-post-id; %!posts{$id} = $post; $id } }