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
|
|
|
|
|
|
|
|
|
2024-01-26 08:10:36 +01:00
|
|
|
def update_user(id: int, name: str, email: str, password: str):
|
|
|
|
|
query = f"UPDATE users SET name = {name}, email = {email}, password = {password} WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
user = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
2024-01-17 11:23:22 +01:00
|
|
|
def delete_user(id: int):
|
|
|
|
|
query = f"DELETE FROM habits WHERE user_id = {id};"
|
|
|
|
|
query2 = f"DELETE FROM users WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
cursor.execute(query2)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
2024-01-26 08:10:36 +01:00
|
|
|
return cursor.lastrowid
|
2024-01-17 11:23:22 +01:00
|
|
|
|
|
|
|
|
|
2024-01-23 10:32:14 +01:00
|
|
|
def create_habit(user_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None):
|
2024-01-13 13:30:29 +01:00
|
|
|
now = datetime.now().isoformat()
|
2024-01-23 10:32:14 +01:00
|
|
|
query = (f"INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{user_id}', "
|
2024-01-17 10:32:52 +01:00
|
|
|
f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
2024-01-23 10:32:14 +01:00
|
|
|
|
2024-01-26 08:30:06 +01:00
|
|
|
def get_next_slot(user_id: int):
|
|
|
|
|
query = f"SELECT slot FROM habits WHERE user_id = {user_id} ORDER BY slot DESC LIMIT 1;"
|
2024-01-19 10:44:00 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
slot = cursor.fetchone()
|
|
|
|
|
conn.close()
|
2024-01-26 08:30:06 +01:00
|
|
|
|
|
|
|
|
if not slot:
|
|
|
|
|
return 0
|
|
|
|
|
|
2024-01-19 10:44:00 +01:00
|
|
|
return slot[0] + 1 if slot else 0
|
|
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-01-16 10:58:25 +01:00
|
|
|
def get_habit(id: int):
|
|
|
|
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-01-17 11:23:22 +01:00
|
|
|
habit = cursor.fetchone()
|
2024-01-13 13:30:29 +01:00
|
|
|
conn.close()
|
2024-01-17 11:23:22 +01:00
|
|
|
return habit
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
|
2024-01-16 11:10:54 +01:00
|
|
|
def get_habits(user_id: int):
|
|
|
|
|
query = f"SELECT * FROM habits WHERE user_id = {user_id};"
|
2024-01-13 13:30:29 +01:00
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
2024-01-17 11:23:22 +01:00
|
|
|
habits = cursor.fetchall()
|
2024-01-13 13:30:29 +01:00
|
|
|
conn.close()
|
2024-01-17 11:23:22 +01:00
|
|
|
return habits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_habit(id: int):
|
|
|
|
|
query = f"DELETE FROM habits WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
2024-01-23 10:32:14 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_habitTrackings(habit_id: int, times: int):
|
|
|
|
|
now = datetime.now().isoformat()
|
|
|
|
|
query = (
|
|
|
|
|
f"INSERT INTO habit_trackings (habit_id, times, created_at) VALUES ('{habit_id}', '{times}','{now}');")
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habitTrackings(id: int):
|
|
|
|
|
query = f"SELECT * FROM habit_trackings WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
habit_tracking = cursor.fetchone()
|
|
|
|
|
conn.close()
|
|
|
|
|
return habit_tracking
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_habitTrackings(id: int):
|
|
|
|
|
query = f"DELETE FROM habit_trackings WHERE id = {id};"
|
|
|
|
|
conn = con3()
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
conn.commit()
|
|
|
|
|
conn.close()
|