Created
June 8, 2011 16:36
-
-
Save wchristian/1014777 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package TFX::Crontabs; | |
use Dancer qw( get post template params redirect forward ); | |
use Dancer::Plugin::Database 'database'; | |
our $VERSION = '0.1'; | |
my $table = 'cronjobs'; | |
get '/' => sub { | |
my @cronjobs = database->quick_select( $table, {} ); | |
@cronjobs = sort { $a->{sort} <=> $b->{sort} } @cronjobs; | |
my %data_types; | |
push @{ $data_types{ $_->{data_type} } }, $_ for @cronjobs; | |
my @data_types = map { { data_type => $_, jobs => $data_types{$_}, max_sort => 0 } } sort keys %data_types; | |
for my $type ( @data_types ) { | |
for my $job ( @{$type->{jobs}} ) { | |
next if $job->{sort} <= $type->{max_sort}; | |
$type->{max_sort} = $job->{sort}; | |
} | |
} | |
template 'list', { cronjobs => \@data_types }; | |
}; | |
get '/edit/:id' => sub { | |
my $job = database->quick_select( $table, { id => params->{id} } ); | |
template 'edit', { job => $job, mode => 'add' }; | |
}; | |
post '/edit/:id' => sub { | |
my %changes = map { $_ => params->{$_} } qw( data_type min hour day month weekday command sort active crontab_comment comment ); | |
database->quick_update( $table, { id => params->{id} }, \%changes ); | |
redirect '/edit/' . params->{id}; | |
}; | |
get '/delete/:id' => sub { | |
database->quick_delete($table, { id => params->{id} }); | |
redirect '/'; | |
}; | |
get '/add/:data_type/:sort' => sub { forward '/add' }; | |
get '/add/:data_type' => sub { forward '/add' }; | |
get '/add' => sub { | |
my %job = ( data_type => params->{data_type} || '', sort => params->{sort} || '', active => 1, min => 0, hour => 0, day => '*', month => '*', weekday => '2' ); | |
template 'edit', { job => \%job, mode => 'add' }; | |
}; | |
post '/add' => sub { | |
my %changes = map { $_ => params->{$_} } qw( data_type min hour day month weekday command sort active crontab_comment comment ); | |
database->quick_insert( $table, \%changes ); | |
redirect '/'; | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment