diff --git a/backend/app.py b/backend/app.py index 6444b96..8a2cfe1 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,4 +1,5 @@ import os +import click from flask import Flask, send_from_directory from flask_cors import CORS from flask_migrate import Migrate @@ -87,4 +88,35 @@ def create_app(config_name=None): """Health check endpoint""" return {'status': 'ok', 'message': 'Trivia Game API is running'}, 200 + @app.cli.command('transfer-questions') + @click.argument('username') + def transfer_questions(username): + """Transfer ownership of ALL questions to the specified user. + + USERNAME is the preferred_username of the target user. + """ + from backend.models import User, Question + + user = User.query.filter_by(preferred_username=username).first() + if not user: + click.echo(f"Error: No user found with username '{username}'") + click.echo("Available users:") + for u in User.query.all(): + click.echo(f" - {u.preferred_username} (id={u.id}, name={u.name})") + raise SystemExit(1) + + count = Question.query.count() + if count == 0: + click.echo("No questions found in the database.") + return + + click.echo(f"This will transfer ownership of {count} question(s) to '{username}' (id={user.id}).") + if not click.confirm("Proceed?"): + click.echo("Aborted.") + return + + Question.query.update({Question.created_by: user.id}) + db.session.commit() + click.echo(f"Successfully transferred {count} question(s) to '{username}'.") + return app