Compare commits
No commits in common. "8c2f324f7a5378860e4a98c8d3bde79fb7ce82be" and "0887bf339447131e96310d8383e85c27c94e1edd" have entirely different histories.
8c2f324f7a
...
0887bf3394
@ -41,10 +41,10 @@ def get_user_by_email(email: str):
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def create_habit(name: str, user_id: int, times: int, unit: int, note: str | None=None):
|
def create_habit(name: str, times: int, note: str | None=None):
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
query = (f"INSERT INTO habits (user_id, name, note, times, unit, created_at, updated_at) VALUES ('{user_id}', "
|
query = (f"INSERT INTO habits (name, note, times, created_at, updated_at) VALUES ('{name}', '{note}', "
|
||||||
f"'{name}', '{note}', '{times}', '{unit}', '{now}', '{now}');")
|
f"'{times}', '{now}', '{now}');")
|
||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
@ -53,16 +53,6 @@ def create_habit(name: str, user_id: int, times: int, unit: int, note: str | Non
|
|||||||
return cursor.lastrowid
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
|
||||||
def get_habit(id: int):
|
|
||||||
query = f"SELECT * FROM habits WHERE id = {id};"
|
|
||||||
conn = con3()
|
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute(query)
|
|
||||||
habits = cursor.fetchone()
|
|
||||||
conn.close()
|
|
||||||
return habits
|
|
||||||
|
|
||||||
|
|
||||||
def get_habits():
|
def get_habits():
|
||||||
query = f"SELECT * FROM habits;"
|
query = f"SELECT * FROM habits;"
|
||||||
conn = con3()
|
conn = con3()
|
||||||
@ -71,3 +61,13 @@ def get_habits():
|
|||||||
habits = cursor.fetchone()
|
habits = cursor.fetchone()
|
||||||
conn.close()
|
conn.close()
|
||||||
return habits
|
return habits
|
||||||
|
|
||||||
|
|
||||||
|
def get_habit(id: int):
|
||||||
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
||||||
|
conn = con3()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
habits = cursor.fetchone()
|
||||||
|
conn.close()
|
||||||
|
return habits
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE habits;
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
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,36 +1,28 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from db.SQLiteClient import create_habit, get_habits, get_habit
|
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
|
@dataclass
|
||||||
class Habit:
|
class Habit:
|
||||||
id: int
|
id: int
|
||||||
user_id: int
|
|
||||||
name: str
|
name: str
|
||||||
note: str
|
note: str
|
||||||
times: int
|
times: int
|
||||||
unit: int
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(user_id: int, name: str, times: int, unit: int, note: str | None=None):
|
def create(name: str, times: int, note: str | None=None):
|
||||||
id = create_habit(user_id, name, note, times, unit)
|
id = create_habit(name, note, times)
|
||||||
return Habit(id, user_id, name, note, times, unit)
|
return Habit(id, name, note, times)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get(id: int):
|
def get(id: int):
|
||||||
habit = get_habit(id)
|
habit = get_habit(id)
|
||||||
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5]) if habit else None
|
return Habit(habit[0], habit[1], habit[2], habit[3]) if habit else None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all():
|
def get_all():
|
||||||
raw_habits = get_habits()
|
raw_habits = get_habits()
|
||||||
habits = []
|
habits = []
|
||||||
for habit in raw_habits:
|
for habit in raw_habits:
|
||||||
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5])
|
habit = Habit(habit[0], habit[1], habit[2], habit[3])
|
||||||
habits.append(habit)
|
habits.append(habit)
|
||||||
return habits if habits else None
|
return habits if habits else None
|
||||||
Loading…
x
Reference in New Issue
Block a user