diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 540f1a2..9db42b9 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -30,6 +30,8 @@ sub startup { }); my $config = $self->config; + push @{ $self->commands->namespaces }, __PACKAGE__ . '::Command'; + $self->plugin('Pear::LocalLoop::Plugin::Validators'); $self->plugin('Authentication' => { diff --git a/lib/Pear/LocalLoop/Command/leaderboard.pm b/lib/Pear/LocalLoop/Command/leaderboard.pm new file mode 100644 index 0000000..ba8f1c7 --- /dev/null +++ b/lib/Pear/LocalLoop/Command/leaderboard.pm @@ -0,0 +1,73 @@ +package Pear::LocalLoop::Command::leaderboard; +use Mojo::Base 'Mojolicious::Command'; + +use Mojo::Util 'getopt'; + +has description => 'Build leaderboards'; + +has usage => sub { shift->extract_usage }; + +sub run { + my ( $self, @args ) = @_; + + getopt \@args, + 't|type=s' => \my $type, + 'l|list' => \my $list, + 'd|date=s' => \my $date; + + my $leaderboard_rs = $self->app->schema->resultset('Leaderboard'); + + if ( defined $list ) { + say sprintf('%20s : %20s', 'Type', 'Name'); + for my $leaderboard ( $leaderboard_rs->all ) { + say sprintf('%20s : %20s', $leaderboard->type, $leaderboard->name); + } + return; + } + + if ( defined $type ) { + my $leaderboard = $leaderboard_rs->find({ type => $type }); + + unless ( defined $leaderboard ) { + say "Unknown Leaderboard Type"; + return; + } + + if ( defined $date ) { + say "Creating leaderboard of type $type with date $date"; + + my $date_formatter = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%d' + ); + + my $datetime = $date_formatter->parse_datetime($date); + + unless ( defined $datetime ) { + say "Unrecognised date format, please use 'YYYY-MM-DD' Format"; + } + + $leaderboard->create_new($datetime); + + say "Done"; + } else { + say 'Leaderboards of type ' . $type . ' available:'; + for my $set ( $leaderboard->sets->all ) { + say $set->date; + } + } + } +} + +=head1 SYNOPSIS + + Usage: APPLICATION leaderboard [OPTIONS] + + Options: + + -l, --list List all leaderboard types + -t, --type Leaderboard type to create + -d, --date Start Date (in YYYY-MM-DD format) + +=cut + +1;