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/; { 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) VALUES (?) RETURNING uuid; EOF 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; } } sub _dbh { my $self = shift; return $self->{dbh}; } 1;