From a752ecd087c97d74f333a3399159d23e45fe1a02 Mon Sep 17 00:00:00 2001 From: Yapollon Date: Tue, 23 Jan 2024 10:32:14 +0100 Subject: [PATCH] create habit Tracking table habit tracking databank erstellt. --- db/SQLiteClient.py | 38 +++++++++++++++++-- .../1706001378_create_habit_trackings.sql | 8 ++++ models/HabitTrackings.py | 23 +++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 db/migrations/1706001378_create_habit_trackings.sql create mode 100644 models/HabitTrackings.py diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index c28540f..3ac6bfd 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -52,9 +52,9 @@ def delete_user(id: int): conn.close() -def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note: str | None=None): +def create_habit(user_id: int, name: str, times: int, unit: int, slot: int, note: str | None=None): now = datetime.now().isoformat() - query = (f"INSERT INTO habits (user_id, name, note, times, unit, list_index, created_at, updated_at) VALUES ('{user_id}', " + query = (f"INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) VALUES ('{user_id}', " f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');") conn = con3() cursor = conn.cursor() @@ -63,6 +63,7 @@ def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note conn.close() return cursor.lastrowid + def get_next_slot(): query = f"SELECT slot FROM habits ORDER BY slot DESC LIMIT 1;" conn = con3() @@ -99,4 +100,35 @@ def delete_habit(id: int): cursor = conn.cursor() cursor.execute(query) conn.commit() - conn.close() \ No newline at end of file + 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() diff --git a/db/migrations/1706001378_create_habit_trackings.sql b/db/migrations/1706001378_create_habit_trackings.sql new file mode 100644 index 0000000..924617a --- /dev/null +++ b/db/migrations/1706001378_create_habit_trackings.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS habit_trackings +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + habit_id INTEGER, + times INTEGER NOT NULL, + created_at TEXT NOT NULL, + FOREIGN KEY (habit_id) REFERENCES habits(id) +); \ No newline at end of file diff --git a/models/HabitTrackings.py b/models/HabitTrackings.py new file mode 100644 index 0000000..4374551 --- /dev/null +++ b/models/HabitTrackings.py @@ -0,0 +1,23 @@ +from dataclasses import dataclass +from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings + + +@dataclass +class HabitTrackings: + id: int + habit_id: int + times: int + + @staticmethod + def create(habit_id: int, times: int): + id = create_habitTrackings(habit_id, times) + return HabitTrackings(id, habit_id, times) + + @staticmethod + def get(id: int): + habitTrackings = get_habitTrackings(id) + return HabitTrackings(habitTrackings[0], habitTrackings[1], habitTrackings[2]) if habitTrackings else None + + @staticmethod + def delete(id: int): + delete_habitTrackings(id)