diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index a04a9bc..4813064 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -171,6 +171,8 @@ sub startup { $admin_routes->post('/users/:id/delete')->to('admin-users#delete'); $admin_routes->get('/organisations')->to('admin-organisations#list'); + $admin_routes->get('/organisations/add')->to('admin-organisations#add_org'); + $admin_routes->post('/organisations/add/submit')->to('admin-organisations#add_org_submit'); $admin_routes->get('/organisations/valid/:id')->to('admin-organisations#valid_read'); $admin_routes->post('/organisations/valid/:id/edit')->to('admin-organisations#valid_edit'); $admin_routes->get('/organisations/pending/:id')->to('admin-organisations#pending_read'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm index 0b3e4c5..178d9f7 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Organisations.pm @@ -16,6 +16,46 @@ sub list { ); } +sub add_org { + my $c = shift; +} + +sub add_org_submit { + my $c = shift; + + my $validation = $c->validation; + + $validation->required('name'); + $validation->optional('street_name'); + $validation->required('town'); + $validation->optional('postcode')->postcode; + + if ( $validation->has_error ) { + $c->flash( error => 'The validation has failed' ); + $c->app->log->warn(Dumper $validation); + return $c->redirect_to( '/admin/organisations/add/' ); + } + + my $organisation; + + try { + $organisation = $c->schema->resultset('Organisation')->create({ + name => $validation->param('name'), + street_name => $validation->param('street_name'), + town => $validation->param('town'), + postcode => $validation->param('postcode'), + }); + } finally { + if ( @_ ) { + $c->flash( error => 'Something went wrong Adding the Organisation' ); + $c->app->log->warn(Dumper @_); + } else { + $c->flash( success => 'Added Organisation' ); + } + }; + $c->redirect_to( '/admin/organisations/add/' ); +} + sub valid_read { my $c = shift; my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') ); diff --git a/t/admin/update.t b/t/admin/organisation.t similarity index 82% rename from t/admin/update.t rename to t/admin/organisation.t index 84fc3e6..720415a 100644 --- a/t/admin/update.t +++ b/t/admin/organisation.t @@ -78,4 +78,19 @@ $t->post_ok('/admin/organisations/pending/2/edit', form => { postcode => 'WC1E 6AD', })->content_like(qr/The validation has failed/); +#Valid adding organisation +$t->post_ok('/admin/organisations/add/submit', form => { + name => 'Wall Market', + street_name => 'Slums, Sector 6', + town => 'Midgar', + postcode => 'TN35 5AQ', +})->status_is(200)->content_like(qr/Added Organisation/); + +#Failed validation on adding organisation +$t->post_ok('/admin/organisations/add/submit', form => { + name => 'Wall Market', + street_name => 'Slums, Sector 6', + postcode => 'TN35 5AQ', +})->content_like(qr/The validation has failed/); + done_testing(); diff --git a/templates/admin/organisations/add_org.html.ep b/templates/admin/organisations/add_org.html.ep new file mode 100644 index 0000000..07cf699 --- /dev/null +++ b/templates/admin/organisations/add_org.html.ep @@ -0,0 +1,28 @@ +% layout 'admin'; +% title 'Organisations'; +% content_for javascript => begin +% end +% if ( my $error = flash 'error' ) { +