302 lines
8.4 KiB
Python
302 lines
8.4 KiB
Python
from datetime import datetime, timedelta
|
|
import hashlib
|
|
import sqlite3
|
|
|
|
|
|
def con3():
|
|
conn = sqlite3.connect('db/db.sqlite')
|
|
return conn
|
|
|
|
|
|
### User.py ###
|
|
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}');")
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
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}';"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
user = cursor.fetchone()
|
|
conn.close()
|
|
return user
|
|
|
|
|
|
def update_user(id: int, name: str, email: str, password: str = None):
|
|
now = datetime.now().isoformat()
|
|
if password:
|
|
query = f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}', updated_at = '{now}' WHERE id = {id};"
|
|
else:
|
|
query = f"UPDATE users SET name = '{name}', email = '{email}', updated_at = '{now}' WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def delete_user(id: int):
|
|
query = f"DELETE FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {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()
|
|
return cursor.lastrowid
|
|
|
|
|
|
### Habit.py ###
|
|
def create_habit(list_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None):
|
|
now = datetime.now().isoformat()
|
|
query = (f"INSERT INTO habits (list_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{list_id}', "
|
|
f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def get_habit(id: int):
|
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
habit = cursor.fetchone()
|
|
conn.close()
|
|
return habit
|
|
|
|
|
|
def get_habits(list_id: int):
|
|
query = f"SELECT * FROM habits WHERE list_id = {list_id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
habits = cursor.fetchall()
|
|
conn.close()
|
|
return habits
|
|
|
|
|
|
def get_heatmap_value(user_id: int, days: int):
|
|
# Berechnet das Datum, ab dem die Habits gezählt werden sollen
|
|
date = (datetime.now() - timedelta(days=days)).date()
|
|
print(date)
|
|
|
|
# Uses JOINs to get all Habits
|
|
query = (f"SELECT habits.id FROM habits "
|
|
f"JOIN habit_lists ON habits.list_id = habit_lists.id "
|
|
f"JOIN habit_users ON habit_lists.id = habit_users.list_id "
|
|
f"WHERE habit_users.user_id = {user_id};")
|
|
|
|
# Uses JOINs to get all checked Habits on a specific date
|
|
query2 = (f"SELECT habits.id FROM habits "
|
|
f"JOIN habit_lists ON habits.list_id = habit_lists.id "
|
|
f"JOIN habit_users ON habit_lists.id = habit_users.list_id "
|
|
f"JOIN habit_trackings ON habits.id = habit_trackings.habit_id "
|
|
f"WHERE habit_users.user_id = {user_id} AND DATE(habit_trackings.created_at) = '{date}';")
|
|
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
|
|
# Execute the queries
|
|
cursor.execute(query)
|
|
all_habits = cursor.fetchall()
|
|
cursor.execute(query2)
|
|
checked_habits = cursor.fetchall()
|
|
|
|
# Close the database connection
|
|
count = len(all_habits)
|
|
print(count)
|
|
count2 = len(checked_habits)
|
|
print(count2)
|
|
|
|
# Close the database connection
|
|
conn.close()
|
|
|
|
# Calculate the percentage of checked Habits
|
|
if count > 0:
|
|
return int(count2 / count * 100)
|
|
else:
|
|
return 0
|
|
|
|
|
|
def get_next_slot(list_id: int):
|
|
query = f"SELECT slot FROM habits WHERE list_id = {list_id} ORDER BY slot DESC LIMIT 1;"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
slot = cursor.fetchone()
|
|
conn.close()
|
|
return slot[0] + 1 if slot else 0
|
|
|
|
|
|
def get_slots(list_id: int):
|
|
query = f"SELECT id, slot FROM habits WHERE list_id = {list_id} ORDER BY slot;"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
slots = cursor.fetchall()
|
|
conn.close()
|
|
return slots
|
|
|
|
|
|
def update_slot(id: int, slot: int):
|
|
now = datetime.now().isoformat()
|
|
query = f"UPDATE habits SET slot = {slot}, updated_at = '{now}' WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def update_habit(id: int, name: str, note: str, times: int, unit: int):
|
|
now = datetime.now().isoformat()
|
|
query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit}, updated_at = '{now}' WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def delete_habit(id: int):
|
|
query = f"DELETE FROM habits WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
### HabitTrackings.py ###
|
|
def create_habitTrackings(habit_id: int):
|
|
now = datetime.now().isoformat()
|
|
query = (
|
|
f"INSERT INTO habit_trackings (habit_id, created_at) VALUES ('{habit_id}','{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 get_habitTrackings_by_habit_id(habit_id: int):
|
|
query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
habit_trackings = cursor.fetchall()
|
|
conn.close()
|
|
return habit_trackings
|
|
|
|
|
|
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()
|
|
|
|
|
|
### HabitList.py ###
|
|
def create_habitList(user_id: int, name: str, description: str):
|
|
now = datetime.now().isoformat()
|
|
query = (
|
|
f"INSERT INTO habit_lists (name, description, created_at, updated_at) VALUES ('{name}', '{description}', '{now}', '{now}');")
|
|
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
|
|
query1 = f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at) VALUES ('{user_id}', '{cursor.lastrowid}', '{now}', '{now}');"
|
|
|
|
cursor.execute(query1)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def get_habitList(id: int):
|
|
query = f"SELECT * FROM habit_lists WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
habit_list = cursor.fetchone()
|
|
conn.close()
|
|
return habit_list
|
|
|
|
|
|
def get_habitLists(user_id: int):
|
|
query = f"SELECT habit_lists.* FROM habit_lists JOIN habit_users ON habit_lists.id = habit_users.list_id WHERE habit_users.user_id = {user_id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
habit_lists = cursor.fetchall()
|
|
conn.close()
|
|
return habit_lists
|
|
|
|
|
|
def update_habitList(id: int, name: str, description: str):
|
|
now = datetime.now().isoformat()
|
|
query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
return cursor.lastrowid
|
|
|
|
|
|
def delete_habitList(id: int):
|
|
query = f"DELETE FROM habit_lists WHERE id = {id};"
|
|
conn = con3()
|
|
cursor = conn.cursor()
|
|
cursor.execute(query)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
habits = get_habits(1)
|
|
for habit in habits:
|
|
print(habit[6])
|