Skip to content

Commit

Permalink
Merge branch 'develop' into pablo
Browse files Browse the repository at this point in the history
  • Loading branch information
uo264915 committed Apr 11, 2024
2 parents 31ee341 + 11f5964 commit 109df3a
Show file tree
Hide file tree
Showing 21 changed files with 578 additions and 196 deletions.
15 changes: 13 additions & 2 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ app.post('/adduser', async (req, res) => {

app.post('/addgame', async (req, res) => {
try {
const userResponse = await axios.post(userServiceUrl+'/addgame', req.body);
const userResponse = await axios.post(retrieveServiceUrl+'/addgame', req.body);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
Expand All @@ -60,7 +60,7 @@ app.post('/addgame', async (req, res) => {
app.get('/getgamehistory/:username', async (req, res) => {
try {
const username = req.params.username;
const userResponse = await axios.get(`${userServiceUrl}/getgamehistory/${username}`);
const userResponse = await axios.get(`${retrieveServiceUrl}/getgamehistory/${username}`);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
Expand Down Expand Up @@ -91,6 +91,17 @@ app.get('/getquestionshistory', async (req, res) => {
}
});

app.get('/getregisteredusers', async (req, res) => {
try {
// Create a petition to the URL (le llegará a retrieve-service.js) with the option /getregisteredusers and the req.body params
const registeredUsersResponse = await axios.get(userServiceUrl+'/getregisteredusers', req.body);
// Return a json response with what we obtained on the petition
res.json(registeredUsersResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
});



// Read the OpenAPI YAML file synchronously
Expand Down
2 changes: 1 addition & 1 deletion questions/creationservice/creation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var randomQuerySelector;
// Array of the possible queries
var queries = ['SELECT DISTINCT ?questionObject ?questionObjectLabel ?answer ?answerLabel WHERE { ?questionObject wdt:P31 wd:Q6256. ?questionObject wdt:P36 ?answer. SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es".}}'];
// Array of the possible questions
var questions = ["¿Cual es la capital de "];
var questions = ["¿Cuál es la capital de "];

// Recieves the information of the query and select wich data use on the question
function getQuestionInfo(info){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require('mongoose');
const gameSchema = new mongoose.Schema({
username: { type: String, required: true },
duration: Number,
questions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Question' }],
questions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'QuestionAnswered' }],
date: { type: Date, default: Date.now } ,
percentage: Number,
totalQuestions: Number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const questionSchema = new mongoose.Schema({
userAnswer: String
});

const Question = mongoose.model('Question', questionSchema);
const QuestionAnswered = mongoose.model('QuestionAnswered', questionSchema);

module.exports = Question;
module.exports = QuestionAnswered
58 changes: 58 additions & 0 deletions questions/retrieveservice/retrieve-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require('express');
const mongoose = require('mongoose');
const Question = require('./questionshistory-model')
const Game = require('./playedGame-model')
const QuestionAnswered = require('./question-model')

const app = express();
const port = 8004;
Expand All @@ -22,6 +24,62 @@ app.get('/getquestionshistory', async (req, res) => {
res.status(200).json(solution);
});

app.post('/addgame', async (req, res) => {
try {
// Obtener los datos del juego desde el cuerpo de la solicitud
const gameData = req.body;

// Convertir las preguntas del juego en ObjectId
const questionIds = await Promise.all(gameData.questions.map(async (question) => {
const existingQuestion = await QuestionAnswered.findOne({
question: question.question,
correctAnswer: question.correctAnswer,
userAnswer: question.userAnswer
});
if (existingQuestion) {
return existingQuestion._id;
} else {
const newQuestion = new QuestionAnswered(question);
await newQuestion.save();
return newQuestion._id;
}
}));

// Reemplazar las preguntas en el juego con sus ObjectId
gameData.questions = questionIds;

// Crear una nueva instancia del modelo de juego con los datos proporcionados
const newGame = new Game(gameData);

// Guardar el nuevo juego en la base de datos
await newGame.save();

// Enviar una respuesta de éxito
res.status(200).json({ message: "Partida guardada exitosamente" });
} catch (error) {
// Manejar errores y enviar una respuesta de error con el mensaje de error
console.error("Error al guardar el juego:", error);
res.status(400).json({ error: error.message });
}
});



app.get('/getgamehistory/:username', async (req, res) => {
try {
const username = req.params.username;
console.log("Se está intentando encontrar el historial del usuario " + username);
// Buscar las partidas asociadas al nombre de usuario proporcionado
const games = await Game.find({ username }).populate('questions');
console.log("Se encontraron los juegos para " + username + ": ", games);
res.json(games);
} catch (error) {
res.status(400).json({
error: error.message
});
}
});

const server = app.listen(port, () => {
console.log(`Creation Service listening at http://localhost:${port}`);
});
Expand Down
61 changes: 7 additions & 54 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser');
const User = require('./user-model')
const Game = require('./playedGame-model')
const Question = require('./question-model')

const app = express();
const port = 8001;
Expand Down Expand Up @@ -50,60 +48,15 @@ app.post('/adduser', async (req, res) => {
}
});

app.post('/addgame', async (req, res) => {
try {
// Obtener los datos del juego desde el cuerpo de la solicitud
const gameData = req.body;

// Convertir las preguntas del juego en ObjectId
const questionIds = await Promise.all(gameData.questions.map(async (question) => {
const existingQuestion = await Question.findOne({
question: question.question,
correctAnswer: question.correctAnswer,
userAnswer: question.userAnswer
});
if (existingQuestion) {
return existingQuestion._id;
} else {
const newQuestion = new Question(question);
await newQuestion.save();
return newQuestion._id;
}
}));

// Reemplazar las preguntas en el juego con sus ObjectId
gameData.questions = questionIds;

// Crear una nueva instancia del modelo de juego con los datos proporcionados
const newGame = new Game(gameData);

// Guardar el nuevo juego en la base de datos
await newGame.save();

// Enviar una respuesta de éxito
res.status(200).json({ message: "Partida guardada exitosamente" });
} catch (error) {
// Manejar errores y enviar una respuesta de error con el mensaje de error
console.error("Error al guardar el juego:", error);
res.status(400).json({ error: error.message });
}
});

app.get('/getregisteredusers', async (req, res) => {
const registeredUsers = await User.find({});

var solution = [];
registeredUsers.forEach(row => {
solution.push([row.username,new Date(row.createdAt).toLocaleDateString()]);
});

app.get('/getgamehistory/:username', async (req, res) => {
try {
const username = req.params.username;
console.log("Se está intentando encontrar el historial del usuario " + username);
// Buscar las partidas asociadas al nombre de usuario proporcionado
const games = await Game.find({ username }).populate('questions');
console.log("Se encontraron los juegos para " + username + ": ", games);
res.json(games);
} catch (error) {
res.status(400).json({
error: error.message
});
}
res.status(200).json(solution);
});

const server = app.listen(port, () => {
Expand Down
25 changes: 25 additions & 0 deletions webapp/package-lock.json

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

2 changes: 2 additions & 0 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.2",
"animate.css": "^4.1.1",
"axios": "^1.6.5",
"bootstrap": "^5.3.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.1",
Expand Down
Binary file added webapp/public/questions-illustration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webapp/public/questions-video.mp4
Binary file not shown.
2 changes: 2 additions & 0 deletions webapp/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@
transform: rotate(360deg);
}
}


4 changes: 4 additions & 0 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function App() {

<Container component="main" maxWidth="xs">
<CssBaseline />
<div title='main'>


<Typography component="h1" variant="h5" align="center" sx={{ marginTop: 2 }}>
Bienvenido a WIQ 2024 del curso de Arquitectura del Software
</Typography>
Expand All @@ -35,6 +38,7 @@ function App() {
)}

</Typography>
</div>
</Container>

);
Expand Down
Loading

0 comments on commit 109df3a

Please sign in to comment.