73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
from dataclasses import dataclass
|
|
|
|
from models.Habit import Habit
|
|
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)
|
|
|
|
|
|
@dataclass
|
|
class HabitList:
|
|
id: int
|
|
name: str
|
|
description: str
|
|
habits: list = None #? unclear usage
|
|
|
|
@staticmethod
|
|
def create(user_id: int, name: str, description: str):
|
|
id = create_habitList(user_id, name, description)
|
|
return HabitList(id, name, description)
|
|
|
|
@staticmethod
|
|
def get(id: int):
|
|
habitList = get_habitList(id)
|
|
return HabitList(habitList[0], habitList[1], habitList[2]) if habitList else None
|
|
|
|
|
|
# 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):
|
|
if len(get_users) > 1:
|
|
self.remove_user(user_id)
|
|
else:
|
|
delete_habitList(self.id)
|
|
|
|
|
|
# Returns the Habits connected with the HabitList
|
|
def get_habits(self) -> list:
|
|
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])
|
|
habits.append(habit)
|
|
|
|
return habits
|
|
|
|
|
|
# Returns the Users connected with the HabitList
|
|
def get_users(self) -> list:
|
|
raw_users = get_users(self.id)
|
|
users = []
|
|
for user in raw_users:
|
|
user = User(user[0], user[1], user[2], user[3], user[4])
|
|
users.append(user)
|
|
|
|
return users
|
|
|
|
|
|
# Adds a User by email to the HabitList
|
|
def add_user(self, user: User):
|
|
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)
|