| 1 |
package [d2% appname %2d]; |
| 2 |
use Dancer2; |
| 3 |
use Dancer2::Plugin::DBIx::Class; |
| 4 |
use Dancer2::Plugin::Auth::Tiny; |
| 5 |
use Dancer2::Plugin::CryptPassphrase; |
| 6 |
use Feature::Compat::Try; |
| 7 |
|
| 8 |
get '/' => sub { |
| 9 |
# Give us the most recent first |
| 10 |
my @entries = resultset('Entry')->search( |
| 11 |
{}, |
| 12 |
{ order_by => { -desc => 'created_at' } }, |
| 13 |
)->all; |
| 14 |
template 'index', { entries => \@entries }; |
| 15 |
}; |
| 16 |
|
| 17 |
get '/entry/:id[Int]' => sub { |
| 18 |
my $id = route_parameters->get('id'); |
| 19 |
my $entry = resultset( 'Entry' )->find( $id ); |
| 20 |
template 'entry', { entry => $entry }; |
| 21 |
}; |
| 22 |
|
| 23 |
get '/create' => needs login => sub { |
| 24 |
# Vars are passed to templates automatically |
| 25 |
template 'create_update', { post_to => uri_for '/create' }; |
| 26 |
}; |
| 27 |
|
| 28 |
post '/create' => needs login => sub { |
| 29 |
my $params = body_parameters(); |
| 30 |
var $_ => $params->{ $_ } foreach qw< title summary content >; |
| 31 |
|
| 32 |
my @missing = grep { $params->{$_} eq '' } qw< title summary content >; |
| 33 |
if( @missing ) { |
| 34 |
var missing => join ",", @missing; |
| 35 |
warning "Missing parameters: " . var 'missing'; |
| 36 |
forward '/create', {}, { method => 'GET' }; |
| 37 |
} |
| 38 |
|
| 39 |
my $entry = do { |
| 40 |
try { |
| 41 |
resultset('Entry')->create( $params->as_hashref ); |
| 42 |
} |
| 43 |
catch( $e ) { |
| 44 |
error "Database error: $e"; |
| 45 |
var error_message => 'A database error occurred; your entry could not be created', |
| 46 |
forward '/create', {}, { method => 'GET' }; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
debug 'Created entry ' . $entry->id . ' for "' . $entry->title . '"'; |
| 51 |
redirect uri_for "/entry/" . $entry->id; # redirect does not need a return |
| 52 |
}; |
| 53 |
|
| 54 |
get '/update/:id[Int]' => needs login => sub { |
| 55 |
my $id = route_parameters->get('id'); |
| 56 |
my $entry = resultset( 'Entry' )->find( $id ); |
| 57 |
var $_ => $entry->$_ foreach qw< title summary content >; |
| 58 |
template 'create_update', { post_to => uri_for "/update/$id" }; |
| 59 |
}; |
| 60 |
|
| 61 |
post '/update/:id[Int]' => needs login => sub { |
| 62 |
my $id = route_parameters->get('id'); |
| 63 |
my $entry = resultset( 'Entry' )->find( $id ); |
| 64 |
if( !$entry ) { |
| 65 |
status 'not_found'; |
| 66 |
return "Attempt to update non-existent entry $id"; |
| 67 |
} |
| 68 |
|
| 69 |
my $params = body_parameters(); |
| 70 |
var $_ => $params->{ $_ } foreach qw< title summary content >; |
| 71 |
my @missing = grep { $params->{$_} eq '' } qw< title summary content >; |
| 72 |
if( @missing ) { |
| 73 |
var missing => join ",", @missing; |
| 74 |
warning "Missing parameters: " . var 'missing'; |
| 75 |
forward "/update/$id", {}, { method => 'GET' }; |
| 76 |
} |
| 77 |
|
| 78 |
try { |
| 79 |
$entry->update( $params->as_hashref ); |
| 80 |
} |
| 81 |
catch( $e ) { |
| 82 |
error "Database error: $e"; |
| 83 |
var error_message => 'A database error occurred; your entry could not be updated', |
| 84 |
forward "/update/$id", {}, { method => 'GET' }; |
| 85 |
} |
| 86 |
|
| 87 |
debug 'Updated entry ' . $entry->id . ' for "' . $entry->title . '"'; |
| 88 |
redirect uri_for "/entry/" . $entry->id; # redirect does not need a return |
| 89 |
}; |
| 90 |
|
| 91 |
get '/delete/:id[Int]' => needs login => sub { |
| 92 |
my $id = route_parameters->get('id'); |
| 93 |
my $entry = resultset( 'Entry' )->find( $id ); |
| 94 |
template 'delete', { entry => $entry }; |
| 95 |
}; |
| 96 |
|
| 97 |
post '/delete/:id[Int]' => needs login => sub { |
| 98 |
my $id = route_parameters->get('id'); |
| 99 |
my $entry = resultset( 'Entry' )->find( $id ); |
| 100 |
if( !$entry ) { |
| 101 |
status 'not_found'; |
| 102 |
return "Attempt to delete non-existent entry $id"; |
| 103 |
} |
| 104 |
|
| 105 |
# Always default to not destroying data |
| 106 |
my $delete_it = body_parameters->get('delete_it') // 0; |
| 107 |
|
| 108 |
if( $delete_it ) { |
| 109 |
$entry->delete; |
| 110 |
redirect uri_for "/"; |
| 111 |
} else { |
| 112 |
# Display our entry again |
| 113 |
redirect uri_for "/entry/$id"; |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
get '/login' => sub { |
| 118 |
template 'login' => { return_url => params->{ return_url } }; |
| 119 |
}; |
| 120 |
|
| 121 |
post '/login' => sub { |
| 122 |
my $username = body_parameters->get('username'); |
| 123 |
my $password = body_parameters->get('password'); |
| 124 |
my $return_url = body_parameters->get('return_url'); |
| 125 |
|
| 126 |
my $user = resultset( 'User' )->find({ username => $username }); |
| 127 |
if ( $user and verify_password( $password, $user->password) ) { |
| 128 |
app->change_session_id; |
| 129 |
session user => $username; |
| 130 |
info "$username successfully logged in"; |
| 131 |
return redirect $return_url || '/'; |
| 132 |
} |
| 133 |
else { |
| 134 |
warning "Failed login attempt for $username"; |
| 135 |
template 'login' => { login_error => 1, return_url => $return_url }; |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
get '/logout' => sub { |
| 140 |
app->destroy_session; |
| 141 |
redirect uri_for "/"; |
| 142 |
}; |
| 143 |
|
| 144 |
true; |
| 145 |
|