Merge pull request #32 from Pear-Trading/finn/AddOrg

Add Organisation & test
This commit is contained in:
Tom Bloor 2017-08-21 16:49:29 +01:00 committed by GitHub
commit f0334be0fc
5 changed files with 86 additions and 0 deletions

View file

@ -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');

View file

@ -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') );

View file

@ -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();

View file

@ -0,0 +1,28 @@
% layout 'admin';
% title 'Organisations';
% content_for javascript => begin
% end
% if ( my $error = flash 'error' ) {
<div class="alert alert-danger" role="alert">
<strong>Error!</strong> <%= $error %>
</div>
% } elsif ( my $success = flash 'success' ) {
<div class="alert alert-success" role="alert">
<strong>Success!</strong> <%= $success %>
</div>
% }
<div class="card mb-3">
<h3 class="card-header">
Add an Organisation
</h3>
<ul class="list-group list-group-flush">
<form action="<%= url_for . '/submit' %>" method="post">
<li class="list-group-item">Only 'Organisation Name' and 'Town' are required entries.</li>
<li class="list-group-item"><input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name"></li>
<li class="list-group-item"><input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name"></li>
<li class="list-group-item"><input id="town" type="text" class="form-control" placeholder="Town" name="town"></li>
<li class="list-group-item"><input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode"></li>
<li class="list-group-item"><button class="btn btn-success" type="submit">Add Organisation</button></li>
</form>
</ul>
</div>

View file

@ -16,6 +16,7 @@
<div class="card">
<h3 class="card-header">
Valid Organisations
<a href="<%= url_for . '/add' %>" class="btn btn-success" style="float: right">Add Organisation</a>
</h3>
<div class="list-group list-group-flush">
% for my $valid_org ($valid_orgs_rs->all) {