Rename helper to SQL

This commit is contained in:
Citlali del Rey 2023-02-22 20:39:54 -08:00
parent 019e7eddff
commit 807ae8b0d3
Signed by: nullobsi
GPG Key ID: 933A1F44222C2634
3 changed files with 20 additions and 20 deletions

View File

@ -34,16 +34,16 @@ sub startup($self) {
die "Only postgres is supported";
}
$self->helper(pg => sub ($app) {
state $pg = Mojo::Pg->new($config->{'database'}->{connection});
$self->helper(sql => sub ($app) {
state $sql = Mojo::Pg->new($config->{'database'}->{connection});
});
$self->helper(pins => sub ($app) {
state $pins = IpfsUpload::Model::Pins->new(pg => $app->pg);
state $pins = IpfsUpload::Model::Pins->new(sql => $app->sql);
});
$self->helper(users => sub ($app) {
state $users = IpfsUpload::Model::Users->new(pg => $app->pg);
state $users = IpfsUpload::Model::Users->new(sql => $app->sql);
});
# Configure the application

View File

@ -5,32 +5,32 @@ use experimental q/signatures/;
use Mojo::Base -base, -signatures;
has 'pg';
has 'sql';
sub add($self, $pin) {
return $self->pg->db->insert_p('pins', $pin, {returning => ['id', 'created_at', 'cid']})->then(sub ($res) {
return $self->sql->db->insert_p('pins', $pin, {returning => ['id', 'created_at', 'cid']})->then(sub ($res) {
return $res->hash;
});
}
sub del($self, $where) {
return $self->pg->db->delete_p('pins', $where);
return $self->sql->db->delete_p('pins', $where);
}
sub get($self, $where) {
return $self->pg->db->select_p('pins', '*', $where, {limit => 1})->then(sub ($res) {
return $self->sql->db->select_p('pins', '*', $where, {limit => 1})->then(sub ($res) {
return $res->hash;
});
}
sub exists($self, $where) {
return $self->pg->db->select_p('pins', 'id', $where, {limit => 1})->then(sub ($res) {
return $self->sql->db->select_p('pins', 'id', $where, {limit => 1})->then(sub ($res) {
return $res->rows == 1;
});
}
sub update($self, $update, $where) {
return $self->pg->db->update_p('pins', $update, $where, { returning => ['cid', 'id']})->then(sub ($res) {
return $self->sql->db->update_p('pins', $update, $where, { returning => ['cid', 'id']})->then(sub ($res) {
return $res->hash;
});
}
@ -40,13 +40,13 @@ sub cid_count($self, $cid) {
}
sub count($self, $where) {
return $self->pg->db->select_p('pins', 'count(*)', $where)->then(sub ($res) {
return $self->sql->db->select_p('pins', 'count(*)', $where)->then(sub ($res) {
return $res->hash->{count};
});
}
sub list($self, $where, $limit) {
return $self->pg->db->select_p('pins', '*', $where, {order_by => {-desc => 'created_at'}, limit => $limit})->then(sub ($res) {
return $self->sql->db->select_p('pins', '*', $where, {order_by => {-desc => 'created_at'}, limit => $limit})->then(sub ($res) {
return $res->hashes;
});
}

View File

@ -6,14 +6,14 @@ use Crypt::Random qw( makerandom_octet );
use Mojo::Base -base, -signatures;
has 'pg';
has 'sql';
sub token_info($self, $token) {
return $self->pg->db->select('access_token', ['uid', 'app_name'], { token => $token })->hash;
return $self->sql->db->select('access_token', ['uid', 'app_name'], { token => $token })->hash;
}
sub token_info_p($self, $token) {
return $self->pg->db->select_p('access_token', ['uid', 'app_name', 'id'], {
return $self->sql->db->select_p('access_token', ['uid', 'app_name', 'id'], {
token => $token,
})->then(sub ($res) {
return $res->hash;
@ -26,7 +26,7 @@ sub gen_token($self, $uid, $app_name) {
my $s = unpack "H*", pack "B*", '0' x ( $size%8 ? 8-$size % 8 : 0 ).
unpack "b$size", $r;
return $self->pg->db->insert_p(
return $self->sql->db->insert_p(
'access_token',
{
uid => $uid,
@ -40,7 +40,7 @@ sub gen_token($self, $uid, $app_name) {
}
sub del_token($self, $uid, $id) {
return $self->pg->db->delete_p(
return $self->sql->db->delete_p(
'access_token',
{
uid => $uid,
@ -50,7 +50,7 @@ sub del_token($self, $uid, $id) {
}
sub list_tokens($self, $uid) {
return $self->pg->db->select_p('access_token', ['uid', 'app_name', 'id'], {
return $self->sql->db->select_p('access_token', ['uid', 'app_name', 'id'], {
uid => $uid,
})->then(sub ($res) {
return $res->hashes;
@ -58,11 +58,11 @@ sub list_tokens($self, $uid) {
}
sub getOrMake($self, $username) {
return $self->pg->db->select_p('users', ['uid'], {username => $username})->then(sub ($res) {
return $self->sql->db->select_p('users', ['uid'], {username => $username})->then(sub ($res) {
if ($res->rows != 0) {
return $res->hash->{uid};
}
return $self->pg->db->insert_p('users', {username => $username}, {returning => 'uid'})->then(sub ($n) {
return $self->sql->db->insert_p('users', {username => $username}, {returning => 'uid'})->then(sub ($n) {
return $n->hash->{uid};
});
});