Refactor database message handling to support multiple messages and enhance type hints across various classes

This commit is contained in:
2025-05-14 14:34:40 +02:00
parent f7c499ea6e
commit 0c53778f99
6 changed files with 126 additions and 108 deletions

View File

@@ -583,25 +583,28 @@ class Database:
return self.query_db("SELECT * FROM subjects")
# Messages
def addMessage(self, message: dict, user: str, app_id: Union[str, int]):
def addMessage(
self, messages: list[dict[str, Any]], user: str, app_id: Union[str, int]
):
"""add a Message to the database
Args:
message (dict): the message to be added
user (str): the user who added the message
messages (list[dict[str, Any]]): the messages to be added
user (str): the user who added the messages
app_id (Union[str,int]): the id of the apparat
"""
def __getUserId(user):
def __getUserId(user: str):
return self.query_db(
"SELECT id FROM user WHERE username=?", (user,), one=True
)[0]
user_id = __getUserId(user)
self.query_db(
"INSERT INTO messages (message, user_id, remind_at,appnr) VALUES (?,?,?,?)",
(message["message"], user_id, message["remind_at"], app_id),
)
for message in messages:
self.query_db(
"INSERT INTO messages (message, user_id, remind_at,appnr) VALUES (?,?,?,?)",
(message["message"], user_id, message["remind_at"], app_id),
)
def getAllMessages(self) -> list[dict[str, str, str, str]]:
"""Get all the messages in the database
@@ -610,7 +613,7 @@ class Database:
list[dict[str, str, str, str]]: a list of dictionaries containing the message, the user who added the message, the apparat id and the id of the message
"""
def __get_user_name(user_id):
def __get_user_name(user_id: int):
return self.query_db(
"SELECT username FROM user WHERE id=?", (user_id,), one=True
)[0]
@@ -628,17 +631,17 @@ class Database:
]
return ret
def getMessages(self, date: str) -> list[dict[str, str, str, str]]:
def getMessages(self, date: str) -> list[dict[str, str]]:
"""Get all the messages for a specific date
Args:
date (str): a date.datetime object formatted as a string in the format "YYYY-MM-DD"
Returns:
list[dict[str, str, str, str]]: a list of dictionaries containing the message, the user who added the message, the apparat id and the id of the message
list[dict[str, str]]: a list of dictionaries containing the message, the user who added the message, the apparat id and the id of the message
"""
def __get_user_name(user_id):
def __get_user_name(user_id: int):
return self.query_db(
"SELECT username FROM user WHERE id=?", (user_id,), one=True
)[0]
@@ -650,7 +653,7 @@ class Database:
]
return ret
def deleteMessage(self, message_id):
def deleteMessage(self, message_id: int):
"""Delete a message from the database
Args:
@@ -692,7 +695,9 @@ class Database:
)[0]
return f"{title} " if title is not None else ""
def getSpecificProfData(self, prof_id: Union[str, int], fields: List[str]) -> tuple:
def getSpecificProfData(
self, prof_id: Union[str, int], fields: List[str]
) -> tuple[Any, ...]:
"""A customisable function to get specific data of a professor based on the id
Args:
@@ -761,7 +766,7 @@ class Database:
return [Prof().from_tuple(prof) for prof in profs]
# Apparat
def getAllAparats(self, deleted=0) -> list[tuple]:
def getAllAparats(self, deleted: int = 0) -> list[Apparat]:
"""Get all the apparats in the database
Args:
@@ -770,9 +775,13 @@ class Database:
Returns:
list[tuple]: a list of tuples containing the apparats
"""
return self.query_db(
apparats = self.query_db(
"SELECT * FROM semesterapparat WHERE deletion_status=?", (deleted,)
)
ret: list[Apparat] = []
for apparat in apparats:
ret.append(Apparat().from_tuple(apparat))
return ret
def getApparatData(self, appnr, appname) -> ApparatData:
"""Get the Apparat data based on the apparat number and the name
@@ -1595,7 +1604,7 @@ class Database:
else:
return Prof()
def getProfIDByApparat(self, apprarat_id):
def getProfIDByApparat(self, apprarat_id: int) -> Optional[int]:
"""Get the prof id based on the semesterapparat id from the database
Args:
@@ -1613,7 +1622,7 @@ class Database:
else:
return None
def copyBookToApparat(self, book_id, apparat):
def copyBookToApparat(self, book_id: int, apparat: int):
# get book data
new_apparat_id = apparat
new_prof_id = self.getProfIDByApparat(new_apparat_id)
@@ -1634,7 +1643,7 @@ class Database:
connection.commit()
connection.close()
def moveBookToApparat(self, book_id, appratat):
def moveBookToApparat(self, book_id: int, appratat: int):
"""Move the book to the new apparat
Args:
@@ -1649,7 +1658,7 @@ class Database:
connection.commit()
connection.close()
def getApparatNameByAppNr(self, appnr):
def getApparatNameByAppNr(self, appnr: int):
query = f"SELECT name FROM semesterapparat WHERE appnr = '{appnr}' and deletion_status = 0"
data = self.query_db(query)
if data: