"""Rulebook / topic REST endpoints. Wraps services/rulebooks.py. Standard Scribe auth: g.user.id is the authenticated owner; the service enforces ownership scoping. """ from __future__ import annotations from quart import Blueprint, g, jsonify, request from fabledassistant.auth import login_required import fabledassistant.services.rulebooks as rulebooks_svc rulebooks_bp = Blueprint("rulebooks", __name__, url_prefix="/api") def _uid() -> int: return g.user.id # ── Rulebooks ─────────────────────────────────────────────────────────── @rulebooks_bp.get("/rulebooks") @login_required async def list_rulebooks(): rows = await rulebooks_svc.list_rulebooks(_uid()) return jsonify({"rulebooks": [rb.to_dict() for rb in rows]}) @rulebooks_bp.post("/rulebooks") @login_required async def create_rulebook(): data = await request.get_json() or {} title = (data.get("title") or "").strip() if not title: return jsonify({"error": "title is required"}), 400 rb = await rulebooks_svc.create_rulebook( user_id=_uid(), title=title, description=data.get("description", ""), ) return jsonify(rb.to_dict()), 201 @rulebooks_bp.get("/rulebooks/") @login_required async def get_rulebook(rulebook_id: int): rb = await rulebooks_svc.get_rulebook(rulebook_id, _uid()) if rb is None: return jsonify({"error": "rulebook not found"}), 404 return jsonify(rb.to_dict()) @rulebooks_bp.patch("/rulebooks/") @login_required async def update_rulebook(rulebook_id: int): data = await request.get_json() or {} fields = {k: v for k, v in data.items() if k in ("title", "description")} rb = await rulebooks_svc.update_rulebook(rulebook_id, _uid(), **fields) if rb is None: return jsonify({"error": "rulebook not found"}), 404 return jsonify(rb.to_dict()) @rulebooks_bp.delete("/rulebooks/") @login_required async def delete_rulebook(rulebook_id: int): await rulebooks_svc.delete_rulebook(rulebook_id, _uid()) return "", 204 # ── Topics ────────────────────────────────────────────────────────────── @rulebooks_bp.get("/rulebooks//topics") @login_required async def list_topics(rulebook_id: int): try: rows = await rulebooks_svc.list_topics(rulebook_id, _uid()) except ValueError as exc: return jsonify({"error": str(exc)}), 404 return jsonify({"topics": [t.to_dict() for t in rows]}) @rulebooks_bp.post("/rulebooks//topics") @login_required async def create_topic(rulebook_id: int): data = await request.get_json() or {} title = (data.get("title") or "").strip() if not title: return jsonify({"error": "title is required"}), 400 try: topic = await rulebooks_svc.create_topic( rulebook_id=rulebook_id, user_id=_uid(), title=title, description=data.get("description", ""), order_index=data.get("order_index", 0), ) except ValueError as exc: return jsonify({"error": str(exc)}), 404 return jsonify(topic.to_dict()), 201 @rulebooks_bp.patch("/rulebook-topics/") @login_required async def update_topic(topic_id: int): data = await request.get_json() or {} fields = { k: v for k, v in data.items() if k in ("title", "description", "order_index") } topic = await rulebooks_svc.update_topic(topic_id, _uid(), **fields) if topic is None: return jsonify({"error": "topic not found"}), 404 return jsonify(topic.to_dict()) @rulebooks_bp.delete("/rulebook-topics/") @login_required async def delete_topic(topic_id: int): await rulebooks_svc.delete_topic(topic_id, _uid()) return "", 204