From ec313dada3f4a942eb5e4116591ed193937ad0b7 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Wed, 16 Mar 2022 17:05:10 +0100 Subject: [PATCH] Adding a database access object for application with options to create and fetch applications. --- db_tests/00003-application-dao.t | 58 +++++++++++++++++ lib/Peace/DAO/Application.pm | 107 +++++++++++++++++++++++++++++++ lib/Peace/DAO/Developer.pm | 2 +- t/00007-application-model.t | 6 +- 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 db_tests/00003-application-dao.t create mode 100644 lib/Peace/DAO/Application.pm diff --git a/db_tests/00003-application-dao.t b/db_tests/00003-application-dao.t new file mode 100644 index 0000000..e9c1699 --- /dev/null +++ b/db_tests/00003-application-dao.t @@ -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.'; +} diff --git a/lib/Peace/DAO/Application.pm b/lib/Peace/DAO/Application.pm new file mode 100644 index 0000000..559d860 --- /dev/null +++ b/lib/Peace/DAO/Application.pm @@ -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; diff --git a/lib/Peace/DAO/Developer.pm b/lib/Peace/DAO/Developer.pm index faf8eda..1a12d9a 100644 --- a/lib/Peace/DAO/Developer.pm +++ b/lib/Peace/DAO/Developer.pm @@ -13,7 +13,7 @@ use DateTime::Format::Pg; { my $validator = validation_for( params => { - dbh => { type => HasMethods ['selectall_arrayref'] }, + dbh => { type => HasMethods ['selectrow_hashref'] }, } ); diff --git a/t/00007-application-model.t b/t/00007-application-model.t index 4505b5d..c2e863f 100644 --- a/t/00007-application-model.t +++ b/t/00007-application-model.t @@ -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, );