Compare commits
2 Commits
a704c3a0c8
...
9ef8f3f513
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ef8f3f513 | ||
|
|
d8c796ced7 |
@ -39,3 +39,35 @@ def get_user_by_email(email: str):
|
|||||||
user = cursor.fetchone()
|
user = cursor.fetchone()
|
||||||
conn.close()
|
conn.close()
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
def create_habit(name: str, times: 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}');")
|
||||||
|
conn = con3()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
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()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
habits = cursor.fetchone()
|
||||||
|
conn.close()
|
||||||
|
return habits
|
||||||
@ -2,8 +2,8 @@ CREATE TABLE IF NOT EXISTS habits
|
|||||||
(
|
(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
note TEXT NOT NULL,
|
note TEXT NULL,
|
||||||
times TEXT NOT NULL,
|
times INTEGER NOT NULL,
|
||||||
created_at TEXT NOT NULL,
|
created_at TEXT NOT NULL,
|
||||||
updated_at TEXT NOT NULL
|
updated_at TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|||||||
28
models/Habit.py
Normal file
28
models/Habit.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from db.SQLiteClient import create_habit, get_habits, get_habit
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Habit:
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
note: str
|
||||||
|
times: int
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create(name: str, times: int, note: str | None=None):
|
||||||
|
id = create_habit(name, note, times)
|
||||||
|
return Habit(id, name, note, times)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(id: int):
|
||||||
|
habit = get_habit(id)
|
||||||
|
return Habit(habit[0], habit[1], habit[2], habit[3]) 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])
|
||||||
|
habits.append(habit)
|
||||||
|
return habits if habits else None
|
||||||
@ -1,5 +1,4 @@
|
|||||||
from flask_login import UserMixin
|
from flask_login import UserMixin
|
||||||
|
|
||||||
from db.SQLiteClient import create_user, get_user, get_user_by_email
|
from db.SQLiteClient import create_user, get_user, get_user_by_email
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user