2018-01-11 16:23:42 +00:00
|
|
|
package Pear::LocalLoop::Controller::Admin::Categories;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
|
|
has result_set => sub {
|
|
|
|
my $c = shift;
|
|
|
|
return $c->schema->resultset('Category');
|
|
|
|
};
|
|
|
|
|
|
|
|
sub index {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $category_rs = $c->result_set;
|
|
|
|
$category_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
|
|
|
$c->stash( categories => [ $category_rs->all ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
# POST
|
|
|
|
sub create {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
2018-01-18 14:24:47 +00:00
|
|
|
$validation->required('category', 'trim')->not_in_resultset('name', $c->result_set);
|
2018-01-11 16:23:42 +00:00
|
|
|
|
|
|
|
my $category_name = $validation->param('category');
|
|
|
|
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
my $check = shift @{ $c->validation->error('category') };
|
|
|
|
if ( $check eq 'required' ) {
|
|
|
|
$c->flash( error => 'Category name is required' );
|
|
|
|
} elsif ( $check eq 'like' ) {
|
|
|
|
$c->flash( error => 'Category name not valid - Alphanumeric characters and Underscore only' );
|
|
|
|
} elsif ( $check eq 'not_in_resultset' ) {
|
|
|
|
$c->flash( error => 'Category Already Exists' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Category Created' );
|
|
|
|
$c->result_set->create({ name => $category_name });
|
|
|
|
}
|
|
|
|
$c->redirect_to( '/admin/categories' );
|
|
|
|
}
|
|
|
|
|
|
|
|
# GET
|
|
|
|
sub read {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $id = $c->param('id');
|
|
|
|
|
|
|
|
if ( my $category = $c->result_set->find($id) ) {
|
|
|
|
$c->stash( category => $category );
|
|
|
|
} else {
|
|
|
|
$c->flash( error => 'No Category found' );
|
|
|
|
$c->redirect_to( '/admin/categories' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# POST
|
|
|
|
sub update {
|
|
|
|
my $c = shift;
|
|
|
|
my $validation = $c->validation;
|
2018-01-17 17:36:55 +00:00
|
|
|
$validation->required('id');
|
2018-01-11 16:23:42 +00:00
|
|
|
$validation->required('category', 'trim')->like(qr/^[\w]*$/);
|
|
|
|
|
|
|
|
my $id = $c->param('id');
|
|
|
|
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
my $names = $validation->failed;
|
|
|
|
$c->flash( error => 'Error in submitted data: ' . join(', ', @$names) );
|
|
|
|
$c->redirect_to( '/admin/categories/' . $id );
|
|
|
|
} elsif ( my $category = $c->result_set->find($id) ) {
|
|
|
|
$category->update({
|
2018-01-17 17:36:55 +00:00
|
|
|
id => $validation->param('id'),
|
2018-01-11 16:23:42 +00:00
|
|
|
name => $validation->param('category'),
|
|
|
|
});
|
|
|
|
$c->flash( success => 'Category Updated' );
|
2018-01-17 17:38:07 +00:00
|
|
|
$c->redirect_to( '/admin/categories/' . $validation->param('id') );
|
2018-01-11 16:23:42 +00:00
|
|
|
} else {
|
|
|
|
$c->flash( error => 'No Category found' );
|
|
|
|
$c->redirect_to( '/admin/categories' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# DELETE
|
|
|
|
sub delete {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $id = $c->param('id');
|
|
|
|
|
|
|
|
if ( my $category = $c->result_set->find($id) ) {
|
2018-01-16 14:47:17 +00:00
|
|
|
$category->transaction_category->delete;
|
2018-01-11 16:23:42 +00:00
|
|
|
$category->delete;
|
|
|
|
$c->flash( success => 'Category Deleted' );
|
|
|
|
} else {
|
|
|
|
$c->flash( error => 'No Category found' );
|
|
|
|
}
|
|
|
|
$c->redirect_to( '/admin/categories' );
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|