feat: Added price updating

This commit is contained in:
2026-04-20 13:44:39 +01:00
parent d292791614
commit 2c89ab5e62
4 changed files with 51 additions and 4 deletions

View File

@@ -90,6 +90,33 @@ def restock_drink_post(id: int) -> Response | tuple[str, int]: # noqa: A002
return redirect(url_for("main.manage_drink_get", id=id))
@bp.post("/drink/<id>/price")
def change_price_post(id: int) -> Response | tuple[str, int]: # noqa: A002
session = SessionLocal()
drink = session.get(Drink, id)
if drink is None:
return "Drink not found", 404
price_raw = request.form.get("price")
if price_raw is None:
return "Missing fields", 400
try:
price = float(price_raw)
except ValueError:
return "Non numeric amount", 400
if price <= 0:
return "Invalid price", 400
drink.price = price
session.commit()
return redirect(url_for("main.manage_drink_get", id=id))
@bp.post("/drink/<id>/delete")
def delete_drink_post(id: int) -> Response | tuple[str, int]: # noqa: A002
session = SessionLocal()
@@ -161,8 +188,13 @@ def buy_drink_post(id: int) -> Response | tuple[str, int]: # noqa: A002
# Update stock
drink.stock -= quantity
# Calculate cost
cost = drink.price * quantity
# Create transaction
transaction = Transaction(drink=drink.id, user_name=name, quantity=quantity, timestamp=datetime.now().astimezone())
transaction = Transaction(
drink=drink.id, user_name=name, quantity=quantity, cost=cost, timestamp=datetime.now().astimezone()
)
session.add(transaction)
session.commit()
@@ -187,6 +219,7 @@ def list_transactions_get() -> str:
"drink_name": drinks[t.drink].name if t.drink in drinks else "<removed drink>",
"user_name": t.user_name,
"quantity": t.quantity,
"cost": t.cost,
"timestamp": t.timestamp,
}
for t in txs