forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
654 additions
and
0 deletions.
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
questionsservice/questiongeneratorservice/questiongenerator-service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const axios = require('axios'); | ||
const mongoose = require('mongoose'); | ||
const { Question } = require('./questiongenerator-model') | ||
|
||
const app = express(); | ||
const port = 8006; | ||
|
||
const questionHistoryServiceUrl = process.env.STORE_QUESTION_SERVICE_URL || 'http://localhost:8004'; | ||
|
||
const WikiQueries = require('./wikidataExtractor/wikidataQueries'); | ||
// Connect to MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/questions'; | ||
mongoose.connect(mongoUri); | ||
const db = mongoose.connection; | ||
db.on('error', (error) => console.error(`MongoDB connection error: ${error}`)); | ||
db.once('open', () => console.log("Connected to MongoDB: %s", mongoUri)); | ||
|
||
// Middleware to parse JSON in request body | ||
app.use(express.json()); | ||
|
||
// Middleware to enable CORS (cross-origin resource sharing). In order for the API to be accessible by other origins (domains). | ||
app.use(cors()); | ||
|
||
var mockedQuestions = []; | ||
var isWikiChecked = false; | ||
var elementos; | ||
|
||
function shuffle(array) { | ||
let currentIndex = array.length; | ||
let randomIndex; | ||
|
||
// Mientras queden elementos para mezclar. | ||
while (currentIndex > 0) { | ||
// Escoge un elemento aleatorio. | ||
randomIndex = Math.floor(Math.random() * currentIndex); | ||
currentIndex--; | ||
|
||
// Intercambia el elemento actual con el elemento aleatorio. | ||
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
const generateQuestion = async () => { | ||
if (!isWikiChecked) { | ||
elementos = await WikiQueries.obtenerPaisYCapital(); | ||
isWikiChecked = true; | ||
} | ||
elementos = shuffle(elementos); | ||
mockedQuestions = [{ | ||
pregunta: "¿Cual es la capital de " + elementos[0].countryLabel + "?", | ||
respuesta_correcta: elementos[0].capitalLabel, | ||
respuestas_incorrectas: [elementos[1].capitalLabel, elementos[2].capitalLabel, elementos[3].capitalLabel] | ||
}]; | ||
console.log(mockedQuestions); | ||
} | ||
|
||
|
||
// const mockedQuestions = [ | ||
// { | ||
// pregunta: "¿Cómo me llamo?", | ||
// respuesta_correcta: "Abel", | ||
// respuestas_incorrectas: ["Federico", "Eusebio", "Gervasio"] | ||
// }, | ||
// { | ||
// pregunta: "¿Cuál es el río más largo del mundo?", | ||
// respuesta_correcta: "Amazonas", | ||
// respuestas_incorrectas: ["Nilo", "Misisipi", "Yangtsé"] | ||
// }, | ||
// { | ||
// pregunta: "¿En qué año comenzó la Segunda Guerra Mundial?", | ||
// respuesta_correcta: "1939", | ||
// respuestas_incorrectas: ["1941", "1942", "1945"] | ||
// }, | ||
// { | ||
// pregunta: "¿Quién escribió 'El Quijote'?", | ||
// respuesta_correcta: "Miguel de Cervantes", | ||
// respuestas_incorrectas: ["Garcilaso de la Vega", "Federico García Lorca", "Pablo Neruda"] | ||
// }, | ||
// { | ||
// pregunta: "¿Cuál es el símbolo químico del oro?", | ||
// respuesta_correcta: "Au", | ||
// respuestas_incorrectas: ["Ag", "Fe", "Cu"] | ||
// }, | ||
// { | ||
// pregunta: "¿Cuál es el planeta más grande del sistema solar?", | ||
// respuesta_correcta: "Júpiter", | ||
// respuestas_incorrectas: ["Saturno", "Marte", "Venus"] | ||
// }, | ||
// { | ||
// pregunta: "¿Quién pintó la 'Mona Lisa'?", | ||
// respuesta_correcta: "Leonardo da Vinci", | ||
// respuestas_incorrectas: ["Pablo Picasso", "Vincent van Gogh", "Rembrandt"] | ||
// }, | ||
// { | ||
// pregunta: "¿En qué país se encuentra la Torre Eiffel?", | ||
// respuesta_correcta: "Francia", | ||
// respuestas_incorrectas: ["Italia", "España", "Alemania"] | ||
// }, | ||
// { | ||
// pregunta: "¿Qué año marcó el fin de la Segunda Guerra Mundial?", | ||
// respuesta_correcta: "1945", | ||
// respuestas_incorrectas: ["1943", "1944", "1946"] | ||
// }, | ||
// { | ||
// pregunta: "¿Quién escribió 'Romeo y Julieta'?", | ||
// respuesta_correcta: "William Shakespeare", | ||
// respuestas_incorrectas: ["Jane Austen", "Charles Dickens", "F. Scott Fitzgerald"] | ||
// }, | ||
// { | ||
// pregunta: "¿Qué inventó Thomas Edison?", | ||
// respuesta_correcta: "Bombilla eléctrica", | ||
// respuestas_incorrectas: ["Teléfono", "Automóvil", "Avión"] | ||
// } | ||
// ] | ||
|
||
// Function to generate the required number of questions | ||
async function getQuestions(req) { | ||
const { n_preguntas, n_respuestas, tema } = req.query; | ||
var preguntas = Number(n_preguntas); | ||
var respuestas = Number(n_respuestas); | ||
var temad = String(tema); | ||
|
||
// if (isNaN(preguntas)) { | ||
// generateQuestion() | ||
// console.log("merda", mockedQuestions) | ||
// return mockedQuestions.slice(0, 4); | ||
// } | ||
// const response = []; | ||
await generateQuestion(); | ||
// for (let i = 0; i < preguntas; i++) { | ||
// response.push(mockedQuestions[i % 11]); | ||
// } | ||
return mockedQuestions; | ||
} | ||
|
||
// Route for getting questions | ||
app.get('/questions', async (req, res) => { | ||
try { | ||
// TODO: Implement logic to fetch questions from MongoDB and send response | ||
// const questions = await Question.find() | ||
const defaultQuestion = await getQuestions(req); | ||
|
||
const questionsHistoryResponse = await axios.post(questionHistoryServiceUrl + '/history/questions', defaultQuestion); | ||
res.json(defaultQuestion); | ||
} catch (error) { | ||
// res.status(500).json({ message: error.message }) | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
app.use((err, req, res, next) => { | ||
console.error(`An error occurred: ${err}`); | ||
res.status(500).send(`An error occurred: ${err.message}`); | ||
}); | ||
|
||
// Start the server | ||
const server = app.listen(port, () => { | ||
console.log(`Questions Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
server.on('close', () => { | ||
// Close the Mongoose connection | ||
mongoose.connection.close(); | ||
}); | ||
|
||
module.exports = server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const mongoose = require('mongoose'); | ||
const Question = require('./store-q-model'); | ||
|
||
const app = express(); | ||
const port = 8004; | ||
|
||
// Middleware to parse JSON in request body | ||
app.use(express.json()); | ||
|
||
// Middleware to enable CORS (cross-origin resource sharing). In order for the API to be accessible by other origins (domains). | ||
app.use(cors()); | ||
|
||
// Connect to MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/storedquestion'; | ||
mongoose.connect(mongoUri); | ||
|
||
// Function to validate required fields in the request body | ||
function validateRequiredFields(body, requiredFields) { | ||
for (const field of requiredFields) { | ||
if (!(field in body)) { | ||
throw new Error(`Missing required field: ${field}`); | ||
} | ||
} | ||
} | ||
|
||
app.post('/history/question', async (req, res) => { | ||
try { | ||
// Check if required fields are present in the request body | ||
validateRequiredFields(req.body, ['pregunta', 'respuesta_correcta', 'respuestas_incorrectas']); | ||
|
||
const newQuestion = new Question({ | ||
pregunta: req.body.pregunta, | ||
respuesta_correcta: req.body.respuesta_correcta, | ||
respuestas_incorrectas: req.body.respuestas_incorrectas, | ||
createdAt: req.body.createdAt | ||
}); | ||
|
||
await newQuestion.save(); | ||
res.json(newQuestion); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
app.post('/history/questions', async (req, res) => { | ||
try { | ||
// Check if required fields are present in the request body | ||
if (!Array.isArray(req.body)) { | ||
throw new Error('Invalid request format. Expected an array of questions.'); | ||
} | ||
for (const question of req.body) { | ||
validateRequiredFields(question, ['pregunta', 'respuesta_correcta', 'respuestas_incorrectas']); | ||
} | ||
const newQuestions = []; | ||
|
||
for (const questionData of req.body) { | ||
const newQuestion = new Question({ | ||
pregunta: questionData.pregunta, | ||
respuesta_correcta: questionData.respuesta_correcta, | ||
respuestas_incorrectas: questionData.respuestas_incorrectas, | ||
createdAt: questionData.createdAt | ||
}); | ||
|
||
await newQuestion.save(); | ||
newQuestions.push(newQuestion); | ||
} | ||
|
||
res.json(newQuestions); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
|
||
app.get('/history/questions', async (req, res) => { | ||
try { | ||
const questions = await Question.find({}); // Get all questions | ||
res.json(questions); | ||
/*res.json([{ //FORMATO VIEJO | ||
question: '¿Cuál es la capital de la comunidad autónoma de Casstilla y León?', | ||
answers: ['Segovia','León','Valladolid','Ninguna'], | ||
}]);*/ | ||
/*res.json([{ | ||
pregunta: '¿Cuál es la capital de la comunidad autónoma de Castilla y León?', | ||
respuesta_correcta: 'Ninguna', | ||
respuestas_incorrectas: ['Segovia','León','Valladolid'] | ||
}]);*/ | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
const server = app.listen(port, () => { | ||
console.log(`Store questions service listening at http://localhost:${port}`); | ||
}); | ||
|
||
// Listen for the 'close' event on the Express.js server | ||
server.on('close', () => { | ||
// Close the Mongoose connection | ||
mongoose.connection.close(); | ||
}); | ||
|
||
module.exports = server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
button { | ||
font-size: 20px; | ||
width: 200px; | ||
height: 50px; | ||
justify-content: center; | ||
margin-bottom: 15px; | ||
} | ||
|
||
.questionStructure .answers { | ||
display: flex; | ||
justify-content: center; /* Alinea los elementos en el centro horizontal / | ||
align-items: center; / Alinea los elementos en el centro vertical / | ||
height: 100vh; / Ajusta la altura al 100% del viewport */ | ||
} | ||
|
||
.allAnswers { | ||
width:100%; | ||
display: block; | ||
} | ||
|
||
.asnwers { | ||
width:100%; | ||
} | ||
|
||
|
||
.questionFirstGame { | ||
height: 100%; | ||
display: inline-block; | ||
} | ||
|
||
.questionText { | ||
display: flex; | ||
margin-bottom: 25px; | ||
|
||
} |
Oops, something went wrong.