From acfcda9c0b24ebc1c9dbbeea2444a077ead8cfa8 Mon Sep 17 00:00:00 2001 From: carlospelazas Date: Fri, 23 Feb 2024 07:31:34 +0100 Subject: [PATCH] save questions in db --- game/qgservice/Question4Answers.js | 28 +++++++++++++ game/qgservice/qg-service.js | 4 ++ game/qgservice/questiongenerator.js | 63 +++++++++++++++++------------ 3 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 game/qgservice/Question4Answers.js diff --git a/game/qgservice/Question4Answers.js b/game/qgservice/Question4Answers.js new file mode 100644 index 0000000..62074ff --- /dev/null +++ b/game/qgservice/Question4Answers.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose'); + +const question4AnswersSchema = new mongoose.Schema({ + question: { + type: String, + required: true, + }, + correctAnswer: { + type: String, + required: true, + }, + incorrectAnswer1: { + type: String, + required: true, + }, + incorrectAnswer2: { + type: String, + required: true, + }, + incorrectAnswer3: { + type: String, + required: true, + }, +}); + +const Question4Answers = mongoose.model('Question4Answers', question4AnswersSchema); + +module.exports = Question4Answers; \ No newline at end of file diff --git a/game/qgservice/qg-service.js b/game/qgservice/qg-service.js index ec7aa5b..1652a25 100644 --- a/game/qgservice/qg-service.js +++ b/game/qgservice/qg-service.js @@ -2,6 +2,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const axios = require('axios'); +const mongoose = require('mongoose'); const { usaPopulationQuery, spainPopulationQuery } = require('./queries'); const { generateQuestionPopulation } = require('./questiongenerator'); @@ -10,6 +11,9 @@ const port = 8003; app.use(bodyParser.json()); +const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; +mongoose.connect(mongoUri); + async function executeSparqlQuery(query) { try { const response = await axios.get('https://query.wikidata.org/sparql', { diff --git a/game/qgservice/questiongenerator.js b/game/qgservice/questiongenerator.js index 3915314..3402b6c 100644 --- a/game/qgservice/questiongenerator.js +++ b/game/qgservice/questiongenerator.js @@ -1,29 +1,42 @@ -// questionGenerator.js +const Question4Answers = require('./Question4Answers'); + function generateQuestionPopulation(cityPopulationMap) { - const cityPopulationArray = Array.from(cityPopulationMap); - - const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); - const [city, population] = cityPopulationArray[randomIndex]; - - const incorrectAnswers = []; - while (incorrectAnswers.length < 3) { - const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; - const [randomCityName, randomCityPopulation] = randomCity; - if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { - incorrectAnswers.push(randomCityPopulation); - } + const cityPopulationArray = Array.from(cityPopulationMap); + + const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); + const [city, population] = cityPopulationArray[randomIndex]; + + const incorrectAnswers = []; + while (incorrectAnswers.length < 3) { + const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; + const [randomCityName, randomCityPopulation] = randomCity; + if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { + incorrectAnswers.push(randomCityPopulation); } - - const question = { - question: `What is the population of ${city}?`, - correctAnswer: population, - incorrectAnswers, - }; - - return question; } - - module.exports = { - generateQuestionPopulation, + + // Create the question object + const question = { + question: `What is the population of ${city}?`, + correctAnswer: population.toString(), + incorrectAnswer1: incorrectAnswers[0].toString(), + incorrectAnswer2: incorrectAnswers[1].toString(), + incorrectAnswer3: incorrectAnswers[2].toString(), }; - \ No newline at end of file + + // Save the question to MongoDB + const newQuestion = new Question4Answers(question); + newQuestion.save() + .then(savedQuestion => { + console.log('Question saved to MongoDB:', savedQuestion); + }) + .catch(error => { + console.error('Error saving question to MongoDB:', error.message); + }); + + return question; +} + +module.exports = { + generateQuestionPopulation, +}; \ No newline at end of file