users table endpoints. auth to fix
This commit is contained in:
44
src/db/internal.py
Normal file
44
src/db/internal.py
Normal file
@ -0,0 +1,44 @@
|
||||
import sys
|
||||
|
||||
import psycopg2
|
||||
from loguru import logger
|
||||
|
||||
from db.models import database
|
||||
from settings import settings
|
||||
|
||||
|
||||
def connect_db():
|
||||
logger.info("Initializing DB connection")
|
||||
try:
|
||||
database.conn = psycopg2.connect(
|
||||
dbname=settings.db_name,
|
||||
user=settings.db_user,
|
||||
password=settings.db_password,
|
||||
host=settings.db_host,
|
||||
port=settings.db_port,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize DB connection: {e}")
|
||||
sys.exit(1)
|
||||
logger.success("Successfully initialized DB connection")
|
||||
|
||||
|
||||
def disconnect_db():
|
||||
logger.info("Closing DB connection")
|
||||
if database.conn:
|
||||
try:
|
||||
database.conn.close()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to disconnect from DB: {e}")
|
||||
return
|
||||
else:
|
||||
logger.error("Failed to disconnect from DB: no connection")
|
||||
logger.success("Successfully closed DB connection")
|
||||
|
||||
|
||||
def get_db_connection():
|
||||
if database.conn is not None:
|
||||
yield database.conn
|
||||
else:
|
||||
logger.error("No connection pool")
|
||||
sys.exit(1)
|
||||
8
src/db/models.py
Normal file
8
src/db/models.py
Normal file
@ -0,0 +1,8 @@
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
|
||||
class DataBase:
|
||||
conn: connection | None = None
|
||||
|
||||
|
||||
database = DataBase()
|
||||
157
src/db/users.py
Normal file
157
src/db/users.py
Normal file
@ -0,0 +1,157 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# user create and delete
|
||||
|
||||
def create_user(
|
||||
conn: connection,
|
||||
username: str,
|
||||
password: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.users
|
||||
(username, password, disabled, created_at)
|
||||
values (%s, %s, false, now())
|
||||
""",
|
||||
(username, password),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_user(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# user checks
|
||||
|
||||
def check_user_existence(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
);
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def check_user_disabled(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select disabled
|
||||
from picrinth.users
|
||||
where username = %s;
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
|
||||
# user updates
|
||||
|
||||
def update_user_password(
|
||||
conn: connection,
|
||||
username: str,
|
||||
password: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set password = %s
|
||||
where username = %s
|
||||
""",
|
||||
(password, username),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def update_user_username(
|
||||
conn: connection,
|
||||
username: str,
|
||||
newUsername: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set username = %s
|
||||
where username = %s;
|
||||
""",
|
||||
(newUsername, username),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def update_user_last_seen(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set last_seen_at = now()
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
# user receiving
|
||||
|
||||
def get_user(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username, password, disabled,
|
||||
groups_ids, last_seen_at, created_at
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def get_user_password(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select password
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
Reference in New Issue
Block a user