72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
import sqlite3
|
|
|
|
class Users:
|
|
def __init__(self):
|
|
self.db_path = 'instance/dev.db'
|
|
self.create_table()
|
|
|
|
def create_table(self):
|
|
with sqlite3.connect(self.db_path) 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) 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) 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) 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 |