feat: Added price updating
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<div class="card">
|
||||
<h2>{{ drink.name }}</h2>
|
||||
<p>Price: {{ drink.price }}€</p>
|
||||
<p>Price: {{ "%.2f"|format(drink.price) }}€</p>
|
||||
<p>Stock: {{ drink.stock }}</p>
|
||||
</div>
|
||||
|
||||
@@ -26,6 +26,17 @@
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3>Adjust price</h3>
|
||||
<form method="POST" action="/drink/{{ drink.id }}/price">
|
||||
<label>
|
||||
New price:
|
||||
<input type="number" step="0.01" name="price" required>
|
||||
</label>
|
||||
<button class="button" type="submit">Change price</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3>Danger zone</h3>
|
||||
<form method="POST" action="/drink/{{ drink.id }}/delete" onsubmit="return confirm('Delete this drink?');">
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
<th>Drink</th>
|
||||
<th>User</th>
|
||||
<th>Qty</th>
|
||||
<th>Value</th>
|
||||
<th>Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -28,6 +29,7 @@
|
||||
<td>{{ t.drink_name }}</td>
|
||||
<td>{{ t.user_name }}</td>
|
||||
<td>{{ t.quantity }}</td>
|
||||
<td>{{ "%.2f"|format(t.cost) }}€</td>
|
||||
<td class="muted">
|
||||
{{ t.timestamp.strftime("%Y-%m-%d %H:%M") }}
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user