2017-04-08 17:25:34 +00:00
|
|
|
package Pear::LocalLoop::Controller::Register;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2017-04-09 13:17:12 +00:00
|
|
|
use DateTime;
|
|
|
|
|
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
|
|
|
name => 'Full Name is required',
|
|
|
|
email => 'Email Address is required, and must be a valid address that is not already registered',
|
|
|
|
password => 'Password is required, and must match the Confirmation field',
|
|
|
|
postcode => 'Postcode is required, and must be a valid UK Postcode',
|
|
|
|
token => 'Token is required, and must be a valid, unused token',
|
|
|
|
agerange => 'Age Range is required, and must be a selection from the drop-down',
|
2017-04-09 13:21:36 +00:00
|
|
|
unknown => 'Sorry, there was a problem registering! Have you already registered?',
|
2017-04-09 13:17:12 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-08 17:25:34 +00:00
|
|
|
sub index {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $agerange_rs = $c->schema->resultset('AgeRange');
|
|
|
|
$agerange_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
2017-04-09 13:17:12 +00:00
|
|
|
$c->stash( ageranges => [ $agerange_rs->all ], form_data => {}, error => undef );
|
2017-04-08 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub register {
|
|
|
|
my $c = shift;
|
|
|
|
my $validation = $c->validation;
|
2017-04-09 11:50:50 +00:00
|
|
|
|
2017-04-09 13:17:12 +00:00
|
|
|
$validation->required('name');
|
2017-04-09 11:50:50 +00:00
|
|
|
$validation->required('password')->equal_to('password2');
|
|
|
|
$validation->required('postcode')->postcode;
|
|
|
|
|
2017-04-09 13:17:12 +00:00
|
|
|
my $user_rs = $c->schema->resultset('User');
|
|
|
|
$validation->required('email')->email->not_in_resultset('email', $user_rs);
|
|
|
|
|
2017-04-09 11:50:50 +00:00
|
|
|
my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0});
|
2017-04-21 20:28:50 +00:00
|
|
|
$validation->required('token')->in_resultset('name', $token_rs);
|
2017-04-09 11:50:50 +00:00
|
|
|
|
|
|
|
my $age_rs = $c->schema->resultset('AgeRange');
|
2017-04-21 20:20:47 +00:00
|
|
|
$validation->required('agerange')->in_resultset('id', $age_rs);
|
2017-04-08 17:25:34 +00:00
|
|
|
|
2017-04-09 13:17:12 +00:00
|
|
|
my @error_messages;
|
|
|
|
if ( $validation->has_error ) {
|
|
|
|
my $failed_vals = $validation->failed;
|
|
|
|
@error_messages = map {$c->error_messages->{ $_ } } @$failed_vals;
|
|
|
|
} else {
|
|
|
|
my $new_user = $c->schema->resultset('User')->find_or_new({
|
|
|
|
email => $validation->param('email'),
|
2017-04-21 19:52:14 +00:00
|
|
|
password => $validation->param('password'),
|
2017-04-09 13:17:12 +00:00
|
|
|
customer => {
|
|
|
|
username => $validation->param('name'),
|
|
|
|
postcode => $validation->param('postcode'),
|
2017-04-21 20:20:47 +00:00
|
|
|
age_range_id => $validation->param('agerange'),
|
2017-04-09 13:17:12 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
if ( $new_user->in_storage ) {
|
|
|
|
@error_messages = ( $c->error_messages->{unknown} );
|
|
|
|
} else {
|
|
|
|
$new_user->insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( scalar @error_messages ) {
|
|
|
|
$age_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
|
|
|
$c->stash(
|
|
|
|
error => \@error_messages,
|
|
|
|
ageranges => [ $age_rs->all ],
|
|
|
|
form_data => {
|
|
|
|
name => $validation->param('name'),
|
|
|
|
email => $validation->param('email'),
|
|
|
|
postcode => $validation->param('postcode'),
|
|
|
|
token => $validation->param('token'),
|
|
|
|
agerange => $validation->param('agerange'),
|
|
|
|
}
|
|
|
|
);
|
2017-04-09 13:21:36 +00:00
|
|
|
$c->render( template => 'register/index' );
|
2017-04-09 13:17:12 +00:00
|
|
|
} else {
|
|
|
|
$c->flash( success => 'Registered Successfully, please log in' );
|
|
|
|
$c->redirect_to('/');
|
|
|
|
}
|
2017-04-08 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|