HabitTracker/models/Habit.py

38 lines
1.0 KiB
Python
Raw Normal View History

from dataclasses import dataclass
from db.SQLiteClient import create_habit, get_habits, get_habit
# Unit wird als Integers wie folgt gemessen:
# 0: Tag
2024-01-17 10:32:52 +01:00
# 1: Woche (Default)
# 2: Monal
# 3: Jahr
@dataclass
class Habit:
id: int
user_id: int
name: str
note: str
times: int
unit: int
2024-01-17 10:32:52 +01:00
slot: int
@staticmethod
2024-01-17 10:32:52 +01:00
def create(user_id: int, name: str, times: int, slot: int, note: str | None=None, unit: int | None=1):
id = create_habit(user_id, name, note, times, unit, slot)
return Habit(id, user_id, name, note, times, unit, slot)
@staticmethod
def get(id: int):
habit = get_habit(id)
2024-01-16 13:35:00 +01:00
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
@staticmethod
def get_all(user_id):
raw_habits = get_habits(user_id)
habits = []
for habit in raw_habits:
2024-01-16 13:35:00 +01:00
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6])
habits.append(habit)
2024-01-17 10:32:52 +01:00
return habits if habits else None