package Peace::DAO::Application; use v5.30.0; use strict; use warnings; use Data::Dumper; use Params::ValidationCompiler qw/validation_for/; use Types::Standard qw/HasMethods InstanceOf Str HashRef/; { 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, flatpak_repo, 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->flatpak_repo, $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 => { developer => { type => InstanceOf ['Peace::Model::Developer'], optional => 1 }, } ); sub recover_by_developer { my $self = shift; my %params = $validator->(@_); my $developer = $params{developer}; my $developer_uuid = $developer->uuid; my $dbh = $self->_dbh; if ( !defined $developer_uuid ) { die "Developer has no uuid yet when recovering applications."; } my $result = $dbh->selectall_arrayref( <<'EOF', { Slice => {} }, $developer->uuid ); SELECT uuid, date_creation, name, description, url, developer, price, git_repo, flatpak_builder_file, flatpak_repo, verified FROM applications WHERE developer = ?; EOF $result = [ map { $self->_select_result_to_application($_) } @$result ]; return $result; } } { 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, flatpak_repo, verified FROM applications WHERE uuid = ? EOF if ( !defined $result ) { die "Application with uuid $uuid not found."; } return $self->_select_result_to_application($result); } } { my $validator = validation_for( params => [ { type => HashRef }, ] ); sub _select_result_to_application { my $self = shift; my ($result) = $validator->(@_); 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; =encoding utf8 =head1 NAME Peace::DAO::Application - The database access object of applications. =head1 SYNOPSIS my $application_dao = Peace::DAO::Application->new( dbh => $dbh, ); $application_dao->create( application => $application ); my $application = $application_dao->recover_by_uuid( uuid => $uuid); my $applications = $application_dao->recover_by_developer( developer => $developer ); =head1 DESCRIPTION Peace::DAO::Application allows you to make database operations over the applications table like recover or create. =head1 INSTANCE METHODS Peace::DAO::Application implements the following instance methods: =head2 new my $application_dao = Peace::DAO::Application->new( dbh => $dbh, ); Instances a Peace::DAO::Application object. =head1 METHODS Peace::DAO::Application implements the following methods: =head2 create $application_dao->create( application => $application ); Takes a L and creates its representation in database. As a side effect sets the fields uuid and date_creation in the passed object. =head2 recover_by_uuid my $application = $application_dao->recover_by_uuid( uuid => $uuid, ); Recovers from database a L from its uuid. =head2 recover_by_developer my $applications = $application_dao->recover_by_developer( developer => $developer, ); Recovers a arrayref of L from its L. =head1 SEE ALSO L, L, L