Refactor Database to its own folder
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from contextlib import contextmanager
|
||||
import duckdb
|
||||
import bcrypt
|
||||
from rich import print
|
||||
|
||||
|
||||
from lib import settings
|
||||
from models.user import UserIn, UserOut
|
||||
|
||||
|
||||
@contextmanager
|
||||
def get_db():
|
||||
try:
|
||||
conn = duckdb.connect(database=settings.db_url)
|
||||
yield conn
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def db_run(sql):
|
||||
with get_db() as conn:
|
||||
conn.execute(sql)
|
||||
|
||||
|
||||
def init_db():
|
||||
if settings.db_url.exists():
|
||||
# TODO: Development feature. Remove this in production
|
||||
from os import unlink
|
||||
|
||||
unlink(settings.db_url)
|
||||
# return
|
||||
sql = ""
|
||||
with open("lib/database/database.sql") as f:
|
||||
sql = f.read()
|
||||
|
||||
db_run(sql)
|
||||
|
||||
# Initialize default user
|
||||
|
||||
hashed_password = bcrypt.hashpw("password".encode("utf-8"), bcrypt.gensalt())
|
||||
|
||||
sql = f"INSERT INTO users (username, password, is_admin) VALUES ('admin', '{hashed_password.decode('utf-8')}', true)"
|
||||
db_run(sql)
|
||||
|
||||
sql = f"INSERT INTO users (username, password, is_admin) VALUES ('test', '{hashed_password.decode('utf-8')}', false)"
|
||||
db_run(sql)
|
||||
@@ -0,0 +1,50 @@
|
||||
CREATE TABLE users (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
email TEXT UNIQUE DEFAULT NULL,
|
||||
avatar_url TEXT DEFAULT 'https://api.dicebear.com/9.x/adventurer-neutral/svg?seed=Kingston' NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
is_admin BOOLEAN DEFAULT FALSE,
|
||||
last_login TIMESTAMP DEFAULT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE movies (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
imdb_id TEXT UNIQUE,
|
||||
actors TEXT,
|
||||
release_year INTEGER,
|
||||
plot TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE cards (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
point_value INTEGER CHECK (point_value BETWEEN 1 AND 3),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE scores (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
movie_id UUID,
|
||||
user_id UUID,
|
||||
FOREIGN KEY (movie_id) REFERENCES movies(id),
|
||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||
score INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE log (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
message TEXT NOT NULL,
|
||||
message_type TEXT NOT NULL,
|
||||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -0,0 +1,45 @@
|
||||
from models.user import UserIn, UserOut
|
||||
from lib.database.database import get_db
|
||||
|
||||
|
||||
def get_user_by_username(username) -> UserIn | None:
|
||||
sql = f"SELECT * FROM users WHERE username = '{username}'"
|
||||
with get_db() as conn:
|
||||
result = conn.execute(sql).fetchone()
|
||||
if result is not None:
|
||||
return UserIn(
|
||||
id=str(result[0]),
|
||||
username=result[1],
|
||||
password=result[2],
|
||||
email=result[3],
|
||||
avatar_url=result[4],
|
||||
is_active=result[5],
|
||||
is_admin=result[6],
|
||||
last_login=result[7],
|
||||
created_at=result[8],
|
||||
updated_at=result[9],
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_all_users() -> list[UserOut]:
|
||||
sql = "SELECT * FROM users"
|
||||
with get_db() as conn:
|
||||
result = conn.execute(sql).fetchall()
|
||||
users = []
|
||||
for row in result:
|
||||
user = UserOut(
|
||||
id=str(row[0]),
|
||||
username=row[1],
|
||||
email=row[3],
|
||||
avatar_url=row[4],
|
||||
is_active=row[5],
|
||||
is_admin=row[6],
|
||||
last_login=row[7],
|
||||
created_at=row[8],
|
||||
updated_at=row[9],
|
||||
)
|
||||
users.append(user)
|
||||
|
||||
return users
|
||||
Reference in New Issue
Block a user