forked from technolyceum/ai6-m2
Updated starter pack
This commit is contained in:
55
starter_templates/docs/roles/backend-developer-dima.md
Normal file
55
starter_templates/docs/roles/backend-developer-dima.md
Normal file
@@ -0,0 +1,55 @@
|
||||
// 1. This file should contain backend API documentation
|
||||
// TODO: Import Flask and PyMongo (line 1-2)
|
||||
from flask import Flask, session, redirect, url_for, request, jsonify
|
||||
from flask_pymongo import PyMongo
|
||||
|
||||
// TODO: Connect to MongoDB database (line 4-6)
|
||||
app = Flask(__name__)
|
||||
app.config['MONGO_URI'] = 'mongodb://localhost:27017/student_db'
|
||||
mongo = PyMongo(app)
|
||||
|
||||
// TODO: Define API endpoint for getting questions (line 8-10)
|
||||
@app.route('/get_question')
|
||||
def get_question():
|
||||
question_num = session.get('current_question', 0)
|
||||
question = mongo.db.questions.find_one({"question_number": question_num})
|
||||
|
||||
if question:
|
||||
return jsonify({
|
||||
"question": question['question'],
|
||||
"options": question['options'],
|
||||
"question_number": question_num,
|
||||
"current_prize": calculate_prize(question_num)
|
||||
})
|
||||
else:
|
||||
return jsonify({"game_over": True, "final_score": calculate_final_score()})
|
||||
|
||||
// TODO: Define API endpoint for submitting answers (line 12-14)
|
||||
@app.route('/answer', methods=['POST'])
|
||||
def answer_question():
|
||||
user_answer = request.json.get('answer')
|
||||
question_num = session.get('current_question', 0)
|
||||
current_question = mongo.db.questions.find_one({"question_number": question_num})
|
||||
|
||||
is_correct = current_question['options'][current_question['correct_answer']] == user_answer
|
||||
|
||||
if is_correct:
|
||||
session['current_question'] = question_num + 1
|
||||
return jsonify({
|
||||
"correct": True,
|
||||
"correct_answer": current_question['correct_answer'],
|
||||
"game_over": question_num + 1 >= TOTAL_QUESTIONS
|
||||
})
|
||||
else:
|
||||
return jsonify({
|
||||
"correct": False,
|
||||
"correct_answer": current_question['correct_answer'],
|
||||
"game_over": True
|
||||
})
|
||||
|
||||
// TODO: Add error handling for database connection (line 16-18)
|
||||
try:
|
||||
mongo.db.command("ping")
|
||||
print("Successfully connected to MongoDB!")
|
||||
except Exception as e:
|
||||
print(f"MongoDB connection error: {e}")
|
||||
Reference in New Issue
Block a user