Refactor dbh items in api register to dbic
This commit is contained in:
parent
d3dab84d06
commit
a8b917808f
2 changed files with 36 additions and 65 deletions
1
cpanfile
1
cpanfile
|
@ -5,7 +5,6 @@ requires 'Devel::Dwarn';
|
||||||
requires 'Mojo::JSON';
|
requires 'Mojo::JSON';
|
||||||
requires 'Email::Valid';
|
requires 'Email::Valid';
|
||||||
requires 'Geo::UK::Postcode';
|
requires 'Geo::UK::Postcode';
|
||||||
requires 'ORM::Date';
|
|
||||||
requires 'Authen::Passphrase::BlowfishCrypt';
|
requires 'Authen::Passphrase::BlowfishCrypt';
|
||||||
requires 'Time::Fake';
|
requires 'Time::Fake';
|
||||||
requires 'Scalar::Util';
|
requires 'Scalar::Util';
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package Pear::LocalLoop::Controller::Api::Register;
|
package Pear::LocalLoop::Controller::Api::Register;
|
||||||
use Mojo::Base 'Mojolicious::Controller';
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
use ORM::Date;
|
use DateTime;
|
||||||
use Data::Dumper;
|
|
||||||
|
|
||||||
has error_messages => sub {
|
has error_messages => sub {
|
||||||
return {
|
return {
|
||||||
|
@ -46,8 +45,6 @@ sub post_register{
|
||||||
my $validation = $c->validation;
|
my $validation = $c->validation;
|
||||||
|
|
||||||
my $json = $self->req->json;
|
my $json = $self->req->json;
|
||||||
$self->app->log->debug( "\n\nStart of register");
|
|
||||||
$self->app->log->debug( "JSON: " . Dumper $json );
|
|
||||||
|
|
||||||
if ( ! defined $json ){
|
if ( ! defined $json ){
|
||||||
$self->app->log->debug('Path: file:' . __FILE__ . ', line: ' . __LINE__);
|
$self->app->log->debug('Path: file:' . __FILE__ . ', line: ' . __LINE__);
|
||||||
|
@ -85,6 +82,7 @@ sub post_register{
|
||||||
|
|
||||||
} elsif ( $usertype eq 'organisation' ) {
|
} elsif ( $usertype eq 'organisation' ) {
|
||||||
|
|
||||||
|
#TODO validation on the address. Or perhaps add the organisation to a "to be inspected" list then manually check them.
|
||||||
$validation->required('fulladdress');
|
$validation->required('fulladdress');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -111,75 +109,49 @@ sub post_register{
|
||||||
|
|
||||||
my $hashedPassword = $self->generate_hashed_password($password);
|
my $hashedPassword = $self->generate_hashed_password($password);
|
||||||
|
|
||||||
my $secondsTime = time();
|
|
||||||
my $date = ORM::Date->new_epoch($secondsTime)->mysql_date;
|
|
||||||
|
|
||||||
if ($usertype eq 'customer'){
|
if ($usertype eq 'customer'){
|
||||||
my $ageForeignKey = $self->get_age_foreign_key( $validation->param('age') );
|
my $ageForeignKey = $self->get_age_foreign_key( $validation->param('age') );
|
||||||
|
|
||||||
#TODO this will go away with a transaction, when we move this bit to dbic schema code
|
$c->schema->txn_do( sub {
|
||||||
#TODO UNTESTED as it's hard to simulate.
|
$c->schema->resultset('AccountToken')->find({
|
||||||
#Token is no longer valid race condition.
|
accounttokenname => $token,
|
||||||
if ( ! $self->set_token_as_used($token) ){
|
used => 0,
|
||||||
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
|
})->update({ used => 1 });
|
||||||
return $self->render( json => {
|
$c->schema->resultset('User')->create({
|
||||||
success => Mojo::JSON->false,
|
customer => {
|
||||||
message => 'Token no longer is accepted.',
|
username => $username,
|
||||||
},
|
agerange_fk => $ageForeignKey,
|
||||||
status => 500,); #Internal server error. Racecondition
|
postcode => $postcode,
|
||||||
}
|
},
|
||||||
|
email => $email,
|
||||||
|
hashedpassword => $hashedPassword,
|
||||||
|
joindate => DateTime->now,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
my ($idToUse) = $self->db->selectrow_array("SELECT MAX(CustomerId) FROM Customers");
|
|
||||||
if (defined $idToUse){
|
|
||||||
$idToUse++;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$idToUse = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#TODO Race condition here.
|
|
||||||
my $insertCustomer = $self->db->prepare("INSERT INTO Customers (CustomerId, UserName, AgeRange_FK, PostCode) VALUES (?, ?, ?, ?)");
|
|
||||||
my $rowsInsertedCustomer = $insertCustomer->execute($idToUse, $username, $ageForeignKey, $postcode);
|
|
||||||
my $insertUser = $self->db->prepare("INSERT INTO Users (CustomerId_FK, Email, JoinDate, HashedPassword) VALUES (?, ?, ?, ?)");
|
|
||||||
my $rowsInsertedUser = $insertUser->execute($idToUse, $email, $date, $hashedPassword);
|
|
||||||
|
|
||||||
return $self->render( json => { success => Mojo::JSON->true } );
|
|
||||||
}
|
}
|
||||||
elsif ($usertype eq 'organisation') {
|
elsif ($usertype eq 'organisation') {
|
||||||
#TODO validation on the address. Or perhaps add the organisation to a "to be inspected" list then manually check them.
|
|
||||||
my $fullAddress = $validation->param('fulladdress');
|
my $fullAddress = $validation->param('fulladdress');
|
||||||
|
|
||||||
# TODO This will go away with transactioning
|
$c->schema->txn_do( sub {
|
||||||
#TODO UNTESTED as it's hard to simulate.
|
$c->schema->resultset('AccountToken')->find({
|
||||||
#Token is no longer valid race condition.
|
accounttokenname => $token,
|
||||||
if ( ! $self->set_token_as_used($token) ){
|
used => 0,
|
||||||
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);
|
})->update({ used => 1 });
|
||||||
return $self->render( json => {
|
$c->schema->resultset('User')->create({
|
||||||
success => Mojo::JSON->false,
|
organisation => {
|
||||||
message => 'Token no longer is accepted.',
|
name => $username,
|
||||||
},
|
fulladdress => $fullAddress,
|
||||||
status => 500,); #Internal server error. Racecondition
|
postcode => $postcode,
|
||||||
}
|
},
|
||||||
|
email => $email,
|
||||||
my $idToUse = $self->db->selectrow_array("SELECT MAX(OrganisationalId) FROM Organisations");
|
hashedpassword => $hashedPassword,
|
||||||
if (defined $idToUse){
|
joindate => DateTime->now,
|
||||||
$idToUse++;
|
});
|
||||||
}
|
});
|
||||||
else{
|
|
||||||
$idToUse = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#TODO Race condition here.
|
|
||||||
my $insertOrganisation = $self->db->prepare("INSERT INTO Organisations (OrganisationalId, Name, FullAddress, PostCode) VALUES (?, ?, ?, ?)");
|
|
||||||
my $rowsInsertedOrganisation = $insertOrganisation->execute($idToUse, $username, $fullAddress, $postcode);
|
|
||||||
my $insertUser = $self->db->prepare("INSERT INTO Users (OrganisationalId_FK, Email, JoinDate, HashedPassword) VALUES (?, ?, ?, ?)");
|
|
||||||
my $rowsInsertedUser = $insertUser->execute($idToUse, $email, $date, $hashedPassword);
|
|
||||||
|
|
||||||
return $self->render( json => { success => Mojo::JSON->true } );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $self->render( json => { success => Mojo::JSON->true } );
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
|
|
Reference in a new issue