Adding customer object.
This commit is contained in:
parent
f21aae07de
commit
7ffeac7bc7
5
Build.PL
5
Build.PL
@ -5,8 +5,13 @@ my $home = $ENV{HOME};
|
||||
my $build = Module::Build->new(
|
||||
module_name => 'Peace',
|
||||
license => 'AGPLv3',
|
||||
dist_author => 'Sergio Iglesias <sergiotarxz@posteo.net>',
|
||||
dist_abstract => 'Peace application shop.',
|
||||
requires => {
|
||||
'Mojolicious' => 0,
|
||||
'DBI' => 0,
|
||||
'DBD::Pg' => 0,
|
||||
'DBD::Mock' => 0,
|
||||
},
|
||||
install_path => {
|
||||
'templates' => "$HOME/.local/share/peace/template",
|
||||
|
52
MYMETA.json
Normal file
52
MYMETA.json
Normal file
@ -0,0 +1,52 @@
|
||||
{
|
||||
"abstract" : "Peace application shop.",
|
||||
"author" : [
|
||||
"Sergio Iglesias <sergiotarxz@posteo.net>"
|
||||
],
|
||||
"dynamic_config" : 0,
|
||||
"generated_by" : "Module::Build version 0.4231",
|
||||
"license" : [
|
||||
"unknown"
|
||||
],
|
||||
"meta-spec" : {
|
||||
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
|
||||
"version" : 2
|
||||
},
|
||||
"name" : "Peace",
|
||||
"prereqs" : {
|
||||
"runtime" : {
|
||||
"requires" : {
|
||||
"DBD::Mock" : "0",
|
||||
"DBD::Pg" : "0",
|
||||
"DBI" : "0",
|
||||
"Mojolicious" : "0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"provides" : {
|
||||
"Peace" : {
|
||||
"file" : "lib/Peace.pm"
|
||||
},
|
||||
"Peace::Controller::User" : {
|
||||
"file" : "lib/Peace/Controller/User.pm"
|
||||
},
|
||||
"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",
|
||||
"version" : 0,
|
||||
"x_serialization_backend" : "JSON::PP version 4.06"
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package Peace::DAO::User;
|
||||
package Peace::DAO::Customer;
|
||||
|
||||
use v5.30.0;
|
||||
|
||||
@ -28,13 +28,21 @@ use Types::Standard qw/HasMethods InstanceOf/;
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
user => { type => InstanceOf['Peace::Model::User'] },
|
||||
customer => { type => InstanceOf['Peace::Model::Customer'] },
|
||||
}
|
||||
);
|
||||
sub create {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $user = $params{user};
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,14 @@ my @migrations = (
|
||||
'CREATE EXTENSION "pgcrypto";',
|
||||
'CREATE TABLE customers (
|
||||
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
date_creation timestamp DEFAULT NOW(),
|
||||
secret TEXT NOT NULL,
|
||||
stripe_id TEXT NOT NULL,
|
||||
stripe_id TEXT,
|
||||
PRIMARY KEY (uuid)
|
||||
);',
|
||||
'CREATE TABLE developers (
|
||||
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
date_creation timestamp DEFAULT NOW(),
|
||||
name TEXT NOT NULL,
|
||||
surname TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
@ -35,6 +37,7 @@ my @migrations = (
|
||||
);',
|
||||
'CREATE TABLE applications (
|
||||
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
date_creation timestamp DEFAULT NOW(),
|
||||
name TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
developer UUID NOT NULL,
|
||||
@ -62,10 +65,10 @@ my @migrations = (
|
||||
FOREIGN KEY (release) REFERENCES releases (uuid)
|
||||
);',
|
||||
'CREATE TABLE purchases (
|
||||
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
customer UUID NOT NULL,
|
||||
application UUID NOT NULL,
|
||||
PRIMARY KEY (uuid),
|
||||
date timestamp DEFAULT NOW(),
|
||||
PRIMARY KEY (customer, application),
|
||||
FOREIGN KEY (application) REFERENCES applications (uuid),
|
||||
FOREIGN KEY (customer) REFERENCES customers (uuid)
|
||||
);',
|
||||
|
86
lib/Peace/Model/Customer.pm
Normal file
86
lib/Peace/Model/Customer.pm
Normal file
@ -0,0 +1,86 @@
|
||||
package Peace::Model::Customer;
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Params::ValidationCompiler qw/validation_for/;
|
||||
use Types::Standard qw/Str InstanceOf Bool/;
|
||||
|
||||
use DateTime;
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
uuid => { type => Str, optional => 1 },
|
||||
date_creation => { type => InstanceOf ['DateTime'], optional => 1 },
|
||||
secret => { type => Str },
|
||||
stripe_id => { type => Str, optional => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $self = bless {%params}, $class;
|
||||
return $self;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator =
|
||||
validation_for( params => [ { type => Str, optional => 1 } ] );
|
||||
|
||||
sub uuid {
|
||||
my $self = shift;
|
||||
if (@_) {
|
||||
my ($new_uuid) = $validator->(@_);
|
||||
$self->{uuid} = $new_uuid;
|
||||
}
|
||||
return $self->{uuid};
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator =
|
||||
validation_for(
|
||||
params => [ { type => InstanceOf ['DateTime'], optional => 1 } ] );
|
||||
|
||||
sub date_creation {
|
||||
my $self = shift;
|
||||
if (@_) {
|
||||
my ($new_date_creation) = $validator->(@_);
|
||||
$self->{date_creation} = $new_date_creation;
|
||||
}
|
||||
return $self->{date_creation};
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator =
|
||||
validation_for( params => [ { type => Str, optional => 1 } ] );
|
||||
|
||||
sub secret {
|
||||
my $self = shift;
|
||||
if (@_) {
|
||||
my ($new_secret) = $validator->(@_);
|
||||
$self->{secret} = $new_secret;
|
||||
}
|
||||
return $self->{secret};
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator =
|
||||
validation_for( params => [ { type => Str, optional => 1 } ] );
|
||||
|
||||
sub stripe_id {
|
||||
my $self = shift;
|
||||
if (@_) {
|
||||
my ($new_stripe_id) = $validator->(@_);
|
||||
$self->{stripe_id} = $new_stripe_id;
|
||||
}
|
||||
return $self->{stripe_id};
|
||||
}
|
||||
}
|
||||
1;
|
@ -1,35 +0,0 @@
|
||||
package Peace::Model::User;
|
||||
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Params::ValidationCompiler qw/validation_for/;
|
||||
use Types::Standard qw/Str InstanceOf Bool/;
|
||||
|
||||
use DateTime;
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
username => { type => Str },
|
||||
firstname => { type => Str },
|
||||
lastname => { type => Str },
|
||||
email => { type => Str },
|
||||
password => { type => Str },
|
||||
date_creation => { type => InstanceOf ['DateTime'], optional => 1 },
|
||||
last_connection =>
|
||||
{ type => InstanceOf ['DateTime'], optional => 1 },
|
||||
verified => { type => Bool },
|
||||
}
|
||||
);
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $self = bless {%params}, $class;
|
||||
return $self;
|
||||
}
|
||||
}
|
||||
1;
|
35
t/00001-customer-model.t
Normal file
35
t/00001-customer-model.t
Normal file
@ -0,0 +1,35 @@
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 5;
|
||||
|
||||
BEGIN {
|
||||
use_ok 'Peace::Model::Customer';
|
||||
}
|
||||
|
||||
{
|
||||
## GIVEN
|
||||
my $secret = 'hola';
|
||||
## WHEN
|
||||
my $customer = Peace::Model::Customer->new( secret => $secret );
|
||||
## THEN
|
||||
ok $customer->isa('Peace::Model::Customer'),
|
||||
'Instanced customer is made of Peace::Model::Customer.';
|
||||
is $customer->secret, $secret, 'Secret is correctly setup';
|
||||
is $customer->uuid, undef, 'Uuid is undef.';
|
||||
}
|
||||
|
||||
{
|
||||
## GIVEN
|
||||
my $secret = 'hola';
|
||||
my $uuid = 'example';
|
||||
my $customer = Peace::Model::Customer->new( secret => $secret );
|
||||
|
||||
## WHEN
|
||||
$customer->uuid($uuid);
|
||||
|
||||
## THEN
|
||||
is $customer->uuid, $uuid, 'Uuid can be set.';
|
||||
}
|
39
t/00002-customer-dao.t
Normal file
39
t/00002-customer-dao.t
Normal file
@ -0,0 +1,39 @@
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 2;
|
||||
|
||||
use DBI;
|
||||
|
||||
use Peace::Model::Customer;
|
||||
|
||||
BEGIN {
|
||||
use_ok 'Peace::DAO::Customer';
|
||||
}
|
||||
|
||||
{
|
||||
## GIVEN
|
||||
my $sql = <<'EOF';
|
||||
INSERT INTO customers (secret) VALUES (?) RETURNING uuid;
|
||||
EOF
|
||||
my $uuid = 'hola';
|
||||
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
||||
my $customer = Peace::Model::Customer->new( secret => 'hola' );
|
||||
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
||||
|
||||
$dbh->{mock_add_resultset} = {
|
||||
sql => $sql,
|
||||
results => [
|
||||
[ 'uuid' ],
|
||||
[ $uuid ],
|
||||
]
|
||||
};
|
||||
|
||||
## WHEN
|
||||
$customer_dao->create( customer => $customer );
|
||||
|
||||
## THEN
|
||||
is $customer->uuid, $uuid, 'Customer id correctly set after user creation.';
|
||||
}
|
10
t/00003-db.t
Normal file
10
t/00003-db.t
Normal file
@ -0,0 +1,10 @@
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 1;
|
||||
|
||||
BEGIN {
|
||||
use_ok 'Peace::DB';
|
||||
}
|
10
t/00004-peace.t
Normal file
10
t/00004-peace.t
Normal file
@ -0,0 +1,10 @@
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 1;
|
||||
|
||||
BEGIN {
|
||||
use_ok 'Peace';
|
||||
}
|
Loading…
Reference in New Issue
Block a user