diff --git a/website/app.py b/website/app.py index 92c3370..e76fc33 100644 --- a/website/app.py +++ b/website/app.py @@ -116,7 +116,16 @@ def index(): # Route for the mindmap page @app.route('/mindmap') def mindmap(): - return render_template('mindmap.html') + try: + root_node = MindMapNode.query.filter_by(parent_id=None).first() + if not root_node: + root_node = MindMapNode(name="Wissenschaft") + db.session.add(root_node) + db.session.commit() + return render_template('mindmap.html') + except Exception as e: + app.logger.error(f"Error loading mindmap: {str(e)}") + return render_template('mindmap.html', error="Fehler beim Laden der Mindmap. Bitte versuchen Sie es erneut.") # Route for user profile @app.route('/profile') @@ -128,17 +137,28 @@ def profile(): # API routes for mindmap and thoughts @app.route('/api/mindmap') def get_mindmap(): - root_nodes = MindMapNode.query.filter_by(parent_id=None).all() - - def build_tree(node): - return { - 'id': node.id, - 'name': node.name, - 'children': [build_tree(child) for child in node.children] - } - - result = [build_tree(node) for node in root_nodes] - return jsonify(result) + try: + root_nodes = MindMapNode.query.filter_by(parent_id=None).all() + + def build_tree(node): + return { + 'id': node.id, + 'name': node.name, + 'children': [build_tree(child) for child in node.children] + } + + result = [build_tree(node) for node in root_nodes] + if not result: + # Create default root if no nodes exist + root = MindMapNode(name="Wissenschaft") + db.session.add(root) + db.session.commit() + result = [build_tree(root)] + + return jsonify(result) + except Exception as e: + app.logger.error(f"Error in get_mindmap: {str(e)}") + return jsonify({'error': 'Fehler beim Laden der Mindmap'}), 500 @app.route('/api/thoughts/', methods=['GET']) def get_thoughts(node_id): @@ -256,9 +276,27 @@ def admin(): return render_template('admin.html', users=users, nodes=nodes, thoughts=thoughts) +@app.before_first_request +def initialize_database(): + db.create_all() + # Create root node if it doesn't exist + if not MindMapNode.query.first(): + root = MindMapNode(name="Wissenschaft") + db.session.add(root) + db.session.commit() + # Flask starten if __name__ == '__main__': with app.app_context(): - # Make sure tables exist + # Initialize database and create root node db.create_all() + try: + root = MindMapNode.query.filter_by(parent_id=None).first() + if not root: + root = MindMapNode(name="Wissenschaft") + db.session.add(root) + db.session.commit() + except Exception as e: + print(f"Error initializing database: {str(e)}") + app.run(host="0.0.0.0", debug=True) \ No newline at end of file