Adding initial project.

This commit is contained in:
sergiotarxz 2022-02-23 02:52:14 +01:00
parent bdc4eaffa9
commit caa066a5aa
6 changed files with 273 additions and 0 deletions

16
Build.PL Normal file
View File

@ -0,0 +1,16 @@
use Module::Build;
my $home = $ENV{HOME};
my $build = Module::Build->new(
module_name => 'Peace',
license => 'AGPLv3',
requires => {
'Mojolicious' => 0,
},
install_path => {
'templates' => "$HOME/.local/share/peace/template",
'public' => "$HOME/.local/share/peace/public",
}
);
$build->create_build_script;

24
lib/Peace.pm Normal file
View File

@ -0,0 +1,24 @@
package Peace;
use v5.30.0;
use strict;
use warnings;
use Mojo::Base 'Mojolicious';
sub startup {
my $self = shift;
my $home = $ENV{HOME};
# Load configuration from config file
my $config =
$self->plugin( JSONConfig => { file => "$home/.config/peace/peace.conf" } );
# Configure the application
$self->secrets( $config->{secrets} );
# Router
my $r = $self->routes;
$r->post('/users')->to('user#post');
}
1;

View File

@ -0,0 +1,10 @@
package Peace::Controller::User;
use Mojo::Base 'Mojolicious::Controller';
# Action
sub post {
my $self = shift;
my $user_data = $self->req->json;
$self->render( json => $user_data );
}
1;

45
lib/Peace/DAO/User.pm Normal file
View File

@ -0,0 +1,45 @@
package Peace::DAO::User;
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 => {
user => { type => InstanceOf['Peace::Model::User'] },
}
);
sub create {
my $self = shift;
my %params = $validator->(@_);
my $user = $params{user};
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;

143
lib/Peace/DB.pm Normal file
View File

@ -0,0 +1,143 @@
package Peace::DB;
use v5.30.0;
use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/HashRef/;
use DBI;
use Const::Fast;
my @migrations = (
'CREATE TABLE options (
key TEXT PRIMARY KEY,
value TEXT
);',
'CREATE TABLE users (
username TEXT PRIMARY KEY NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
date_creation TIMESTAMP NOT NULL DEFAULT NOW(),
last_connection TIMESTAMP NOT NULL DEFAULT NOW(),
verified boolean NOT NULL DEFAULT false
);',
'CREATE EXTENSION "pgcrypto";',
'CREATE TABLE countries (
id SERIAL NOT NULL,
code VARCHAR[5] NOT NULL UNIQUE,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY id
);',
q/CREATE TABLE addresses (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
username TEXT NOT NULL,
line1 TEXT NOT NULL,
line2 TEXT NOT NULL DEFAULT '',
city TEXT NOT NULL,
postal_code TEXT NOT NULL,
id_country INTEGER NOT NULL,
FOREIGN KEY (id_country) REFERENCES countries (id),
FOREIGN KEY (username) REFERENCES users (username)
);/,
q/CREATE TABLE categories (
id SERIAL NOT NULL,
name TEXT NOT NULL,
shortname TEXT NOT NULL,
image TEXT,
)/,
q/CREATE TABLE products (
ean TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
image TEXT,
price INTEGER NOT NULL,
id_category INTEGER NOT NULL,
FOREIGN KEY (id_category) REFERENCES categories (id_category)
);/,
);
{
my $validator = validation_for(
params => {
config => { type => HashRef }
}
);
sub dbh {
my %params = $validator->(@_);
my $config = $params{config};
my $db_config = $config->{db_config};
my $dbname = $db_config->{dbname} or die 'No dbnabe in db_config';
my $host = $db_config->{host} or die 'No host in db_config';
my $username = $db_config->{username} or die 'No username in db_config';
my $password = $db_config->{password} or die 'No password in db_config';
my $port = $db_config->{port} or die 'No port in db_config';
my $dsn =
"dbi:Pg:"
. ( defined $dbname ? "dbname=$dbname;" : "" )
. ( defined $host ? "host=$host;" : "" )
. ( defined $port ? "port=$port;" : "" );
my $dbh = DBI->connect_cached(
$dsn, $username, $password,
{
AutoCommit => 1,
RaiseError => 1,
sqlite_unicode => 1,
}
);
state $migrations_run = 0;
if ( !$migrations_run ) {
run_migrations($dbh);
$migrations_run = 1;
}
return $dbh;
}
}
sub run_migrations {
my $dbh = shift;
my $current_migration = _get_current_migration_number($dbh);
say $current_migration;
if ( $current_migration < scalar @migrations ) {
my @needed_migrations =
@migrations[ $current_migration .. $#migrations ];
for my $migration (@needed_migrations) {
$dbh->do($migration);
if (
!(
0 + $dbh->do(
'UPDATE options SET value = ? WHERE key = "migration"',
undef,
++$current_migration
)
)
)
{
$dbh->do(
'INSERT INTO options (key, value) VALUES ("migration", ?)',
undef, $current_migration
);
}
}
}
}
sub _get_current_migration_number {
my $dbh = shift;
local $dbh->{RaiseError} = 0;
my $migration = $dbh->selectrow_hashref( <<'EOF', {} );
SELECT value FROM options WHERE key = 'migration'
EOF
my $value = 0;
if ( defined $migration ) {
$value = $migration->{value};
}
return $value;
}
1;

35
lib/Peace/Model/User.pm Normal file
View File

@ -0,0 +1,35 @@
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;