Skip to content

Commit

Permalink
save questions in db
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Feb 23, 2024
1 parent e454103 commit acfcda9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
28 changes: 28 additions & 0 deletions game/qgservice/Question4Answers.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions game/qgservice/qg-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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', {
Expand Down
63 changes: 38 additions & 25 deletions game/qgservice/questiongenerator.js
Original file line number Diff line number Diff line change
@@ -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(),
};


// 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,
};

0 comments on commit acfcda9

Please sign in to comment.