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" : {
|
||||
"file" : "lib/Peace/DAO/Customer.pm"
|
||||
},
|
||||
"Peace::DAO::User" : {
|
||||
"file" : "lib/Peace/DAO/User.pm"
|
||||
},
|
||||
"Peace::DB" : {
|
||||
"file" : "lib/Peace/DB.pm"
|
||||
},
|
||||
"Peace::Model::Customer" : {
|
||||
"file" : "lib/Peace/Model/Customer.pm"
|
||||
},
|
||||
"Peace::Model::User" : {
|
||||
"file" : "lib/Peace/Model/User.pm"
|
||||
}
|
||||
},
|
||||
"release_status" : "stable",
|
||||
|
@ -6,7 +6,7 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
use Params::ValidationCompiler qw/validation_for/;
|
||||
use Types::Standard qw/HasMethods InstanceOf/;
|
||||
use Types::Standard qw/HasMethods InstanceOf Str/;
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
@ -31,6 +31,7 @@ use Types::Standard qw/HasMethods InstanceOf/;
|
||||
customer => { type => InstanceOf ['Peace::Model::Customer'] },
|
||||
}
|
||||
);
|
||||
|
||||
sub create {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
@ -39,9 +40,42 @@ use Types::Standard qw/HasMethods InstanceOf/;
|
||||
my $insert = <<'EOF';
|
||||
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
||||
EOF
|
||||
my $result = $dbh->selectrow_hashref($insert, undef, $customer->secret);
|
||||
my $result =
|
||||
$dbh->selectrow_hashref( $insert, undef, $customer->secret );
|
||||
my $uuid = $result->{uuid};
|
||||
$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 warnings;
|
||||
|
||||
use Test::Most tests => 2;
|
||||
use Test::Most tests => 6;
|
||||
|
||||
use DBI;
|
||||
use DateTime;
|
||||
|
||||
use Peace::Model::Customer;
|
||||
|
||||
@ -25,15 +26,50 @@ EOF
|
||||
|
||||
$dbh->{mock_add_resultset} = {
|
||||
sql => $sql,
|
||||
results => [
|
||||
[ 'uuid' ],
|
||||
[ $uuid ],
|
||||
]
|
||||
results => [ ['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
|
||||
$customer_dao->create( customer => $customer );
|
||||
|
||||
## THEN
|
||||
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