Skip to content

Commit

Permalink
Mostrar la base de una pregunta aleatoria
Browse files Browse the repository at this point in the history
  • Loading branch information
uo283055 committed Feb 18, 2024
1 parent 615016f commit 2332741
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ services:
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
QUES_SERVICE_URL: http://createservice:8005
ANSW_SERVICE_URL: http://answerservice:8004

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
11 changes: 11 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const port = 8000;

const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const questionServiceUrl = process.env.QUES_SERVICE_URL || 'http://localhost:8005';

app.use(cors());
app.use(express.json());
Expand Down Expand Up @@ -41,6 +42,16 @@ app.post('/adduser', async (req, res) => {
}
});

app.post('/getQuestionBody', async (req, res) => {
try {
// Forward the add user request to the user service
const questionResponse = await axios.post(`${questionServiceUrl}/getQuestionBody`);
res.json(questionResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});

// Start the gateway service
const server = app.listen(port, () => {
console.log(`Gateway Service listening at http://localhost:${port}`);
Expand Down
47 changes: 47 additions & 0 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"node": "^21.6.2"
}
}
7 changes: 7 additions & 0 deletions questions/createservice/create-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const questionSchema = new mongoose.Schema({
required: true,
},//para filtrar por el tipo de pregunta
//autor, year etc...
typeAnswer: {
type: String,
required: true,
},//para sacar el tipo de respuesta->p.ej
//quien escribio...typeQuestion: libro
//typeAnswer : autor
//en wikidata se buscaria aleatoriamente para la pregunta un libro y para su respuesta un autor
});

const Question = mongoose.model('Question', questionSchema);
Expand Down
21 changes: 17 additions & 4 deletions questions/createservice/create-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app.post('/addQuestion', async (req, res) => {
const newQuestion1 = new Question({
questionBody: req.body.questionBody,
typeQuestion: req.body.typeQuestion,
typeAnswer: req.body.typeAnswer,
});
newQuestion1.save();
res.json(newQuestion1);
Expand All @@ -30,30 +31,42 @@ app.post('/addQuestion', async (req, res) => {
});

//obtiene una pregunta de forma aleatoria
app.get('/getQuestionBody', async (req, res) => {
app.post('/getQuestionBody', async (req, res) => {
try {

//modelo mongo
const Question = mongoose.model('Question');
//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);
res.json({ pregunta: rQuestionResult });
} else {
console.log("No hay preguntas");
}
}*/

res.json(rQuestion[0]);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});




// Start the server
const server = app.listen(port, () => {
console.log(`Auth Service listening at http://localhost:${port}`);
Expand Down
4 changes: 2 additions & 2 deletions questions/createservice/package-lock.json

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

4 changes: 2 additions & 2 deletions questions/createservice/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "authservice",
"name": "createservice",
"version": "1.0.0",
"description": " Authentication service, in charge of authenticating users in the application",
"description": " Creacion de preguntas automaticas",
"main": "service.js",
"scripts": {
"start": "node auth-service.js",
Expand Down
64 changes: 64 additions & 0 deletions webapp/src/components/Game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// src/components/Game.js
import axios from 'axios';
import React, { useState, useEffect } from 'react';
import { Container, Typography, TextField, Button, Snackbar } from '@mui/material';



import Link from '@mui/material/Link';

const Game=() =>{
const [questionBody, setquestionBody] = useState('');//pregunta aleatoria cuerpo
const [informacionWikidata, setInformacionWikidata] = useState('');
const [questionType, setQuestionType] = useState('');;//para el tipo de pregunta a buscar
const [answerType, setAnswerType] = useState('');;//para el tipo de respuesta a buscar


const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';


//para el tipo de respuesta a buscar


// Obtener pregunta una pregunta aleatoria al acceder a la url
const obtenerPreguntaAleatoria = async () => {
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);

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


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


return (

<div>
<h1>Pregunta</h1>
<div>
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
{questionBody}
</Typography>

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

export default Game;
10 changes: 10 additions & 0 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import React, { useState } from 'react';
import axios from 'axios';
import { Container, Typography, TextField, Button, Snackbar } from '@mui/material';


import Game from './Game';

import Link from '@mui/material/Link';

const Login = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
Expand Down Expand Up @@ -43,6 +48,11 @@ const Login = () => {
<Typography component="p" variant="body1" sx={{ textAlign: 'center', marginTop: 2 }}>
Your account was created on {new Date(createdAt).toLocaleDateString()}.
</Typography>

<Link to="/game" name="gameView" component={Game} variant="body2" >
vista juego
</Link>

</div>
) : (
<div>
Expand Down

0 comments on commit 2332741

Please sign in to comment.