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;
####
# First, create the Models
# lib/MusicApp/Schema/Result/Album.pm
package MusicApp::Schema::Result::Album;
use strict;
use warnings;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('albums');
__PACKAGE__->add_columns(
id => {
data_type => 'integer',
is_auto_increment => 1,
is_nullable => 0,
},
title => {
data_type => 'varchar',
size => 128,
is_nullable => 1,
},
artist => {
data_type => 'varchar',
size => 128,
is_nullable => 1,
},
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many(album_songs => 'MusicApp::Schema::Result::AlbumSong', 'album_id');
__PACKAGE__->many_to_many(songs => 'album_songs', 'song');
1;
####
package MusicApp::Schema::Result::Song;
use strict;
use warnings;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('songs');
__PACKAGE__->add_columns(
id => {
data_type => 'integer',
is_auto_increment => 1,
is_nullable => 0,
},
title => {
data_type => 'varchar',
size => 128,
is_nullable => 1,
},
artist => {
data_type => 'varchar',
size => 128,
is_nullable => 1,
},
length => {
data_type => 'varchar',
size => 16,
is_nullable => 1,
},
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many(album_songs => 'MusicApp::Schema::Result::AlbumSong', 'song_id');
__PACKAGE__->many_to_many(albums => 'album_songs', 'album');
1;
####
package MusicApp::Schema::Result::AlbumSong;
use strict;
use warnings;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('album_songs');
__PACKAGE__->add_columns(
album_id => {
data_type => 'integer',
is_nullable => 0,
},
song_id => {
data_type => 'integer',
is_nullable => 0,
},
);
__PACKAGE__->belongs_to(album => 'MusicApp::Schema::Result::Album', 'album_id');
__PACKAGE__->belongs_to(song => 'MusicApp::Schema::Result::Song', 'song_id');
1; # Return true value at end of modules
####
package MusicApp::Model::DB;
use strict;
use base qw( Rose::DB );
__PACKAGE__->use_private_registry;
__PACKAGE__->register_db(
domain => __PACKAGE__->default_domain,
type => __PACKAGE__->default_type,
driver => 'sqlite',
database => $ENV{DB_PATH} || 'mycrud.db',
);
1;
>/code>
My yaml config file
---
abstract: 'Catalyst based application'
author:
- 'Catalyst developer'
build_requires:
ExtUtils::MakeMaker: 6.36
Test::More: '0.88'
configure_requires:
ExtUtils::MakeMaker: 6.36
distribution_type: module
dynamic_config: 1
generated_by: 'Module::Install version 1.21'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: MusicApp
no_index:
directory:
- inc
- t
requires:
Catalyst::Action::RenderView: 0
Catalyst::Plugin::ConfigLoader: 0
Catalyst::Plugin::Static::Simple: 0
Catalyst::Runtime: '5.90132'
Config::General: 0
Moose: 0
namespace::autoclean: 0
db_path: C:\MusicApp\script\music.db
resources:
license: http://dev.perl.org/licenses/
version: '0.01'
####
$c->stash(albums => [$c->stash->{resultset}->all]);