From 0b16720dda8b9d98aa8ebed9fc844120f0a1b164 Mon Sep 17 00:00:00 2001 From: Alfonso Date: Thu, 7 Mar 2024 16:33:14 +0100 Subject: [PATCH 01/40] Added user context --- gameservice/game-service.js | 39 ++++++++++++++++++------- gatewayservice/gateway-service.js | 10 +++++++ webapp/src/App.js | 17 ++++++----- webapp/src/App.jsx | 21 ------------- webapp/src/components/AddUser.js | 2 ++ webapp/src/components/Login.js | 3 ++ webapp/src/components/Participation.jsx | 6 ++-- webapp/src/components/UserContext.js | 19 ++++++++++++ 8 files changed, 75 insertions(+), 42 deletions(-) delete mode 100644 webapp/src/App.jsx create mode 100644 webapp/src/components/UserContext.js diff --git a/gameservice/game-service.js b/gameservice/game-service.js index 6d06842b..cce5c102 100644 --- a/gameservice/game-service.js +++ b/gameservice/game-service.js @@ -45,24 +45,40 @@ app.post('/addgame', async (req, res) => { }); // Ruta para obtener datos de participación del usuario -app.get('/getParticipation/:userId', async (req, res) => { +app.get('/getParticipation/:username', async (req, res) => { try { - const userId = req.params.userId; + const { username } = req.params; + const user = await User.findOne({ username }); // Consulta para obtener los datos de participación del usuario const participationData = await Game.aggregate([ - { $match: { user: mongoose.Types.ObjectId(userId) } }, + { $match: { user } }, { $group: { _id: null, - totalGames: { $sum: 1 }, //$sum -> Returns a sum of numerical values - correctAnswers: { $sum: { $size: { - $filter: { - input: "$answers", as: "answer", cond: "$$answer.isCorrect" } - } } }, - incorrectAnswers: { $sum: { $size: { - $filter: { input: "$answers", as: "answer", cond: { $eq: ["$$answer.isCorrect", false] } } - } } }, + totalGames: { $sum: 1 }, + correctAnswers: { + $sum: { + $size: { + $filter: { + input: "$answers", + as: "answer", + cond: "$$answer.isCorrect" + } + } + } + }, + incorrectAnswers: { + $sum: { + $size: { + $filter: { + input: "$answers", + as: "answer", + cond: { $eq: ["$$answer.isCorrect", false] } + } + } + } + }, totalTime: { $sum: "$totalTime" }, }, }, @@ -81,6 +97,7 @@ app.get('/getParticipation/:userId', async (req, res) => { } }); + const server = app.listen(port, () => { console.log(`Games Service listening at http://localhost:${port}`); }); diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 59c4c94c..f7ea06e0 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -80,6 +80,16 @@ app.post('/addgame', async (req, res) => { } }); +app.post('/getParticipation/:username', async (req, res) => { + try { + // Forward the login request to the authentication service + const participationResponse = await axios.post(gameServiceUrl+'/getParticipation/:username', req.body); + res.json(participationResponse.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}`); diff --git a/webapp/src/App.js b/webapp/src/App.js index db9ed9b2..5ec488ef 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -5,6 +5,7 @@ import { Start } from './components/Start' import { Game } from './components/Game' import { Participation } from './components/Participation' import User from './components/User' +import { UserProvider } from './UserContext'; function App() { const [menuState, setMenuState] = useState(0) @@ -14,13 +15,15 @@ function App() { } return ( - <> - {menuState === 0 && goTo(x)}/>} - {menuState > 0 &&