2018-01-11 16:23:42 +00:00
package Pear::LocalLoop::Controller::Admin::Categories ;
use Mojo::Base 'Mojolicious::Controller' ;
has result_set = > sub {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
return $ c - > schema - > resultset ( 'Category' ) ;
2018-01-11 16:23:42 +00:00
} ;
2021-03-20 19:03:59 +00:00
sub idx {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
2018-01-11 16:23:42 +00:00
2021-03-20 12:09:50 +00:00
my $ category_rs = $ c - > result_set ;
$ category_rs - > result_class ( 'DBIx::Class::ResultClass::HashRefInflator' ) ;
$ c - > stash ( categories = > [ $ category_rs - > all ] ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2018-01-11 16:23:42 +00:00
}
# POST
sub create {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
my $ validation = $ c - > validation ;
$ validation - > required ( 'category' , 'trim' )
- > not_in_resultset ( 'name' , $ c - > result_set ) ;
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 } ) ;
2018-01-11 16:23:42 +00:00
}
2021-03-20 12:09:50 +00:00
$ c - > redirect_to ( '/admin/categories' ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2018-01-11 16:23:42 +00:00
}
# GET
2021-03-20 19:03:59 +00:00
sub get {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
2018-01-11 16:23:42 +00:00
2021-03-20 12:09:50 +00:00
my $ id = $ c - > param ( 'id' ) ;
2018-01-11 16:23:42 +00:00
2021-03-20 12:09:50 +00:00
if ( my $ category = $ c - > result_set - > find ( $ id ) ) {
$ c - > stash ( category = > $ category ) ;
}
else {
$ c - > flash ( error = > 'No Category found' ) ;
$ c - > redirect_to ( '/admin/categories' ) ;
}
2021-03-20 15:02:00 +00:00
return 1 ;
2018-01-11 16:23:42 +00:00
}
# POST
sub update {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
my $ validation = $ c - > validation ;
$ validation - > required ( 'id' ) ;
$ validation - > required ( 'category' , 'trim' ) - > like ( qr/^[\w]*$/ ) ;
$ validation - > optional ( 'line_icon' ) ;
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 (
{
id = > $ validation - > param ( 'id' ) ,
name = > $ validation - > param ( 'category' ) ,
line_icon = > (
defined $ validation - > param ( 'line_icon' )
? $ validation - > param ( 'line_icon' )
: undef
) ,
}
) ;
$ c - > flash ( success = > 'Category Updated' ) ;
$ c - > redirect_to ( '/admin/categories/' . $ validation - > param ( 'id' ) ) ;
}
else {
$ c - > flash ( error = > 'No Category found' ) ;
$ c - > redirect_to ( '/admin/categories' ) ;
}
2021-03-20 15:02:00 +00:00
return 1 ;
2018-01-11 16:23:42 +00:00
}
# DELETE
2021-03-20 19:03:59 +00:00
sub del {
2021-03-20 12:09:50 +00:00
my $ c = shift ;
my $ id = $ c - > param ( 'id' ) ;
if ( my $ category = $ c - > result_set - > find ( $ id ) ) {
$ category - > transaction_category - > delete ;
$ category - > delete ;
$ c - > flash ( success = > 'Category Deleted' ) ;
}
else {
$ c - > flash ( error = > 'No Category found' ) ;
}
$ c - > redirect_to ( '/admin/categories' ) ;
2021-03-20 15:02:00 +00:00
return 1 ;
2018-01-11 16:23:42 +00:00
}
1 ;