From a1b6dd8d2aece1c4c2f45f5a4a131571aa5544f4 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 21 Aug 2017 15:10:33 +0100 Subject: [PATCH] Add Organisation Code --- lib/Pear/LocalLoop.pm | 2 + .../Controller/Admin/Organisations.pm | 40 +++++++++++++++++++ templates/admin/organisations/add_org.html.ep | 28 +++++++++++++ templates/admin/organisations/list.html.ep | 1 + 4 files changed, 71 insertions(+) create mode 100644 templates/admin/organisations/add_org.html.ep 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/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' ) { + +% } elsif ( my $success = flash 'success' ) { + +% } +
+

+ Add an Organisation +

+ +
diff --git a/templates/admin/organisations/list.html.ep b/templates/admin/organisations/list.html.ep index 75e7670..dad10cf 100644 --- a/templates/admin/organisations/list.html.ep +++ b/templates/admin/organisations/list.html.ep @@ -16,6 +16,7 @@

Valid Organisations + Add Organisation

% for my $valid_org ($valid_orgs_rs->all) {