Merge pull request #31 from Pear-Trading/finn/EditOrganisation

Edit Orgs capability and added test
This commit is contained in:
Finn 2017-08-21 14:03:06 +01:00 committed by GitHub
commit 0a65cc2f47
5 changed files with 171 additions and 7 deletions

View file

@ -172,7 +172,9 @@ sub startup {
$admin_routes->get('/organisations')->to('admin-organisations#list');
$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');
$admin_routes->post('/organisations/pending/:id/edit')->to('admin-organisations#pending_edit');
$admin_routes->get('/organisations/pending/:id/approve')->to('admin-organisations#pending_approve');
$admin_routes->get('/feedback')->to('admin-feedback#index');

View file

@ -2,6 +2,7 @@ package Pear::LocalLoop::Controller::Admin::Organisations;
use Mojo::Base 'Mojolicious::Controller';
use Try::Tiny;
use Data::Dumper;
sub list {
my $c = shift;
@ -30,6 +31,43 @@ sub valid_read {
);
}
sub valid_edit {
my $c = shift;
my $validation = $c->validation;
$validation->required('name');
$validation->required('street_name');
$validation->required('town');
$validation->required('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/valid/' . $c->param('id') );
}
my $valid_org = $c->schema->resultset('Organisation')->find( $c->param('id') );
try {
$c->schema->storage->txn_do( sub {
$valid_org->update({
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 Updating the Organisation' );
$c->app->log->warn(Dumper @_);
} else {
$c->flash( success => 'Updated Organisation' );
}
};
$c->redirect_to( '/admin/organisations/valid/' . $valid_org->id );
}
sub pending_read {
my $c = shift;
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );
@ -45,6 +83,43 @@ sub pending_read {
);
}
sub pending_edit {
my $c = shift;
my $validation = $c->validation;
$validation->required('name');
$validation->required('street_name');
$validation->required('town');
$validation->required('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/pending/' . $c->param('id') );
}
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );
try {
$c->schema->storage->txn_do( sub {
$pending_org->update({
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 Updating the Organisation' );
$c->app->log->warn(Dumper @_);
} else {
$c->flash( success => 'Updated Organisation' );
}
};
$c->redirect_to( '/admin/organisations/pending/' . $pending_org->id );
}
sub pending_approve {
my $c = shift;
my $pending_org = $c->schema->resultset('PendingOrganisation')->find( $c->param('id') );

81
t/admin/update.t Normal file
View file

@ -0,0 +1,81 @@
use Mojo::Base -strict;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework;
my $schema = $t->app->schema;
my $user = $schema->resultset('User')->create({
email => 'admin@example.com',
password => 'abc123',
administrator => {},
});
is $schema->resultset('Administrator')->count, 1, 'Admin Created';
$schema->resultset('Organisation')->create({
id => 1,
name => 'Shinra Electric Power Company',
street_name => 'Sector 0, Midgar, Eastern Continent',
town => 'Gaia',
postcode => 'WC1E 6AD',
});
$schema->resultset('PendingOrganisation')->create({
id => 2,
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
postcode => 'WC1E 6AD',
submitted_by_id => $user->id,
});
#login to admin
$t->ua->max_redirects(10);
$t->post_ok('/admin', form => {
email => 'admin@example.com',
password => 'abc123',
})->status_is(200);
#Read approved organisation
$t->get_ok('/admin/organisations/valid/1/')
->status_is(200);
#Read pending organisation
$t->get_ok('/admin/organisations/pending/2/')
->status_is(200);
#Valid approved organisation update
$t->post_ok('/admin/organisations/valid/1/edit', form => {
name => 'Shinra Electric Power Company',
street_name => 'Sector 0, Midgar, Eastern Continent',
town => 'Gaia',
postcode => 'WC1E 6AD',
})->status_is(200)->content_like(qr/Updated Organisation/);
#Failed validation on approved organisation
$t->post_ok('/admin/organisations/valid/1/edit', form => {
name => 'Shinra Electric Power Company',
street_name => 'Sector 0, Midgar, Eastern Continent',
postcode => 'WC1E 6AD',
})->content_like(qr/The validation has failed/);
#Valid pending organisation update
$t->post_ok('/admin/organisations/pending/2/edit', form => {
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
postcode => 'WC1E 6AD',
})->status_is(200)->content_like(qr/Updated Organisation/);
#Failed validation on pending organisation
$t->post_ok('/admin/organisations/pending/2/edit', form => {
name => '7th Heaven',
street_name => 'Slums, Sector 7',
postcode => 'WC1E 6AD',
})->content_like(qr/The validation has failed/);
done_testing();

View file

@ -16,10 +16,12 @@
%= $pending_org->name
</h3>
<ul class="list-group list-group-flush">
<li class="list-group-item">Street Name: <%= $pending_org->street_name %></li>
<li class="list-group-item">Town: <%= $pending_org->town %></li>
<li class="list-group-item">Postcode: <%= $pending_org->postcode %></li>
<li class="list-group-item"><a href="<%= url_for . '/approve' %>" class="btn btn-success">Approve Organisation</a></li>
<form action="<%= url_for . '/edit' %>" method="post">
<li class="list-group-item"><input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $pending_org->name %>"></li>
<li class="list-group-item"><input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $pending_org->street_name %>"></li>
<li class="list-group-item"><input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $pending_org->town %>"></li>
<li class="list-group-item"><input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $pending_org->postcode %>"></li>
<li class="list-group-item"><button class="btn btn-success" type="submit">Update</button><a href="<%= url_for . '/approve' %>" class="btn btn-success">Approve Organisation</a></li>
</ul>
</div>
<div class="card mb-3">

View file

@ -16,9 +16,13 @@
%= $valid_org->name
</h3>
<ul class="list-group list-group-flush">
<li class="list-group-item">Street Name: <%= $valid_org->street_name %></li>
<li class="list-group-item">Town: <%= $valid_org->town %></li>
<li class="list-group-item">Postcode: <%= $valid_org->postcode %></li>
<form action="<%= url_for . '/edit' %>" method="post">
<li class="list-group-item"><input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>"></li>
<li class="list-group-item"><input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $valid_org->street_name %>"></li>
<li class="list-group-item"><input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>"></li>
<li class="list-group-item"><input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>"></li>
<li class="list-group-item"><button class="btn btn-success" type="submit">Edit Organisation</button></li>
</form>
</ul>
</div>
<div class="card mb-3">