2024-01-13 13:30:29 +01:00
|
|
|
from dataclasses import dataclass
|
2024-01-26 09:06:47 +01:00
|
|
|
from models.HabitTrackings import HabitTrackings
|
2024-01-26 09:33:41 +01:00
|
|
|
from db.SQLiteClient import create_habit, get_habit,update_habit, delete_habit, get_next_slot, \
|
|
|
|
|
get_habitTrackings_by_habit_id, get_slots, update_slot
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-01-16 10:58:25 +01:00
|
|
|
# Unit wird als Integers wie folgt gemessen:
|
|
|
|
|
# 0: Tag
|
2024-01-17 10:32:52 +01:00
|
|
|
# 1: Woche (Default)
|
2024-01-19 10:44:00 +01:00
|
|
|
# 2: Monat
|
2024-01-16 10:58:25 +01:00
|
|
|
# 3: Jahr
|
|
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
@dataclass
|
|
|
|
|
class Habit:
|
|
|
|
|
id: int
|
2024-01-16 10:58:25 +01:00
|
|
|
user_id: int
|
2024-01-13 13:30:29 +01:00
|
|
|
name: str
|
|
|
|
|
note: str
|
|
|
|
|
times: int
|
2024-01-16 10:58:25 +01:00
|
|
|
unit: int
|
2024-01-17 10:32:52 +01:00
|
|
|
slot: int
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-19 10:44:00 +01:00
|
|
|
def create(user_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
|
2024-01-26 08:30:06 +01:00
|
|
|
slot = get_next_slot(user_id)
|
2024-01-23 10:50:28 +01:00
|
|
|
id = create_habit(user_id, name, times, unit, slot, note)
|
2024-01-17 10:32:52 +01:00
|
|
|
return Habit(id, user_id, name, note, times, unit, slot)
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
@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
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-01-26 09:29:47 +01:00
|
|
|
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
|
|
|
|
|
update_habit(self.id, name, note, times, unit)
|
|
|
|
|
if name is not None:
|
|
|
|
|
self.name = name
|
|
|
|
|
if note is not None:
|
|
|
|
|
self.note = note
|
|
|
|
|
if times is not None:
|
|
|
|
|
self.times = times
|
|
|
|
|
if unit is not None:
|
|
|
|
|
self.unit = unit
|
|
|
|
|
|
2024-01-26 09:33:41 +01:00
|
|
|
# So sollte die Slots Liste aussehen damit es funktioniert
|
2024-01-26 09:29:47 +01:00
|
|
|
#[(id, 1), (id, 2), (id, 3), (id, 4), (id, 5)]
|
|
|
|
|
def update_slot(self, new_slot: int):
|
|
|
|
|
slots = get_slots(self.user_id)
|
|
|
|
|
if new_slot > self.slot:
|
|
|
|
|
slots = slots[self.slot:new_slot]
|
|
|
|
|
for slot in slots:
|
|
|
|
|
update_slot(slot[0], slot[1]-1)
|
|
|
|
|
if new_slot < self.slot:
|
|
|
|
|
slots = slots[new_slot-1:self.slot-1]
|
|
|
|
|
for slot in slots:
|
|
|
|
|
update_slot(slot[0], slot[1]+1)
|
|
|
|
|
self.slot = new_slot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
delete_habit(self.id)
|
2024-01-26 09:06:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_habitTrackings(self) -> list[HabitTrackings]:
|
|
|
|
|
return get_habitTrackings_by_habit_id(self.id)
|