use v6.d;
use IRC::Client;
use Red:api<2>;
use Red::AST::Function;
model Boha::KarmaEvent is table<karma_event> {
has Int $.id is serial;
has DateTime $.created is column .= now;
has Str $.nick is column;
has Int $.delta is column;
method current-karma-for(::?CLASS:U: Str $nick) {
self.^all.grep({ .nick eq $nick }).map(
{Red::AST::Function.new(:func<SUM>, :args(.delta), :returns(Int)) }
);
}
method current-karma(::?CLASS:U: Int :$top) {
my $rs = self.^all
.map(
{
.nick,
Red::AST::Function.new(:func<SUM>, :args(.delta), :returns(Int))
}
)
.sort({ -1 * .^columns[1].column})
;
$rs.group = self.nick;
return $top ?? $rs.head($top) !! $rs;
}
method new-karma(::?CLASS:U: Str $nick, Int $delta) {
self.^create(:$nick,:$delta);
return;
}
}