use v6.e.PREVIEW;

use JSON::Class:auth<zef:vrurg>;

#| A plain markdown post
unit class Series is json(:pretty);

#| The title of a series
has Str:D $.title is required;

#| The description of a series
has Str:D $.desc is required;

#| The ids of the posts in the series, in series order
has Int:D @.post-ids is rw = [];

#| Returns true if this series contains the given post id
method contains-post(Int:D $post-id --> Bool:D) {
    if $post-id ~~ any @!post-ids {
        True
    } else {
        False
    }
}

#| Returns the date of the lastest post
method latest-post($db) {
    my @posts = @!post-ids.map(-> $i {$db.posts{$i}});
    if @posts {
        my $most-recent-post = @posts.max(*.posted-at);
        $most-recent-post.posted-at
    } else {
        Nil
    }
}