Skip to content

Commit

Permalink
Added call to the question store service API for storing the generate…
Browse files Browse the repository at this point in the history
…d questions (pending for testing)
  • Loading branch information
AbelMH1 committed Mar 13, 2024
1 parent 0692855 commit ac5a2a2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 21 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ services:
build: ./questionsservice/questiongeneratorservice
depends_on:
- mongodb_wiki
- storequestionservice
ports:
- "8007:8006"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb_wiki:27017/questions
STORE_QUESTION_SERVICE_URL: http://storequestionservice:8004

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand Down
93 changes: 93 additions & 0 deletions questionsservice/questiongeneratorservice/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions questionsservice/questiongeneratorservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"homepage": "https://github.com/arquisoft/wiq_es6c#readme",
"dependencies": {
"express": "^4.18.2",
"axios": "^1.6.5",
"cors": "^2.8.5",
"mongoose": "^8.2.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const mongoose = require('mongoose');
const { Question } = require('./questiongenerator-model')
const { Request } = require('./questiongenerator-model')

const app = express();
const port = 8006;

const WikiQueries = require('./wikidataExtractor/wikidataQueries')
const questionHistoryServiceUrl = process.env.QUESTION_HISTORY_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);
Expand All @@ -21,40 +24,40 @@ 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
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--;
// 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]];
// 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 = await WikiQueries.obtenerPaisYCapital();
isWikiChecked = true;
}
elementos = shuffle(elementos)
console.log("caca", elementos)
elementos = shuffle(elementos);
console.log("caca", 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)
}];
console.log(mockedQuestions);
}


Expand Down Expand Up @@ -129,19 +132,21 @@ async function getQuestions(req) {
// return mockedQuestions.slice(0, 4);
// }
// const response = [];
await generateQuestion()
// for (let i = 0; i < preguntas; i++) {
// response.push(mockedQuestions[i % 11]);
// }
return mockedQuestions
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 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 })
Expand Down

0 comments on commit ac5a2a2

Please sign in to comment.