Files
FabledCurator/backend/app/api/artists.py
T
2026-05-20 12:55:51 -04:00

45 lines
1.5 KiB
Python

"""FC-3a: plural /api/artists endpoints — POST (find-or-create) +
autocomplete. The slug-routed singular /api/artist/<slug> blueprint
stays separate and unmodified."""
from quart import Blueprint, jsonify, request
from ..extensions import get_session
from ..services.artist_service import ArtistService
artists_bp = Blueprint("artists", __name__, url_prefix="/api/artists")
@artists_bp.route("", methods=["POST"])
async def create_or_get():
body = await request.get_json()
if not isinstance(body, dict):
return jsonify({"error": "invalid_body"}), 400
name = body.get("name", "")
async with get_session() as session:
svc = ArtistService(session)
try:
artist, created = await svc.find_or_create(name)
except ValueError as exc:
return jsonify({"error": "empty_name", "detail": str(exc)}), 400
return jsonify({
"id": artist.id, "name": artist.name, "slug": artist.slug,
"created": created,
}), 201
@artists_bp.route("/autocomplete", methods=["GET"])
async def autocomplete():
q = request.args.get("q") or ""
try:
limit = int(request.args.get("limit", "20"))
except ValueError:
return jsonify({"error": "invalid_limit"}), 400
if limit < 1 or limit > 100:
return jsonify({"error": "invalid_limit"}), 400
async with get_session() as session:
rows = await ArtistService(session).autocomplete(q, limit=limit)
return jsonify([
{"id": a.id, "name": a.name, "slug": a.slug} for a in rows
])