HabitTracker/models/HabitList.py

73 lines
2.0 KiB
Python
Raw Normal View History

2024-02-12 21:07:55 +01:00
from dataclasses import dataclass
from models.Habit import Habit
2024-02-12 22:31:51 +01:00
from models.User import User
from db.SQLiteClient import (create_habitList, get_habitList, get_habits, get_users, add_user, remove_user,
update_habitList, delete_habitList)
2024-02-12 21:07:55 +01:00
@dataclass
class HabitList:
id: int
name: str
description: str
habits: list = None #? unclear usage
2024-02-12 21:07:55 +01:00
@staticmethod
def create(user_id: int, name: str, description: str):
id = create_habitList(user_id, name, description)
return HabitList(id, name, description)
2024-02-12 21:07:55 +01:00
@staticmethod
def get(id: int):
habitList = get_habitList(id)
2024-02-16 08:11:22 +01:00
return HabitList(habitList[0], habitList[1], habitList[2]) if habitList else None
2024-02-12 21:07:55 +01:00
# Updates: name, description
def update(self):
update_habitList(self.id, self.name, self.description)
# Deletes the HabitList | The id of the current user is necessary
def delete(self, user_id):
2024-03-06 11:04:15 +01:00
if len(get_users(self.id)) > 1:
self.remove_user(user_id)
else:
delete_habitList(self.id)
# Returns the Habits connected with the HabitList
def get_habits(self) -> list:
2024-02-12 21:07:55 +01:00
raw_habits = get_habits(self.id)
habits = []
for habit in raw_habits:
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6], habit[7], habit[8], habit[9])
2024-02-12 21:07:55 +01:00
habits.append(habit)
return habits
2024-02-12 22:31:51 +01:00
# Returns the Users connected with the HabitList
def get_users(self) -> list:
2024-02-12 22:31:51 +01:00
raw_users = get_users(self.id)
users = []
for user in raw_users:
user = User(user[0], user[1], user[2], user[3], user[4], user[5])
2024-02-12 22:31:51 +01:00
users.append(user)
return users
# Adds a User by email to the HabitList
2024-03-01 09:17:52 +01:00
def add_user(self, user: User):
2024-03-01 08:13:58 +01:00
if user:
add_user(self.id, user.id)
else:
return None
# Removes a User from the HabitList
def remove_user(self, user_id):
remove_user(self.id, user_id)