create habit Tracking table
habit tracking databank erstellt.
This commit is contained in:
parent
48bae38872
commit
a752ecd087
@ -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()
|
||||
@ -100,3 +101,34 @@ def delete_habit(id: int):
|
||||
cursor.execute(query)
|
||||
conn.commit()
|
||||
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()
|
||||
|
||||
8
db/migrations/1706001378_create_habit_trackings.sql
Normal file
8
db/migrations/1706001378_create_habit_trackings.sql
Normal 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
23
models/HabitTrackings.py
Normal 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)
|
||||
Loading…
x
Reference in New Issue
Block a user