diff --git a/app/models.py b/app/models.py index 52e782b..cde00f2 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,6 @@ from datetime import datetime -from sqlalchemy import DateTime, ForeignKey, Integer, String +from sqlalchemy import DateTime, Float, ForeignKey, Integer, String from sqlalchemy.orm import Mapped, mapped_column from .db import Base @@ -11,7 +11,7 @@ class Drink(Base): id: Mapped[int] = mapped_column(Integer, primary_key=True) name: Mapped[str] = mapped_column(String) - price: Mapped[int] = mapped_column(Integer) + price: Mapped[float] = mapped_column(Integer) stock: Mapped[int] = mapped_column(Integer) stocked_by: Mapped[str] = mapped_column(String) @@ -24,4 +24,5 @@ class Transaction(Base): drink: Mapped[int] = mapped_column(Integer, ForeignKey(Drink.id)) user_name: Mapped[str] = mapped_column(String) quantity: Mapped[int] = mapped_column(Integer) + cost: Mapped[float] = mapped_column(Float) timestamp: Mapped[datetime] = mapped_column(DateTime) diff --git a/app/routes.py b/app/routes.py index 3bdfea9..82e4856 100644 --- a/app/routes.py +++ b/app/routes.py @@ -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//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//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 "", "user_name": t.user_name, "quantity": t.quantity, + "cost": t.cost, "timestamp": t.timestamp, } for t in txs diff --git a/app/templates/drink_manage.html b/app/templates/drink_manage.html index 98d123f..b768237 100644 --- a/app/templates/drink_manage.html +++ b/app/templates/drink_manage.html @@ -11,7 +11,7 @@

{{ drink.name }}

-

Price: {{ drink.price }}€

+

Price: {{ "%.2f"|format(drink.price) }}€

Stock: {{ drink.stock }}

@@ -26,6 +26,17 @@ +
+

Adjust price

+
+ + +
+
+

Danger zone

diff --git a/app/templates/transactions.html b/app/templates/transactions.html index 7d9e7bc..ca4f301 100644 --- a/app/templates/transactions.html +++ b/app/templates/transactions.html @@ -19,6 +19,7 @@ Drink User Qty + Value Time @@ -28,6 +29,7 @@ {{ t.drink_name }} {{ t.user_name }} {{ t.quantity }} + {{ "%.2f"|format(t.cost) }}€ {{ t.timestamp.strftime("%Y-%m-%d %H:%M") }}