add insert, request, ordered functions for new edition books

This commit is contained in:
2025-09-22 09:37:25 +02:00
parent 65c86a65cd
commit 7079b4d47f

View File

@@ -22,6 +22,7 @@ from src.backend.db import (
CREATE_TABLE_FILES,
CREATE_TABLE_MEDIA,
CREATE_TABLE_MESSAGES,
CREATE_TABLE_NEWEDITIONS,
CREATE_TABLE_PROF,
CREATE_TABLE_SUBJECTS,
CREATE_TABLE_USER,
@@ -87,6 +88,7 @@ class Database:
"elsa",
"elsa_files",
"elsa_media",
"neweditions",
]
for table in required_tables:
@@ -116,6 +118,8 @@ class Database:
query = CREATE_ELSA_FILES_TABLE
case "elsa_media":
query = CREATE_ELSA_MEDIA_TABLE
case "neweditions":
query = CREATE_TABLE_NEWEDITIONS
case _:
log.error(f"Table {table_name} is not a valid table name")
self.query_db(query)
@@ -233,7 +237,7 @@ class Database:
def query_db(
self,
query: str,
args: Tuple[Any, Any] = (), # type:ignore
args: Tuple[Any] = (), # type:ignore
one: bool = False, # type:ignore
) -> Union[Tuple[Any, Any], List[Tuple[Any, Any]]]:
"""
@@ -430,9 +434,8 @@ class Database:
app_id = row[2]
prof_id = row[3]
sig_val = getattr(bookdata, "signature", None) or ""
title_val = getattr(bookdata, "title", None) or ""
sig_val = bookdata.signature
title_val = bookdata.title
if mode == 1:
if sig_re.search(sig_val):
results.append((bookdata, app_id, prof_id))
@@ -471,7 +474,7 @@ class Database:
"""
result = self.query_db(
"SELECT id FROM media WHERE bookdata=? AND app_id=? AND prof_id=?",
(dump_pickle(bookdata), app_id, prof_id),
(bookdata.to_dict, app_id, prof_id),
one=True,
)
return result[0]
@@ -535,17 +538,23 @@ class Database:
ret_result.append(data)
return ret_result
def getBooksByProfId(self, prof_id: int, deleted: int = 0):
def getBooksByProfId(
self, prof_id: int, deleted: int = 0
) -> list[dict[str, Union[int, BookData]]]:
"""
Get the Books based on the professor id
Args:
prof_id (str): The ID of the professor
deleted (int, optional): The state of the book. Set to 1 to include deleted ones. Defaults to 0.
Parameters
----------
prof_id : int
The ID of the professor
deleted : int, optional
If set to 1, it will include deleted books, by default 0
Returns:
list[dict[int, BookData, int]]: A list of dictionaries containing the id, the metadata of the book and the availability of the book
Returns
-------
list[dict[str, Union[int, BookData]]]
A list of dictionaries containing the id, the metadata of the book and the availability of the book
"""
qdata = self.query_db(
f"SELECT id,bookdata,available FROM media WHERE prof_id={prof_id} AND (deleted={deleted if deleted == 0 else '1 OR deleted=0'})"
@@ -1170,7 +1179,7 @@ class Database:
(semester, apparat_nr, apparat.name),
)
# delete all books associated with the app_id
print(apparat_nr, app_id)
# print(apparat_nr, app_id)
self.query_db("UPDATE media SET deleted=1 WHERE app_id=?", (app_id,))
def isEternal(self, id):
@@ -1243,11 +1252,11 @@ class Database:
else False
)
def checkApparatExistsById(self, app_id: Union[str, int]) -> bool:
"""a check to see if the apparat is already present in the database, based on the id
def checkApparatExistsByNr(self, app_nr: Union[str, int]) -> bool:
"""a check to see if the apparat is already present in the database, based on the nr. This query will exclude deleted apparats
Args:
app_id (Union[str, int]): the id of the apparat
app_nr (Union[str, int]): the id of the apparat
Returns:
bool: True if the apparat is present, False if not
@@ -1255,7 +1264,9 @@ class Database:
return (
True
if self.query_db(
"SELECT appnr FROM semesterapparat WHERE appnr=?", (app_id,), one=True
"SELECT id FROM semesterapparat WHERE appnr=? and deletion_status=0",
(app_nr,),
one=True,
)
else False
)
@@ -1844,3 +1855,49 @@ class Database:
result = cursor.fetchone()
connection.close()
return result
def getBookIdByPPN(self, ppn: str) -> int:
query = f"SELECT id FROM media WHERE bookdata LIKE '%{ppn}%'"
data = self.query_db(query)
if data:
return data[0][0]
else:
return None
def getNewEditionsByApparat(self, apparat_id: int) -> list[BookData]:
"""Get all new editions for a specific apparat
Args:
apparat_id (int): the id of the apparat
Returns:
list[tuple]: A list of tuples containing the new editions data
"""
query = "SELECT * FROM neweditions WHERE for_apparat=?"
results = self.query_db(query, (apparat_id,))
res = []
for result in results:
old_edition_edition = self.query_db(
"SELECT bookdata FROM media WHERE id=?", (result[2],), one=True
)
res.append(BookData().from_string(result[1]))
return res
def setOrdered(self, newBook_id: int):
query = "UPDATE neweditions SET ordered=1 WHERE id=?"
self.query_db(query, (newBook_id,))
def getNewEditionId(self, newBook: BookData):
query = "SELECT id FROM neweditions WHERE new_bookdata=?"
params = (newBook.to_dict,)
data = self.query_db(query, params, one=True)
if data:
return data[0]
else:
return None
def insertNewEdition(self, newBook: BookData, oldBookId: int, for_apparat: int):
query = "INSERT INTO neweditions (new_bookdata, old_edition_id, for_apparat) VALUES (?,?,?)"
params = (newBook.to_dict, oldBookId, for_apparat)
self.query_db(query, params)