import sqlite3 import os class Users: def __init__(self): # Allow DB path override via env, default to /app/instance/dev.db inside container default_path = os.getenv("DB_PATH", "instance/dev.db") self.db_path = os.path.abspath(default_path) os.makedirs(os.path.dirname(self.db_path), exist_ok=True) self.create_table() def create_table(self): with sqlite3.connect(self.db_path, check_same_thread=False) as conn: cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, status TEXT NOT NULL DEFAULT 'active' ); """) conn.commit() def insert_user(self, email, password: str) -> bool: try: with sqlite3.connect(self.db_path, check_same_thread=False) as conn: cursor = conn.cursor() cursor.execute(""" INSERT INTO users (email, password) VALUES (?, ?) """, (email, password)) conn.commit() return True except sqlite3.IntegrityError: return False def get_user_by_id(self, id): with sqlite3.connect(self.db_path, check_same_thread=False) as conn: cursor = conn.cursor() cursor.execute(""" SELECT * FROM users WHERE id = ? """, (id,)) row = cursor.fetchone() if row: return { "id": row[0], "email": row[1], "password": row[2], "created_at": row[3], "status":row[4], } else: return None def get_user_by_email(self, email): with sqlite3.connect(self.db_path, check_same_thread=False) as conn: cursor = conn.cursor() cursor.execute(""" SELECT * FROM users WHERE email = ? """, (email,)) row = cursor.fetchone() if row: return { "id": row[0], "email": row[1], "password": row[2], "created_at": row[3], "status":row[4], } else: return None