Initial server code added for updating user account

This commit is contained in:
Finn 2017-08-23 16:50:04 +01:00
parent 6f01bf2661
commit 4c7836c9f6
2 changed files with 68 additions and 0 deletions

View file

@ -169,6 +169,7 @@ sub startup {
$admin_routes->get('/users/:id')->to('admin-users#read'); $admin_routes->get('/users/:id')->to('admin-users#read');
$admin_routes->post('/users/:id')->to('admin-users#update'); $admin_routes->post('/users/:id')->to('admin-users#update');
$admin_routes->post('/users/:id/delete')->to('admin-users#delete'); $admin_routes->post('/users/:id/delete')->to('admin-users#delete');
$admin_routes->post('/users/:id/edit')->to('admin-users#edit');
$admin_routes->get('/organisations')->to('admin-organisations#list'); $admin_routes->get('/organisations')->to('admin-organisations#list');
$admin_routes->get('/organisations/add')->to('admin-organisations#add_org'); $admin_routes->get('/organisations/add')->to('admin-organisations#add_org');

View file

@ -27,6 +27,73 @@ sub read {
} }
} }
sub edit {
my $c = shift;
my $id = $c->param('id');
if ( my $user = $c->result_set->find($id) ) {
$c->stash( user => $user );
} else {
$c->flash( error => 'No User found' );
$c->redirect_to( '/admin/users/' . $id );
}
my $validation = $c->validation;
$validation->required('email')->not_in_resultset( 'email', $user->id );
$validation->required('postcode')->postcode;
$validation->optional('new_password');
if ( defined $user->customer_id ) {
$validation->required('display_name');
$validation->required('full_name');
} elsif ( defined $user->organisation_id ) {
$validation->required('name');
$validation->required('street_name');
$validation->required('town');
}
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
$c->app->log->warn(Dumper $validation);
return $c->redirect_to( '/admin/users/' . $id );
}
if ( defined $user->customer_id ){
$c->schema->txn_do( sub {
$user->customer->update({
full_name => $validation->param('full_name'),
display_name => $validation->param('display_name'),
postcode => $validation->param('postcode'),
});
$user->update({
email => $validation->param('email'),
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
});
});
}
elsif ( defined $user->organisation_id ) {
$c->schema->txn_do( sub {
$user->organisation->update({
name => $validation->param('name'),
street_name => $validation->param('street_name'),
town => $validation->param('town'),
postcode => $validation->param('postcode'),
});
$user->update({
email => $validation->param('email'),
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
});
});
}
$c->redirect_to( '/admin/users/' . $id );
}
sub update { sub update {
my $c = shift; my $c = shift;
$c->redirect_to( '/admin/users' ); $c->redirect_to( '/admin/users' );