Code fixed, now functioning account edit

This commit is contained in:
Finn 2017-08-24 12:37:53 +01:00
parent 9865130666
commit db45f710c3
2 changed files with 85 additions and 21 deletions

View file

@ -1,17 +1,28 @@
package Pear::LocalLoop::Controller::Admin::Users;
use Mojo::Base 'Mojolicious::Controller';
use Try::Tiny;
use Data::Dumper;
has result_set => sub {
has user_result_set => sub {
my $c = shift;
return $c->schema->resultset('User');
};
has customer_result_set => sub {
my $c = shift;
return $c->schema->resultset('Customer');
};
has organisation_result_set => sub {
my $c = shift;
return $c->schema->resultset('Organisation');
};
sub index {
my $c = shift;
my $user_rs = $c->result_set;
my $user_rs = $c->user_result_set;
$user_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash( users => [ $user_rs->all ] );
}
@ -21,7 +32,7 @@ sub read {
my $id = $c->param('id');
if ( my $user = $c->result_set->find($id) ) {
if ( my $user = $c->user_result_set->find($id) ) {
$c->stash( user => $user );
} else {
$c->flash( error => 'No User found' );
@ -36,14 +47,14 @@ sub edit {
my $user;
unless ( $user = $c->result_set->find($id) ) {
unless ( $user = $c->user_result_set->find($id) ) {
$c->flash( error => 'No User found' );
return $c->redirect_to( '/admin/users/' . $id );
}
my $validation = $c->validation;
my $not_myself_user_rs = $c->result_set->search({
my $not_myself_user_rs = $c->user_result_set->search({
id => { "!=" => $user->id },
});
$validation->required('email')->email->not_in_resultset( 'email', $not_myself_user_rs );

View file

@ -11,20 +11,73 @@
<strong>Success!</strong> <%= $success %>
</div>
% }
<form action="<%= url_for %>" method="post">
<div class="form-group">
<label for="email">Email Address</label>
<input id="email" type="text" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>" disabled>
</div>
<div class="form-group">
<label for="joindate">Join Date</label>
<input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>" disabled>
</div>
<div class="form-group">
<label for="type">Account Type</label>
<input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>" disabled>
</div>
<div class="form-group">
<button class="btn btn-primary form-control" type="submit">Update</button>
</div>
<form action="<%= url_for . '/edit' %>" method="post">
<h3 class="card-header">
User Details
</h3>
<div class="form-group">
<label for="email">Email Address</label>
<input id="email" type="text" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>">
</div>
<div class="form-group">
<label for="joindate">Join Date</label>
<input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>">
</div>
<div class="form-group">
<label for="type">Account Type</label>
<input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>">
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input id="new_password" type="text" class="form-control" placeholder="New Password" name="new_password">
</div>
% if ( my $customer_rs = $user->customer ) {
<h3 class="card-header">
Customer Details
</h3>
<div class="form-group">
<label for="postcode">Customer Postcode</label>
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $customer_rs->postcode %>">
</div>
<div class="form-group">
<label for="full_name">Full Name</label>
<input id="full_name" type="text" class="form-control" placeholder="Full Name" name="full_name" value="<%= $customer_rs->full_name %>">
</div>
<div class="form-group">
<label for="display_name">Display Name</label>
<input id="display_name" type="text" class="form-control" placeholder="Display Name" name="display_name" value="<%= $customer_rs->display_name %>">
</div>
<div class="form-group">
<label for="year_of_birth">Year of Birth</label>
<input id="year_of_birth" type="number" class="form-control" placeholder="Year of Birth" name="year_of_birth" value="<%= $customer_rs->year_of_birth %>">
</div>
% } elsif ( my $org_rs = $user->organisation ) {
<h3 class="card-header">
Organisation Details
</h3>
<div class="form-group">
<label for="postcode">Organisation Postcode</label>
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $org_rs->postcode %>">
</div>
<div class="form-group">
<label for="name">Organisation Name</label>
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $org_rs->name %>">
</div>
<div class="form-group">
<label for="street_name">Street Name</label>
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $org_rs->street_name %>">
</div>
<div class="form-group">
<label for="town">Town</label>
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $org_rs->town %>">
</div>
% } else {
<h3 class="card-header">
User is not a customer or an organisation
</h3>
% }
<div class="form-group">
<button class="btn btn-primary form-control" type="submit">Edit Account</button>
</div>
</form>