Implement CRUD operations for luggage items: add, update, delete, and fetch functionalities; enhance logging and error handling

This commit is contained in:
Mabe
2026-08-28 21:02:25 +02:00
parent 79e0af79dc
commit c1931fae20
5 changed files with 216 additions and 40 deletions
+22 -17
View File
@@ -34,14 +34,14 @@ def connect(db_user, db_psswd, db_host, db_port, db_name):
logging.error("Error connecting")
logging.debug(f"Error connecting to DB: {e}")
def new_part(cloth_type, weigth, pieces):
def new_part(cloth_type, weight, pieces):
if type(pieces) is not int:
print("only ints allowed")
logging.error("only ints allowed")
else:
cursor = connection.cursor()
executed = "INSERT INTO luggage (clothes, weigth, pieces) VALUES (?, ?, ?)"
executed_data = (cloth_type, weigth, pieces)
executed = "INSERT INTO luggage (clothes, weight, pieces) VALUES (?, ?, ?)"
executed_data = (cloth_type, weight, pieces)
cursor.execute(executed, executed_data)
connection.commit()
logging.info("new Insert is in DB")
@@ -53,13 +53,13 @@ def fetch_clothes():
logging.info("fetched clothes")
return clothes
def update_weigth(cloth_type, weigth):
def update_weight(cloth_type, weight):
cursor = connection.cursor()
executed = "UPDATE luggage SET weigth = ? WHERE clothes = ?"
executed_data = (weigth, cloth_type)
executed = "UPDATE luggage SET weight = ? WHERE clothes = ?"
executed_data = (weight, cloth_type)
cursor.execute(executed, executed_data)
connection.commit()
logging.info("weigth updated")
logging.info("weight updated")
def update_pieces(cloth_type, pieces):
if type(pieces) is not int:
@@ -72,19 +72,19 @@ def update_pieces(cloth_type, pieces):
connection.commit()
logging.info("pieces updated")
def fetch_weigth(cloth_type):
def fetch_weight(cloth_type):
cursor = connection.cursor()
cursor.execute("SELECT weigth FROM luggage WHERE clothes = ?", (cloth_type,))
weigth = cursor.fetchone()[0]
logging.info("fetched weigth")
return weigth
cursor.execute("SELECT weight FROM luggage WHERE clothes = ?", (cloth_type,))
weight = cursor.fetchone()[0]
logging.info("fetched weight")
return weight
def fetch_luggage_weigth():
def fetch_luggage_weight():
cursor = connection.cursor()
cursor.execute("SELECT sum(weigth) FROM luggage")
weigth = cursor.fetchone()[0]
logging.info("fetched luggage weigth")
return weigth
cursor.execute("SELECT sum(weight) FROM luggage")
weight = cursor.fetchone()[0]
logging.info("fetched luggage weight")
return weight
def fetch_piece(cloth_type):
cursor = connection.cursor()
@@ -106,6 +106,11 @@ def delete(cloth_type):
logging.critical(f"{cloth_type} was deleted")
connection.commit()
def check_clothe(clothe_type):
cursor = connection.cursor()
cursor.execute("SELECT (SELECT clothes FROM luggage WHERE clothes = ? LIMIT 1) AS clothes", (clothe_type,))
check = cursor.fetchone()[0]
return check
#using .env