From d8c796ced770f9189cbf8a1eadffe1fdb10e7d4d Mon Sep 17 00:00:00 2001 From: Yapollon Date: Sat, 13 Jan 2024 13:30:29 +0100 Subject: [PATCH] Stylesheet for main.html Table for Habits --- db/SQLiteClient.py | 32 +++++++++++++++++++ .../1705145041_create_habits_table.sql | 4 +-- models/Habit.py | 28 ++++++++++++++++ models/User.py | 1 - 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 models/Habit.py diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 03aa513..345fb51 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -39,3 +39,35 @@ def get_user_by_email(email: str): user = cursor.fetchone() conn.close() 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 \ No newline at end of file diff --git a/db/migrations/1705145041_create_habits_table.sql b/db/migrations/1705145041_create_habits_table.sql index c4b5b67..7e5d01c 100644 --- a/db/migrations/1705145041_create_habits_table.sql +++ b/db/migrations/1705145041_create_habits_table.sql @@ -2,8 +2,8 @@ CREATE TABLE IF NOT EXISTS habits ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, - note TEXT NOT NULL, - times TEXT NOT NULL, + note TEXT NULL, + times INTEGER NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL ); diff --git a/models/Habit.py b/models/Habit.py new file mode 100644 index 0000000..60d54fa --- /dev/null +++ b/models/Habit.py @@ -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 \ No newline at end of file diff --git a/models/User.py b/models/User.py index 0a1acbe..3f67928 100644 --- a/models/User.py +++ b/models/User.py @@ -1,5 +1,4 @@ from flask_login import UserMixin - from db.SQLiteClient import create_user, get_user, get_user_by_email