package Peace::DAO::Customer; use v5.30.0; use strict; use warnings; use Params::ValidationCompiler qw/validation_for/; use Types::Standard qw/HasMethods InstanceOf Str/; use DateTime::Format::Pg; { 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 => { customer => { type => InstanceOf ['Peace::Model::Customer'] }, } ); sub create { my $self = shift; my %params = $validator->(@_); my $customer = $params{customer}; my $dbh = $self->_dbh; my $insert = <<'EOF'; INSERT INTO customers (secret_bcrypt) VALUES (?) RETURNING uuid; EOF my $result = $dbh->selectrow_hashref( $insert, undef, $customer->secret_bcrypt ); 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 ); SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?; EOF if ( exists $result->{date_creation} ) { my $iso8601 = DateTime::Format::Pg->new; $result->{date_creation} = $iso8601->parse_datetime( $result->{date_creation} ); } for my $key ( keys %$result ) { delete $result->{$key} unless defined $result->{$key}; } my $customer = Peace::Model::Customer->new(%$result); return $customer; } } sub _dbh { my $self = shift; return $self->{dbh}; } 1; =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 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, L, L