Compare commits

..

2 Commits

Author SHA1 Message Date
Yapollon
a33e349f63 Merge branch 'master' of https://repo.cimeyclust.com/CimeyClust/HabitTracker 2024-01-23 10:32:42 +01:00
Yapollon
a752ecd087 create habit Tracking table
habit tracking databank erstellt.
2024-01-23 10:32:14 +01:00
3 changed files with 66 additions and 3 deletions

View File

@ -52,9 +52,9 @@ def delete_user(id: int):
conn.close() 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() 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}');") f"'{name}', '{note}', '{times}', '{unit}', '{slot}', '{now}', '{now}');")
conn = con3() conn = con3()
cursor = conn.cursor() cursor = conn.cursor()
@ -63,6 +63,7 @@ def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note
conn.close() conn.close()
return cursor.lastrowid return cursor.lastrowid
def get_next_slot(): def get_next_slot():
query = f"SELECT slot FROM habits ORDER BY slot DESC LIMIT 1;" query = f"SELECT slot FROM habits ORDER BY slot DESC LIMIT 1;"
conn = con3() conn = con3()
@ -99,4 +100,35 @@ def delete_habit(id: int):
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(query) cursor.execute(query)
conn.commit() conn.commit()
conn.close() 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()

View File

@ -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)
);

23
models/HabitTrackings.py Normal file
View File

@ -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)