From b6351a9ebdd0d08460238f7f5d472ead555687ed Mon Sep 17 00:00:00 2001 From: Moritz Kowalski Date: Wed, 11 Jun 2025 17:53:25 +0200 Subject: [PATCH] Fix bugs --- db/models.py | 49 +++++++++++++++++++++++++++++++++----------- main.py | 30 +++++++++++++-------------- templates/index.html | 2 +- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/db/models.py b/db/models.py index 9239e1a..57dfeb1 100644 --- a/db/models.py +++ b/db/models.py @@ -105,7 +105,7 @@ with engine.connect() as conn: FOREIGN KEY (prepaid_user_id) REFERENCES users_prepaid(id) ) """)) - + # create a table for drink types conn.execute(text(""" CREATE TABLE IF NOT EXISTS drink_types ( @@ -221,7 +221,6 @@ def create_postpaid_user(username: str): int: The ID of the newly created user. """ - print(f"create_postpaid_user: {username}") t_insert = text("INSERT INTO users_postpaid (username) VALUES (:username)") with engine.connect() as connection: t_select = text("SELECT * FROM users_postpaid WHERE username = :username") @@ -344,15 +343,15 @@ def drink_postpaid_user(user_id: int, drink_type: int = 1): connection.commit() t_without_drink_type = text("INSERT INTO drinks (postpaid_user_id, timestamp) VALUES (:postpaid_user_id, CURRENT_TIMESTAMP)") - t_with_drink_type = text("INSERT INTO drinks (postpaid_user_id, timestamp, drink_type) VALUES (:postpaid_user_id, CURRENT_TIMESTAMP, :drink_type)") + t_update_drink_types = text("UPDATE drink_types SET quantity = quantity - 1 WHERE id = 1") with engine.connect() as connection: - if not drink_type: - result = connection.execute(t_without_drink_type, {"postpaid_user_id": user_id}) - else: - result = connection.execute(t_with_drink_type, {"postpaid_user_id": user_id, "drink_type": drink_type}) + result = connection.execute(t_without_drink_type, {"postpaid_user_id": user_id}) if result.rowcount == 0: raise HTTPException(status_code=500, detail="Failed to create drink entry") + result2 = connection.execute(t_update_drink_types) + if result2.rowcount == 0: + raise HTTPException(status_code=500, detail="Failed to update drink type quantity") connection.commit() return result.rowcount @@ -696,8 +695,16 @@ def revert_last_drink(user_id: int, user_is_postpaid: bool, drink_id: int, drink del_t = text("DELETE FROM drinks WHERE prepaid_user_id = :user_id AND id = :drink_id") update_t = text("UPDATE users_prepaid SET money = money + :drink_cost WHERE id = :user_id") money_t = text("SELECT money FROM users_prepaid WHERE id = :user_id") + drink_id_t = text("SELECT drink_type FROM drinks WHERE id = :drink_id") + drink_reduce_t = text("UPDATE drink_types SET quantity = quantity + 1 WHERE id = :drink_type_id") with engine.connect() as connection: + # get the drink type ID from the drink ID + id_res = connection.execute(drink_id_t, {"drink_id": drink_id}).fetchone() + if not id_res: + raise HTTPException(status_code=404, detail="Drink type not found") + drink_type_id = id_res[0] + # Check if the drink exists drink_exists = connection.execute(del_t, {"user_id": user_id, "drink_id": drink_id}).rowcount > 0 if not drink_exists: @@ -709,7 +716,15 @@ def revert_last_drink(user_id: int, user_is_postpaid: bool, drink_id: int, drink raise HTTPException(status_code=404, detail="User not found") new_money = prev_money[0] + drink_cost - connection.execute(update_t, {"user_id": user_id, "drink_cost": drink_cost}) + update_res = connection.execute(update_t, {"user_id": user_id, "drink_cost": drink_cost}) + if update_res.rowcount == 0: + raise HTTPException(status_code=404, detail="User not found") + + # reduce the drink type quantity + reduce_res = connection.execute(drink_reduce_t, {"drink_type_id": drink_type_id}) + if reduce_res.rowcount == 0: + raise HTTPException(status_code=404, detail="Drink type not found") + connection.commit() _log_transaction( @@ -740,17 +755,27 @@ def update_drink_type(user_id: int, user_is_postpaid: bool, drink_id, drink_type t = text("UPDATE drinks SET drink_type = :drink_type WHERE postpaid_user_id = :user_id AND id = :drink_id") else: t = text("UPDATE drinks SET drink_type = :drink_type WHERE prepaid_user_id = :user_id AND id = :drink_id") - - t_update_quantity = text("UPDATE drink_types SET quantity = quantity - 1 WHERE id = :drink_type_id") + + t_update_quantity_old = text("UPDATE drink_types SET quantity = quantity + 1 WHERE id = :drink_type_id") + t_get_old_drink_type = text("SELECT drink_type FROM drinks WHERE id = :drink_id") + t_update_quantity_new = text("UPDATE drink_types SET quantity = quantity - 1 WHERE id = :drink_type_id") with engine.connect() as connection: + result_old_drink_type = connection.execute(t_get_old_drink_type, {"drink_id": drink_id}).fetchone() + if not result_old_drink_type: + raise HTTPException(status_code=404, detail="Old drink type not found") + old_drink_type_id = result_old_drink_type[0] + result = connection.execute(t, {"user_id": user_id, "drink_id": drink_id, "drink_type": drink_type_id}) if result.rowcount == 0: raise HTTPException(status_code=404, detail="Drink not found") - - result_quantity = connection.execute(t_update_quantity, {"drink_type_id": drink_type_id}) + + result_quantity = connection.execute(t_update_quantity_new, {"drink_type_id": drink_type_id}) if result_quantity.rowcount != 1: raise HTTPException(status_code=404, detail="Drink type not found") + result_quantity_old = connection.execute(t_update_quantity_old, {"drink_type_id": old_drink_type_id}) + if result_quantity_old.rowcount != 1: + raise HTTPException(status_code=404, detail="Old drink type not found") connection.commit() return result.rowcount diff --git a/main.py b/main.py index 0c48aaf..e165e85 100644 --- a/main.py +++ b/main.py @@ -503,29 +503,29 @@ def stats(request: Request): "user_db_id": user_db_id, "stats_drink_types": drink_types, }) - + @app.post("/add_drink_type") -def add_drink_type(request: Request, drink_type: str = Form(...), icon: str = Form(...)): +def add_drink_type(request: Request, drink_type_name: str = Form(...), drink_type_icon: str = Form(...)): user_authentik = request.session.get("user_authentik") if not user_authentik or ADMIN_GROUP not in user_authentik["groups"]: raise HTTPException(status_code=403, detail="Not allowed") user_db_id = request.session.get("user_db_id") if not user_db_id: raise HTTPException(status_code=404, detail="User not found") - if not drink_type: - raise HTTPException(status_code=400, detail="Drink type is empty") - if not icon: - raise HTTPException(status_code=400, detail="Icon is empty") - if len(drink_type) < 3 or len(drink_type) > 20: - raise HTTPException(status_code=400, detail="Drink type must be between 3 and 20 characters") - if len(icon) < 3 or len(icon) > 20: - raise HTTPException(status_code=400, detail="Icon must be between 3 and 20 characters") - if not icon.endswith(".png"): - raise HTTPException(status_code=400, detail="Icon must be a .png file") - if db.models.get_drink_type_by_name(drink_type): - raise HTTPException(status_code=400, detail="Drink type already exists") - db.models.add_drink_type(drink_type, icon) + if not drink_type_name: + raise HTTPException(status_code=400, detail="Drink type is empty") + if not drink_type_icon: + raise HTTPException(status_code=400, detail="Icon is empty") + if len(drink_type_name) < 3 or len(drink_type_name) > 20: + raise HTTPException(status_code=400, detail="Drink type must be between 3 and 20 characters") + if len(drink_type_icon) < 5 or len(drink_type_icon) > 20: + raise HTTPException(status_code=400, detail="Icon must be between 3 and 20 characters") + if not drink_type_icon.endswith(".png"): + raise HTTPException(status_code=400, detail="Icon must be a .png file") + + print(f"Adding drink type: {drink_type_name} with icon: {drink_type_icon}") + db.models.add_drink_type(drink_type_name, drink_type_icon) return RedirectResponse(url="/", status_code=303) diff --git a/templates/index.html b/templates/index.html index 01b08d5..c7236c8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -95,7 +95,7 @@ content %}
Letztes Getränk:
- Typ: {{ last_drink.drink_type }}
+ Typ: {{ last_drink.drink_type_name }}
Zeit: {{ last_drink.timestamp }}