Adding auto recovery of database generated data in creation.
This commit is contained in:
parent
7ffeac7bc7
commit
4a3a3b9e36
@ -33,17 +33,11 @@
|
|||||||
"Peace::DAO::Customer" : {
|
"Peace::DAO::Customer" : {
|
||||||
"file" : "lib/Peace/DAO/Customer.pm"
|
"file" : "lib/Peace/DAO/Customer.pm"
|
||||||
},
|
},
|
||||||
"Peace::DAO::User" : {
|
|
||||||
"file" : "lib/Peace/DAO/User.pm"
|
|
||||||
},
|
|
||||||
"Peace::DB" : {
|
"Peace::DB" : {
|
||||||
"file" : "lib/Peace/DB.pm"
|
"file" : "lib/Peace/DB.pm"
|
||||||
},
|
},
|
||||||
"Peace::Model::Customer" : {
|
"Peace::Model::Customer" : {
|
||||||
"file" : "lib/Peace/Model/Customer.pm"
|
"file" : "lib/Peace/Model/Customer.pm"
|
||||||
},
|
|
||||||
"Peace::Model::User" : {
|
|
||||||
"file" : "lib/Peace/Model/User.pm"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"release_status" : "stable",
|
"release_status" : "stable",
|
||||||
|
@ -6,7 +6,7 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Params::ValidationCompiler qw/validation_for/;
|
use Params::ValidationCompiler qw/validation_for/;
|
||||||
use Types::Standard qw/HasMethods InstanceOf/;
|
use Types::Standard qw/HasMethods InstanceOf Str/;
|
||||||
|
|
||||||
{
|
{
|
||||||
my $validator = validation_for(
|
my $validator = validation_for(
|
||||||
@ -28,21 +28,55 @@ use Types::Standard qw/HasMethods InstanceOf/;
|
|||||||
{
|
{
|
||||||
my $validator = validation_for(
|
my $validator = validation_for(
|
||||||
params => {
|
params => {
|
||||||
customer => { type => InstanceOf['Peace::Model::Customer'] },
|
customer => { type => InstanceOf ['Peace::Model::Customer'] },
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
sub create {
|
sub create {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my %params = $validator->(@_);
|
my %params = $validator->(@_);
|
||||||
my $customer = $params{customer};
|
my $customer = $params{customer};
|
||||||
my $dbh = $self->_dbh;
|
my $dbh = $self->_dbh;
|
||||||
my $insert = <<'EOF';
|
my $insert = <<'EOF';
|
||||||
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
||||||
EOF
|
EOF
|
||||||
my $result = $dbh->selectrow_hashref($insert, undef, $customer->secret);
|
my $result =
|
||||||
my $uuid = $result->{uuid};
|
$dbh->selectrow_hashref( $insert, undef, $customer->secret );
|
||||||
$customer->uuid($uuid);
|
my $uuid = $result->{uuid};
|
||||||
return $customer;
|
$customer->uuid($uuid);
|
||||||
|
my $new_customer = $self->recover_by_uuid( uuid => $uuid );
|
||||||
|
$customer->secret( $new_customer->secret )
|
||||||
|
if defined $new_customer->secret;
|
||||||
|
$customer->date_creation( $new_customer->date_creation )
|
||||||
|
if defined $new_customer->date_creation;
|
||||||
|
$customer->stripe_id( $new_customer->stripe_id )
|
||||||
|
if defined $new_customer->stripe_id;
|
||||||
|
return $customer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
my $validator = validation_for(
|
||||||
|
params => {
|
||||||
|
uuid => { type => Str },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
sub recover_by_uuid {
|
||||||
|
my $self = shift;
|
||||||
|
my $dbh = $self->_dbh;
|
||||||
|
|
||||||
|
my %params = $validator->(@_);
|
||||||
|
my $uuid = $params{uuid};
|
||||||
|
|
||||||
|
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
|
||||||
|
SELECT uuid, date_creation, secret, stripe_id FROM customer WHERE uuid = ?;
|
||||||
|
EOF
|
||||||
|
for my $key ( keys %$result ) {
|
||||||
|
delete $result->{$key} unless defined $result->{$key};
|
||||||
|
}
|
||||||
|
my $customer = Peace::Model::Customer->new(%$result);
|
||||||
|
return $customer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,9 +3,10 @@ use v5.30.0;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Test::Most tests => 2;
|
use Test::Most tests => 6;
|
||||||
|
|
||||||
use DBI;
|
use DBI;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
use Peace::Model::Customer;
|
use Peace::Model::Customer;
|
||||||
|
|
||||||
@ -18,22 +19,57 @@ BEGIN {
|
|||||||
my $sql = <<'EOF';
|
my $sql = <<'EOF';
|
||||||
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
||||||
EOF
|
EOF
|
||||||
my $uuid = 'hola';
|
my $uuid = 'hola';
|
||||||
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
||||||
my $customer = Peace::Model::Customer->new( secret => 'hola' );
|
my $customer = Peace::Model::Customer->new( secret => 'hola' );
|
||||||
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
||||||
|
|
||||||
$dbh->{mock_add_resultset} = {
|
$dbh->{mock_add_resultset} = {
|
||||||
sql => $sql,
|
sql => $sql,
|
||||||
results => [
|
results => [ ['uuid'], [$uuid], ]
|
||||||
[ 'uuid' ],
|
};
|
||||||
[ $uuid ],
|
$sql = <<'EOF';
|
||||||
]
|
SELECT uuid, date_creation, secret, stripe_id FROM customer WHERE uuid = ?;
|
||||||
|
EOF
|
||||||
|
$dbh->{mock_add_resultset} = {
|
||||||
|
sql => $sql,
|
||||||
|
results => [ ['uuid', 'secret', 'date_creation', 'stripe_id'], [$uuid, 'hola', undef, undef] ],
|
||||||
};
|
};
|
||||||
|
|
||||||
## WHEN
|
## WHEN
|
||||||
$customer_dao->create( customer => $customer );
|
$customer_dao->create( customer => $customer );
|
||||||
|
|
||||||
## THEN
|
## THEN
|
||||||
is $customer->uuid, $uuid, 'Customer id correctly set after user creation.';
|
is $customer->uuid, $uuid, 'Customer id correctly set after user creation.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
## GIVEN
|
||||||
|
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
||||||
|
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
||||||
|
my $sql = <<'EOF';
|
||||||
|
SELECT uuid, date_creation, secret, stripe_id FROM customer WHERE uuid = ?;
|
||||||
|
EOF
|
||||||
|
my $uuid = 'hola';
|
||||||
|
my $date_creation = DateTime->now;
|
||||||
|
my $secret = 'hola';
|
||||||
|
my $stripe_id = undef;
|
||||||
|
|
||||||
|
$dbh->{mock_add_resultset} = {
|
||||||
|
sql => $sql,
|
||||||
|
results => [
|
||||||
|
[ 'uuid', 'date_creation', 'secret', 'stripe_id' ],
|
||||||
|
[ $uuid, $date_creation, $secret, $stripe_id ],
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
## WHEN
|
||||||
|
my $customer = $customer_dao->recover_by_uuid( uuid => $uuid );
|
||||||
|
|
||||||
|
## THEN
|
||||||
|
is $customer->uuid, $uuid, 'Uuid is recovered correctly';
|
||||||
|
is $customer->date_creation, $date_creation,
|
||||||
|
'Date creation is recovered correctly';
|
||||||
|
is $customer->secret, $secret, 'Secret is recovered correctly';
|
||||||
|
is $customer->stripe_id, $stripe_id, 'Secret is recovered correctly';
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user