tschunkorder/backend/main.py

62 lines
No EOL
1.6 KiB
Python

from fastapi import FastAPI, HTTPException
from typing import List
from models import CreateOrderRequest, Order, DrinkType, MateType
from database import db
app = FastAPI(
title="Tschunk Order API",
description="Eine RESTful API für Tschunk-Bestellungen",
version="1.0.0"
)
@app.post("/orders", response_model=Order, status_code=201)
async def create_order(order_request: CreateOrderRequest):
"""
Erstellt eine neue Bestellung.
- **drinks**: Liste der Getränke mit Typ und Mate-Sorte
"""
if not order_request.drinks:
raise HTTPException(status_code=400, detail="Mindestens ein Getränk muss bestellt werden")
order = db.create_order(order_request.drinks)
return order
@app.get("/orders", response_model=List[Order])
async def get_all_orders():
"""
Gibt alle Bestellungen zurück.
"""
orders = db.get_all_orders()
return orders
@app.delete("/orders/{order_id}")
async def delete_order(order_id: str):
"""
Löscht eine spezifische Bestellung.
- **order_id**: ID der zu löschenden Bestellung
"""
success = db.delete_order(order_id)
if not success:
raise HTTPException(status_code=404, detail="Bestellung nicht gefunden")
return {"message": f"Bestellung {order_id} wurde erfolgreich gelöscht"}
@app.get("/drinks")
async def get_available_drinks():
"""
Gibt alle verfügbaren Getränketypen zurück.
"""
return {
"drink_types": [drink.value for drink in DrinkType],
"mate_types": [mate.value for mate in MateType]
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)