1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
package Blog::Controller::Posts;
use Mojo::Base 'Mojolicious::Controller';
sub create { shift->render(post => {}) }
sub edit {
my $self = shift;
$self->render(post => $self->posts->find($self->param('id')));
}
sub index {
my $self = shift;
$self->render(posts => $self->posts->all);
}
sub remove {
my $self = shift;
$self->posts->remove($self->param('id'));
$self->redirect_to('posts');
}
sub show {
my $self = shift;
$self->render(post => $self->posts->find($self->param('id')));
}
sub store {
my $self = shift;
my $validation = $self->_validation;
return $self->render(action => 'create', post => {})
if $validation->has_error;
my $id = $self->posts->add($validation->output);
$self->redirect_to('show_post', id => $id);
}
sub update {
my $self = shift;
my $validation = $self->_validation;
return $self->render(action => 'edit', post => {}) if $validation->has_error;
my $id = $self->param('id');
$self->posts->save($id, $validation->output);
$self->redirect_to('show_post', id => $id);
}
sub _validation {
my $self = shift;
my $validation = $self->validation;
$validation->required('title', 'not_empty');
$validation->required('body', 'not_empty');
return $validation;
}
1;
|