Skip to content

Commit

Permalink
Merge pull request #42 from Arquisoft/laura
Browse files Browse the repository at this point in the history
Laura
  • Loading branch information
uo277310 authored Feb 20, 2024
2 parents 9d4fc2e + adeaf65 commit 7d24723
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
47 changes: 47 additions & 0 deletions questions/answerservice/answer-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const bcrypt = require('bcrypt');
const Answer = require('./answer-model');

let mongoServer;
let app;

//test answer
const answerTest = {

answerBody: "Cervantes",
typeAnswer: "autor"
};

async function addanswer(answerTest){
const newAnswer = new Answer({
answerBody: answerTest.answerBody,
typeAnswer: answerTest.typeAnswer
});

await newAnswer.save();
}

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./answer-service');
//Load database with initial conditions
await addanswer(answerTest);
});

afterAll(async () => {
app.close();
await mongoServer.stop();
});

describe('Answer Service', () => {
it('Should perform an addRecord operation /addAnswer', async () => {
const response = await request(app).post('/addAnswer').send(answerTest);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('answerBody', 'Cervantes');
expect(response.body).toHaveProperty('typeAnswer', 'autor');
});

});
24 changes: 4 additions & 20 deletions questions/createservice/create-service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Question = require('./create-model')
const Question = require('./create-model');



const app = express();
const port = 8005;
Expand Down Expand Up @@ -30,6 +32,7 @@ app.post('/addQuestion', async (req, res) => {
}
});


//obtiene una pregunta de forma aleatoria
app.post('/getQuestionBody', async (req, res) => {
try {
Expand All @@ -39,25 +42,6 @@ app.post('/getQuestionBody', async (req, res) => {
//saco una pregunta de forma aleatoria
const rQuestion = await Question.aggregate([{ $sample: { size: 1 } }]);

/*const rQuestionResult=null;
if (rQuestion && rQuestion.length > 0) {
//obtengo la pregunta
const rQuestionResult = rQuestion[0];
const newQuestion1 = new Question({
questionBody: rQuestionResult.questionBody,
typeQuestion: rQuestionResult.typeQuestion,
typeAnswer: rQuestionResult.typeAnswer,
});
res.json(newQuestion1);
} else {
console.log("No hay preguntas");
}*/

res.json(rQuestion[0]);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
Expand Down
55 changes: 25 additions & 30 deletions webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ const Game=() =>{
try {

const response = await axios.post(`${apiEndpoint}/getQuestionBody`);
// const { questionBody: questionBodyData } = response.data;


setquestionBody(response.data.questionBody);//obtengo los datos del cuerpo de la pregunta
//setQuestionType(response.data.typeQuestion);
//setQuestionType(response.data.typeAnswer);
setQuestionType(response.data.typeQuestion);
setAnswerType(response.data.typeAnswer);

} catch (error) {
console.error("Error al obtener la pregunta aleatoria", error);
}
};






// Obtener info de wikidata segun el tipo de la pregunta y la respuesta para esa pregunta
//questionType, answerType
const obtenerInformacionWikidata = async () => {
const obtenerPais = async () => {

try {
// Consulta SPARQL//obtengo
Expand Down Expand Up @@ -76,45 +73,43 @@ const Game=() =>{
const index = Math.floor(Math.random() * numEles);//index al azar
const result = data.results.bindings[index];

setInformacionWikidata(result.countryLabel.value);
setInformacionWikidata(result.countryLabel.value+`?`);

} else {
console.error("Error al realizar la consulta en Wikidata. Estado de respuesta:", respuestaWikidata.status);
}
} catch (error) {
console.error("Error al realizar la consulta en Wikidata", error);
}

};



//obtenerInformacionWikidata();
/* useEffect(() => {
obtenerPreguntaAleatoria();
}, []);*/


//obtenerPreguntaAleatoria();
const handleButtonClick = () => {
obtenerPreguntaAleatoria();
//pais, libro,actor, edificio
if (questionType=='pais'){
obtenerPais();}
else if (questionType =='libro'){
obtenerPais();}
else if (questionType ==='actor'){
obtenerPais();}
else if (questionType=='edificio'){
obtenerPais();}
//etc...
obtenerPais();//para que de momento genere un pais hasta tener el resto de queries
};

return (

<div>
<h1>Pregunta</h1>
<h1>Pregunta: </h1>
<div>
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
Base de pregunta al azar de mongo: {questionBody}
</Typography>

<Button variant="contained" color="primary" onClick={obtenerPreguntaAleatoria}>
pregunta
</Button>

<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
Prueba de Wikidata, pais al azar:{informacionWikidata}
{questionBody} {informacionWikidata}
</Typography>

<Button variant="contained" color="primary" onClick={obtenerInformacionWikidata}>
Wikidata
<Button variant="contained" color="primary" onClick={handleButtonClick}>
Generar pregunta
</Button>
</div>
</div>
Expand Down

0 comments on commit 7d24723

Please sign in to comment.