Adding a database access object for application with options
to create and fetch applications.
This commit is contained in:
parent
b869ac5694
commit
ec313dada3
58
db_tests/00003-application-dao.t
Normal file
58
db_tests/00003-application-dao.t
Normal file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::Most tests => 2;
|
||||
|
||||
use DateTime;
|
||||
|
||||
use Peace;
|
||||
use Peace::DB;
|
||||
use Peace::Model::Developer;
|
||||
use Peace::DAO::Developer;
|
||||
use Peace::Model::Application;
|
||||
use Peace::DAO::Application;
|
||||
|
||||
{
|
||||
## GIVEN
|
||||
my $current_date = DateTime->now;
|
||||
my $peace = Peace->new;
|
||||
my $home = $ENV{HOME};
|
||||
my $config =
|
||||
$peace->plugin(
|
||||
JSONConfig => { file => "$home/.config/peace/peace.conf" } );
|
||||
my $dbh = Peace::DB->dbh( config => $config );
|
||||
my $secret_bcrypt = 'hola';
|
||||
my $developer = Peace::Model::Developer->new(
|
||||
name => 'Larry',
|
||||
surname => 'Wall',
|
||||
email => 'larry@perl.org',
|
||||
country => 'US',
|
||||
verified => 0,
|
||||
secret_bcrypt => $secret_bcrypt
|
||||
);
|
||||
my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh );
|
||||
$developer_dao->create( developer => $developer );
|
||||
my $application = Peace::Model::Application->new(
|
||||
name => 'Desfronificator',
|
||||
description => 'Desfronifies the files.',
|
||||
url => 'desfronificator.example.com',
|
||||
developer => $developer,
|
||||
price => 0,
|
||||
flatpak_builder_file => './resources/com.example.desfronificator.yml',
|
||||
git_repo =>
|
||||
'https://git.desfronificator.example.com/larry/desfronificator.git',
|
||||
verified => 0,
|
||||
);
|
||||
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
|
||||
|
||||
## WHEN
|
||||
$application_dao->create( application => $application );
|
||||
|
||||
## THEN
|
||||
ok $application->uuid, 'Generated uuid.';
|
||||
ok $application->date_creation > $current_date, 'The date is recent.';
|
||||
}
|
107
lib/Peace/DAO/Application.pm
Normal file
107
lib/Peace/DAO/Application.pm
Normal file
@ -0,0 +1,107 @@
|
||||
package Peace::DAO::Application;
|
||||
|
||||
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 ['selectrow_hashref'], optional => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my %params = $validator->(@_);
|
||||
|
||||
my $self = bless {}, $class;
|
||||
$self->{dbh} = $params{dbh};
|
||||
return $self;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
application => { type => InstanceOf ['Peace::Model::Application'] },
|
||||
}
|
||||
);
|
||||
|
||||
sub create {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $application = $params{application};
|
||||
my $dbh = $self->_dbh;
|
||||
|
||||
my $insert = <<'EOF';
|
||||
INSERT INTO applications
|
||||
(name, description, url,
|
||||
developer, price, git_repo,
|
||||
flatpak_builder_file, verified)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING uuid;
|
||||
EOF
|
||||
my $result = $dbh->selectrow_hashref(
|
||||
$insert, undef,
|
||||
$application->name, $application->description,
|
||||
$application->url, $application->developer->uuid,
|
||||
$application->price, $application->git_repo,
|
||||
$application->flatpak_builder_file, $application->verified
|
||||
);
|
||||
my $uuid = $result->{uuid};
|
||||
my $new_application = $self->recover_by_uuid( uuid => $uuid );
|
||||
|
||||
$application->uuid( $new_application->uuid );
|
||||
$application->date_creation( $new_application->date_creation );
|
||||
|
||||
return $application;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
uuid => { type => Str },
|
||||
}
|
||||
);
|
||||
|
||||
sub recover_by_uuid {
|
||||
my $self = shift;
|
||||
my %params = $validator->(@_);
|
||||
my $dbh = $self->_dbh;
|
||||
my $uuid = $params{uuid};
|
||||
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
|
||||
SELECT uuid, date_creation, name,
|
||||
description, url, developer,
|
||||
price, git_repo, flatpak_builder_file,
|
||||
verified
|
||||
FROM applications
|
||||
WHERE uuid = ?
|
||||
EOF
|
||||
if ( !defined $result ) {
|
||||
die "Application with uuid $uuid not found.";
|
||||
}
|
||||
if ( exists $result->{developer} ) {
|
||||
$result->{developer_uuid} = delete $result->{developer};
|
||||
$result->{dbh} = $self->_dbh;
|
||||
}
|
||||
if ( exists $result->{date_creation} ) {
|
||||
my $iso8601 = DateTime::Format::Pg->new;
|
||||
$result->{date_creation} =
|
||||
$iso8601->parse_datetime( $result->{date_creation} );
|
||||
}
|
||||
my $application = Peace::Model::Application->new(%$result);
|
||||
return $application;
|
||||
}
|
||||
}
|
||||
|
||||
sub _dbh {
|
||||
my $self = shift;
|
||||
return $self->{dbh};
|
||||
}
|
||||
1;
|
@ -13,7 +13,7 @@ use DateTime::Format::Pg;
|
||||
{
|
||||
my $validator = validation_for(
|
||||
params => {
|
||||
dbh => { type => HasMethods ['selectall_arrayref'] },
|
||||
dbh => { type => HasMethods ['selectrow_hashref'] },
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -29,8 +29,9 @@ BEGIN {
|
||||
### Application data
|
||||
my $app_name = 'Desfronificator';
|
||||
my $app_description = 'Desfronifies the files.';
|
||||
my $app_url = 'desfronificator.example.com';
|
||||
my $app_url = 'https://desfronificator.example.com';
|
||||
my $app_price = '4.20';
|
||||
my $app_git_repo = 'https://git.desfronificator.example.com/larry/desfronificator.git';
|
||||
my $app_flatpak_builder_file =
|
||||
'./resources/com.example.desfronificator.yml';
|
||||
my $app_verified = 0;
|
||||
@ -52,6 +53,7 @@ BEGIN {
|
||||
price => $app_price,
|
||||
flatpak_builder_file => $app_flatpak_builder_file,
|
||||
verified => $app_verified,
|
||||
git_repo => $app_git_repo,
|
||||
);
|
||||
|
||||
## THEN
|
||||
@ -74,6 +76,7 @@ BEGIN {
|
||||
my $app_description = 'Desfronifies the files.';
|
||||
my $app_url = 'desfronificator.example.com';
|
||||
my $app_price = '4.20';
|
||||
my $app_git_repo = 'https://git.desfronificator.example.com/larry/desfronificator.git';
|
||||
my $app_flatpak_builder_file =
|
||||
'./resources/com.example.desfronificator.yml';
|
||||
my $app_verified = 0;
|
||||
@ -103,6 +106,7 @@ BEGIN {
|
||||
price => $app_price,
|
||||
flatpak_builder_file => $app_flatpak_builder_file,
|
||||
verified => $app_verified,
|
||||
git_repo => $app_git_repo,
|
||||
dbh => $dbh,
|
||||
);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user