diff --git a/ER.png b/ER.png new file mode 100644 index 0000000..eb83e0a Binary files /dev/null and b/ER.png differ diff --git a/UML.png b/UML.png new file mode 100644 index 0000000..7e31f69 Binary files /dev/null and b/UML.png differ diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 345fb51..2b6bc09 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -41,10 +41,10 @@ def get_user_by_email(email: str): return user -def create_habit(name: str, times: int, note: str | None=None): +def create_habit(name: str, user_id: int, times: int, unit: 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}');") + query = (f"INSERT INTO habits (user_id, name, note, times, unit, created_at, updated_at) VALUES ('{user_id}', " + f"'{name}', '{note}', '{times}', '{unit}', '{now}', '{now}');") conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -53,16 +53,6 @@ def create_habit(name: str, times: int, note: str | None=None): 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() @@ -70,4 +60,14 @@ def get_habit(id: int): cursor.execute(query) habits = cursor.fetchone() conn.close() + return habits + + +def get_habits(): + query = f"SELECT * FROM habits;" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + habits = cursor.fetchone() + conn.close() return habits \ No newline at end of file diff --git a/db/migrations/1705398235_delete_habits_table.sql b/db/migrations/1705398235_delete_habits_table.sql new file mode 100644 index 0000000..8d667a0 --- /dev/null +++ b/db/migrations/1705398235_delete_habits_table.sql @@ -0,0 +1 @@ +DROP TABLE habits; diff --git a/db/migrations/1705398327_habits_table.sql b/db/migrations/1705398327_habits_table.sql new file mode 100644 index 0000000..d92838b --- /dev/null +++ b/db/migrations/1705398327_habits_table.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS habits +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER, + name TEXT NOT NULL, + note TEXT, + times INTEGER NOT NULL, + unit INTEGER, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id) +); \ No newline at end of file diff --git a/models/Habit.py b/models/Habit.py index 60d54fa..82a30a8 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -1,28 +1,36 @@ from dataclasses import dataclass from db.SQLiteClient import create_habit, get_habits, get_habit +# Unit wird als Integers wie folgt gemessen: +# 0: Tag +# 1: Woche +# 2: Monal +# 3: Jahr + @dataclass class Habit: id: int + user_id: int name: str note: str times: int + unit: int @staticmethod - def create(name: str, times: int, note: str | None=None): - id = create_habit(name, note, times) - return Habit(id, name, note, times) + def create(user_id: int, name: str, times: int, unit: int, note: str | None=None): + id = create_habit(user_id, name, note, times, unit) + return Habit(id, user_id, name, note, times, unit) @staticmethod def get(id: int): habit = get_habit(id) - return Habit(habit[0], habit[1], habit[2], habit[3]) if habit else None + return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5]) if habit else None @staticmethod def get_all(): raw_habits = get_habits() habits = [] for habit in raw_habits: - habit = Habit(habit[0], habit[1], habit[2], habit[3]) + habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5]) habits.append(habit) return habits if habits else None \ No newline at end of file