package MusicApp::Controller::Album; use Moose; use namespace::autoclean; BEGIN { extends 'Catalyst::Controller' } sub base :Chained('/') :PathPart('album') :CaptureArgs(0) { my ($self, $c) = @_; $c->stash(resultset => $c->model('DB::Album')); } sub list :Chained('base') :PathPart('') :Args(0) { my ($self, $c) = @_; $c->stash(albums => [$c->stash->{resultset}->all]); } sub create :Chained('base') :PathPart('create') :Args(0) { my ($self, $c) = @_; if ($c->req->method eq 'POST') { my $album = $c->stash->{resultset}->create({ title => $c->req->params->{title}, artist => $c->req->params->{artist}, }); $c->response->redirect($c->uri_for($self->action_for('list'))); } } sub edit :Chained('base') :PathPart('edit') :Args(1) { my ($self, $c, $id) = @_; my $album = $c->stash->{resultset}->find($id); if ($c->req->method eq 'POST') { $album->update({ title => $c->req->params->{title}, artist => $c->req->params->{artist}, }); $c->response->redirect($c->uri_for($self->action_for('list'))); } $c->stash(album => $album); } sub delete :Chained('base') :PathPart('delete') :Args(1) { my ($self, $c, $id) = @_; my $album = $c->stash->{resultset}->find($id); $album->delete; $c->response->redirect($c->uri_for($self->action_for('list'))); } 1;