# step one:
# set up a base method to make sure your item exists, and
# store your item information in $c->stash
sub base : Chained('/') PathPart('item') CaptureArgs(1) {
my ($self, $c, $itemid) = @_;
my $item = $c->model('Item')->find($itemid);
if ( $item == undef ) {
$c->stash( error_msg => "This item does not exist" );
$c->detach("nonexistent");
} else {
$c->stash( item => $item );
}
}
####
sub base : Chained('/') PathPart('item') CaptureArgs(1)
##
##
## the next part is to link the end point to the chain
## thisis done by passing the name of the base chain method to Chained for this method
sub view : Chained('base') PathPart('view') Args(0) {
my ($self, $c) = @_;
my $item = $c->stash->{item};
$c->stash(
template => 'item/view.tt2',
item => $item,
);
}
##
##
sub blog : Chained('/') PathPart('blog') CaptureArgs(0) {
}
sub load_blog : Chained('blog') PathPart('') CaptureArgs(1) {
my ($self, $c, $id) = @_;
my $entry = $c->model('DB::Blog')->find($id);
if ( $entry ) {
$c->stash(entry => $entry );
} else {
$c->stash( error_msg => "No such blog" );
$c->detach;
}
}
sub view : Chained('load_blog') PathPart('view') Args(0) {
my ($self, $c) = @_;
}
sub create : Chained('blog') PathPart('new') Args(0) FormConfig('blogs/create.yml') {
my ($self, $c) = @_;
my $form = $c->stash->{form};
if ( $form->submitted_and_valid ) {
my $entry = $c->model('DB::Blogs')->new_result({ created => DateTime->now });
$form->model->update($entry);
$c->stash(
status_msg => "Post saved",
entry => $entry
);
} else {
$c->stash( error_msg => "Your form had errors" );
$c->detach;
}
}