2024-01-12 16:53:03 +01:00
|
|
|
from datetime import datetime
|
2024-01-12 10:57:58 +01:00
|
|
|
import hashlib
|
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def con3():
|
|
|
|
|
conn = sqlite3.connect('db/db.sqlite')
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
def create_user(name: str, email: str, password: str):
|
|
|
|
|
password = hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
now = datetime.now().isoformat()
|
|
|
|
|
query = (f"INSERT INTO users (name, email, password, created_at, updated_at) VALUES ('{name}', '{email}', "
|
|
|
|
|
f"'{password}', '{now}', '{now}');")
|
2024-01-12 10:57:58 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
2024-01-12 16:53:03 +01:00
|
|
|
def get_user(id: int):
|
|
|
|
|
query = f"SELECT * FROM users WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
user = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_by_email(email: str):
|
|
|
|
|
query = f"SELECT * FROM users WHERE email = '{email}';"
|
2024-01-12 10:57:58 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
user = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return user
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_habit(name: str, times: int, note: str | None=None):
|
|
|
|
|
now = datetime.now().isoformat()
|
|
|
|
|
query = (f"INSERT INTO habits (name, note, times, created_at, updated_at) VALUES ('{name}', '{note}', "
|
|
|
|
|
f"'{times}', '{now}', '{now}');")
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habits():
|
|
|
|
|
query = f"SELECT * FROM habits;"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habits = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habit(id: int):
|
|
|
|
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habits = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habits
|