feat: Added price updating
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user