2022-03-14 19:14:05 +01:00
|
|
|
package Peace::DAO::Customer;
|
2022-02-23 02:52:14 +01:00
|
|
|
|
|
|
|
use v5.30.0;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Params::ValidationCompiler qw/validation_for/;
|
2022-03-14 19:47:22 +01:00
|
|
|
use Types::Standard qw/HasMethods InstanceOf Str/;
|
2022-02-23 02:52:14 +01:00
|
|
|
|
2022-03-15 02:45:16 +01:00
|
|
|
use DateTime::Format::Pg;
|
|
|
|
|
2022-02-23 02:52:14 +01:00
|
|
|
{
|
|
|
|
my $validator = validation_for(
|
|
|
|
params => {
|
|
|
|
dbh => { type => HasMethods ['selectall_arrayref'] },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my %params = $validator->(@_);
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->{dbh} = $params{dbh};
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
my $validator = validation_for(
|
|
|
|
params => {
|
2022-03-14 19:47:22 +01:00
|
|
|
customer => { type => InstanceOf ['Peace::Model::Customer'] },
|
2022-02-23 02:52:14 +01:00
|
|
|
}
|
|
|
|
);
|
2022-03-14 19:47:22 +01:00
|
|
|
|
2022-02-23 02:52:14 +01:00
|
|
|
sub create {
|
2022-03-14 19:47:22 +01:00
|
|
|
my $self = shift;
|
|
|
|
my %params = $validator->(@_);
|
2022-03-14 19:14:05 +01:00
|
|
|
my $customer = $params{customer};
|
2022-03-14 19:47:22 +01:00
|
|
|
my $dbh = $self->_dbh;
|
|
|
|
my $insert = <<'EOF';
|
2022-03-15 02:45:16 +01:00
|
|
|
INSERT INTO customers (secret_bcrypt) VALUES (?) RETURNING uuid;
|
2022-03-14 19:14:05 +01:00
|
|
|
EOF
|
2022-03-14 19:47:22 +01:00
|
|
|
my $result =
|
2022-03-15 02:45:16 +01:00
|
|
|
$dbh->selectrow_hashref( $insert, undef, $customer->secret_bcrypt );
|
2022-03-14 19:47:22 +01:00
|
|
|
my $uuid = $result->{uuid};
|
|
|
|
$customer->uuid($uuid);
|
|
|
|
my $new_customer = $self->recover_by_uuid( uuid => $uuid );
|
|
|
|
$customer->date_creation( $new_customer->date_creation )
|
|
|
|
if defined $new_customer->date_creation;
|
|
|
|
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 );
|
2022-03-15 02:45:16 +01:00
|
|
|
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?;
|
2022-03-14 19:47:22 +01:00
|
|
|
EOF
|
2022-03-15 02:45:16 +01:00
|
|
|
if ( exists $result->{date_creation} ) {
|
|
|
|
my $iso8601 = DateTime::Format::Pg->new;
|
|
|
|
$result->{date_creation} =
|
|
|
|
$iso8601->parse_datetime( $result->{date_creation} );
|
|
|
|
}
|
2022-03-14 19:47:22 +01:00
|
|
|
for my $key ( keys %$result ) {
|
|
|
|
delete $result->{$key} unless defined $result->{$key};
|
|
|
|
}
|
|
|
|
my $customer = Peace::Model::Customer->new(%$result);
|
|
|
|
return $customer;
|
2022-02-23 02:52:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _dbh {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{dbh};
|
|
|
|
}
|
|
|
|
1;
|
2022-03-18 07:27:55 +01:00
|
|
|
=encoding utf8
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Peace::DAO::Customer - The database access object of customer.
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
my $customer_dao = Peace::DAO::Customer->new(
|
|
|
|
dbh => $dbh,
|
|
|
|
);
|
|
|
|
|
|
|
|
$customer_dao->create( customer => $customer );
|
|
|
|
|
|
|
|
my $customer = $customer_dao->recover_by_uuid(
|
|
|
|
uuid => $uuid,
|
|
|
|
);
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Peace::DAO::Customer allows you to make database
|
|
|
|
operations over the customers table like recover
|
|
|
|
or create.
|
|
|
|
|
|
|
|
=head1 INSTANCE METHODS
|
|
|
|
|
|
|
|
Peace::DAO::Customer implements the following instance
|
|
|
|
methods:
|
|
|
|
|
|
|
|
=head2 new
|
|
|
|
|
|
|
|
my $customer_dao = Peace::DAO::Customer->new(
|
|
|
|
dbh => $dbh,
|
|
|
|
);
|
|
|
|
|
|
|
|
Takes a database handle and instances a Peace::DAO::Customer object.
|
|
|
|
|
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
=head2 create
|
|
|
|
|
|
|
|
$customer_dao->create(
|
|
|
|
customer => $customer,
|
|
|
|
);
|
|
|
|
|
|
|
|
Takes a L<Peace::Model::Customer> object and creates its
|
|
|
|
database representation.
|
|
|
|
|
|
|
|
As a side effect sets the fields uuid and date_creation
|
|
|
|
in the passed object.
|
|
|
|
|
|
|
|
=head2 recover_by_uuid
|
|
|
|
|
|
|
|
my $customer = $customer_dao->recover_by_uuid(
|
|
|
|
uuid => $uuid,
|
|
|
|
);
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
L<Peace::DB>, L<DBI>, L<Peace::Model::Customer>
|