This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Controller/Admin/Categories.pm

118 lines
3.1 KiB
Perl
Raw Normal View History

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');
};
sub index {
2021-03-20 12:09:50 +00:00
my $c = shift;
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 ] );
}
# 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 } );
}
2021-03-20 12:09:50 +00:00
$c->redirect_to('/admin/categories');
}
# GET
sub read {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
my $id = $c->param('id');
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');
}
}
# 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');
}
}
# DELETE
sub delete {
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');
}
1;