UV Erweiterung
This commit is contained in:
@@ -1,14 +1,67 @@
|
||||
from flask import Flask
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from flask import Flask, request, jsonify
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DB_PATH = os.environ.get("DB_PATH", "/app/db/app.sqlite3")
|
||||
|
||||
def get_db():
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
def init_db():
|
||||
with get_db() as conn:
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS rsvps (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
attending INTEGER NOT NULL, -- 1 = ja, 0 = nein
|
||||
plus_one INTEGER NOT NULL DEFAULT 0, -- 1 = ja, 0 = nein
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.get("/")
|
||||
def index():
|
||||
return "Wedding backend is running."
|
||||
@app.post("/api/rsvp")
|
||||
def create_rsvp():
|
||||
data = request.get_json(force=True, silent=True) or {}
|
||||
name = (data.get("name") or "").strip()
|
||||
attending = data.get("attending")
|
||||
plus_one = data.get("plus_one", 0)
|
||||
|
||||
if not name:
|
||||
return jsonify({"error": "name is required"}), 400
|
||||
if attending not in (0, 1, True, False):
|
||||
return jsonify({"error": "attending must be 0/1"}), 400
|
||||
|
||||
attending_int = 1 if attending in (1, True) else 0
|
||||
plus_one_int = 1 if plus_one in (1, True) else 0
|
||||
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"INSERT INTO rsvps (name, attending, plus_one, created_at) VALUES (?, ?, ?, ?)",
|
||||
(name, attending_int, plus_one_int, datetime.utcnow().isoformat())
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
return jsonify({"ok": True})
|
||||
|
||||
@app.get("/api/rsvps")
|
||||
def list_rsvps():
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT id, name, attending, plus_one, created_at FROM rsvps ORDER BY id DESC"
|
||||
).fetchall()
|
||||
return jsonify([dict(r) for r in rows])
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
app.run(host="0.0.0.0", port=5000)
|
||||
|
||||
Reference in New Issue
Block a user