Completed the Habit Model and added units + user_id foreign key.
Also we have some good test data.
This commit is contained in:
parent
9ef8f3f513
commit
dff51be18f
@ -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
|
||||
1
db/migrations/1705398235_delete_habits_table.sql
Normal file
1
db/migrations/1705398235_delete_habits_table.sql
Normal file
@ -0,0 +1 @@
|
||||
DROP TABLE habits;
|
||||
12
db/migrations/1705398327_habits_table.sql
Normal file
12
db/migrations/1705398327_habits_table.sql
Normal file
@ -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)
|
||||
);
|
||||
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user