idk WIP
This commit is contained in:
128
src/db/accounts.py
Normal file
128
src/db/accounts.py
Normal file
@ -0,0 +1,128 @@
|
||||
import json
|
||||
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# account create and delete
|
||||
|
||||
def create_account(
|
||||
conn: connection,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: int,
|
||||
metadata: dict
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.accounts
|
||||
(platform, login, password,
|
||||
metadata, created_at)
|
||||
values (%s, %s, %s, %s, now())
|
||||
returning id
|
||||
""",
|
||||
(platform, login, password, json.dumps(metadata)),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_account(
|
||||
conn: connection,
|
||||
account_id: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.accounts
|
||||
where account_id = %s
|
||||
""",
|
||||
(account_id),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# account checks
|
||||
|
||||
def check_account_existence_by_id(
|
||||
conn: connection,
|
||||
account_id: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.accounts
|
||||
where account_id = %s
|
||||
);
|
||||
""",
|
||||
(account_id),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
def check_account_existence(
|
||||
conn: connection,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.accounts
|
||||
where platform = %s, login = %s, password = %s
|
||||
);
|
||||
""",
|
||||
(platform, login, password),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
# account update
|
||||
|
||||
def update_account(
|
||||
conn: connection,
|
||||
account_id: str,
|
||||
platform: str,
|
||||
login: str,
|
||||
password: int,
|
||||
metadata: dict
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.accounts
|
||||
SET platform = %s,
|
||||
login = %s,
|
||||
password = %s,
|
||||
metadata = %s
|
||||
where account_id = %s
|
||||
""",
|
||||
(platform, login, password, json.dumps(metadata), account_id),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# account receiving
|
||||
|
||||
def get_account(
|
||||
conn: connection,
|
||||
account_id: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username,
|
||||
account_id, platform, login,
|
||||
password, metadata, created_at
|
||||
from picrinth.accounts
|
||||
where account_id = %s
|
||||
""",
|
||||
(account_id),
|
||||
)
|
||||
return cur.fetchone()
|
||||
Reference in New Issue
Block a user