Peace/lib/Peace/DAO/Customer.pm

54 lines
1.1 KiB
Perl

package Peace::DAO::Customer;
use v5.30.0;
use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/HasMethods InstanceOf/;
{
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);
return $customer;
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;