website/lib/DB.rakumod

119 lines
2.9 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-21 22:08:10 -05:00
#| Top level metadata for the blog
class BlogMeta is json(:pretty) {
#| The title of the blog
has Str:D $.title is required;
#| The tagline of the blog
has Str:D $.tagline is required;
}
2025-01-21 01:31:33 -05:00
#| Shared post meta-data
2025-01-21 03:24:01 -05:00
role Post is json {
2025-01-21 01:31:33 -05:00
#| The location of the source file for the post
2025-01-22 04:29:44 -05:00
has
IO::Path:D $.source
is required
is json(
:to-json(*.Str),
:from-json(*.IO)
);
2025-01-21 01:31:33 -05:00
#| The time to display for the creation of the post
2025-01-22 04:29:44 -05:00
has
DateTime:D $.posted-at
is required
is json(
:to-json(*.Str),
:from-json(*.DateTime)
);
2025-01-21 01:31:33 -05:00
#| An optional list of edit times for the post
2025-01-22 04:29:44 -05:00
has
DateTime:D @.edited-at
is json(
:to-json( )
value => { $^value.Str }
),
:from-json( )
value => { $^value.DateTime }
)
)
= [];
2025-01-21 21:58:10 -05:00
#| An optional list of extra slugs to use for this post
2025-01-22 04:29:44 -05:00
has Str:D @.slugs is json = [];
2025-01-21 22:08:10 -05:00
#| Should the post be hidden from the main list
2025-01-22 04:29:44 -05:00
has Bool:D $.hidden is json is rw = False;
2025-01-21 01:31:33 -05:00
#| Get the title for this post, intended to be extracted from whatever
#| document produced it
method title(--> Str:D) {...}
2025-01-21 21:58:10 -05:00
#| Get the list of slugs for this post, including ones auto generated from
#| the title, as well as any additional slugs
2025-01-21 22:08:10 -05:00
method all-slugs(--> Array[Str:D]) {
2025-01-21 21:58:10 -05:00
my $long-title-slug = self.title.lc.subst: /\h*/, '-';
2025-01-21 22:08:10 -05:00
return [$long-title-slug, @!slugs].flat.Array;
2025-01-21 21:58:10 -05:00
}
2025-01-22 04:29:44 -05:00
2025-01-21 01:31:33 -05:00
}
#| A plain markdown post
2025-01-21 03:24:01 -05:00
class MarkdownPost does Post is json {
#| Marker for disambiguation between post types in json representation, the
#| cheaty way
has Bool:D $.markdown = True;
2025-01-21 01:31:33 -05:00
method title(--> Str:D) {
markdown-title($!source)
}
}
2025-01-21 03:24:01 -05:00
2025-01-22 05:00:58 -05:00
#| 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;
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;
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
}
2025-01-21 03:24:01 -05:00
}