From a7b6d96d7b562ee70137812f90f43e1357aa9005 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 28 Mar 2024 14:46:25 +0100 Subject: [PATCH 1/3] =?UTF-8?q?A=C3=B1adiendo=20vista=20b=C3=A1sica=20de?= =?UTF-8?q?=20perfil=20de=20usuario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gatewayservice/gateway-service.js | 15 ++++++++++ statsservice/model/stats-getter.js | 2 +- users/userservice/user-service.js | 10 +++++++ webapp/src/App.js | 11 ++------ webapp/src/components/Nav/Nav.js | 1 + webapp/src/pages/Perfil/Perfil.js | 45 ++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 webapp/src/pages/Perfil/Perfil.js diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 427bd2f1..8afb9258 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -53,6 +53,21 @@ app.post("/adduser", async (req, res) => { } }); +app.get("/userInfo", async (req, res) => { + try { + // Forward the question request to the user service + const userResponse = await axios.get( + userServiceUrl + "/userInfo", + { params: req.query } + ); + res.json(userResponse.data); + } catch (error) { + res + .status(error.response.status) + .json({ error: error.response.data.error }); + } +}); + app.get("/questions", async (req, res) => { try { // Forward the question request to the question service diff --git a/statsservice/model/stats-getter.js b/statsservice/model/stats-getter.js index 04d2ccae..405e885e 100644 --- a/statsservice/model/stats-getter.js +++ b/statsservice/model/stats-getter.js @@ -78,7 +78,7 @@ class StatsForUser { displayField = "ratioCorrect"; break; case "avgTime": - sortBy = { avgTime: -1 }; + sortBy = { avgTime: 1 }; displayField = "avgTime"; break; default: diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 17ac1134..476d2374 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -46,6 +46,16 @@ app.post('/adduser', async (req, res) => { res.status(400).json({ error: error.message }); }}); +app.get('/userInfo', async (req, res) => { + try { + + const user = await User.findOne(localStorage.username); + + res.json(user); + } catch (error) { + res.status(400).json({ error: error.message }); + }}); + const server = app.listen(port, () => { console.log(`User Service listening at http://localhost:${port}`); }); diff --git a/webapp/src/App.js b/webapp/src/App.js index 087885e8..06a34187 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -12,6 +12,7 @@ import Sobre from "./pages/Sobre/Sobre.js"; import Config from "./pages/Config/Config.js"; import Login from "./components/Login/Login.js"; import Register from "./components/Register/Register.js"; +import Perfil from "./pages/Perfil/Perfil.js"; function App() { return ( @@ -29,17 +30,9 @@ function App() { } /> } /> } /> + } /> } /> - {/** Rutas privadas */} - }> - } /> - } /> - } /> - } /> - } /> - } /> - {/* Ruta por defecto */} } /> diff --git a/webapp/src/components/Nav/Nav.js b/webapp/src/components/Nav/Nav.js index 00cde2fe..8c62a3a7 100644 --- a/webapp/src/components/Nav/Nav.js +++ b/webapp/src/components/Nav/Nav.js @@ -47,6 +47,7 @@ const Nav = () => { + diff --git a/webapp/src/pages/Perfil/Perfil.js b/webapp/src/pages/Perfil/Perfil.js new file mode 100644 index 00000000..e4b3c481 --- /dev/null +++ b/webapp/src/pages/Perfil/Perfil.js @@ -0,0 +1,45 @@ +import { Box, VStack, Heading, Text, Center, Spinner } from "@chakra-ui/react"; +import React, { useEffect, useState } from "react"; +import axios from "axios"; + +const Perfil = () => { + const gatewayUrl = process.env.GATEWAY_SERVICE_URL || "http://localhost:8000"; + const [userData, setUserData] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + axios.get(gatewayUrl+"/userInfo").then((response) => { + setUserData(response.data); + setLoading(false); + }); + }, []); + + return ( +
+ + + + Detalles del Usuario + + {loading ? ( +
+ +
+ ) : ( + <> + + Nombre de Usuario: {userData.username} + + + Fecha de creación de la cuenta:{" "} + {new Date(userData.createdAt).toLocaleString()} + + + )} +
+
+
+ ); +}; + +export default Perfil; From 8988b806ef869578c4d5f527697a86d54bb23fce Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 28 Mar 2024 15:10:34 +0100 Subject: [PATCH 2/3] Ya funciona el perfil de usuario --- users/userservice/user-service.js | 4 +--- webapp/src/pages/Perfil/Perfil.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 476d2374..c1f9d6a9 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -48,9 +48,7 @@ app.post('/adduser', async (req, res) => { app.get('/userInfo', async (req, res) => { try { - - const user = await User.findOne(localStorage.username); - + const user = await User.findOne({username:req.query.user}); res.json(user); } catch (error) { res.status(400).json({ error: error.message }); diff --git a/webapp/src/pages/Perfil/Perfil.js b/webapp/src/pages/Perfil/Perfil.js index e4b3c481..c583810b 100644 --- a/webapp/src/pages/Perfil/Perfil.js +++ b/webapp/src/pages/Perfil/Perfil.js @@ -6,12 +6,21 @@ const Perfil = () => { const gatewayUrl = process.env.GATEWAY_SERVICE_URL || "http://localhost:8000"; const [userData, setUserData] = useState(null); const [loading, setLoading] = useState(true); + const [username,setUsername]=useState(localStorage.username); + const [error, setError] = useState(null); useEffect(() => { - axios.get(gatewayUrl+"/userInfo").then((response) => { - setUserData(response.data); - setLoading(false); - }); + fetch(gatewayUrl + `/userInfo?user=${username}`) + .then((response) => response.json()) + .then((data) => { + setUserData(data); + setLoading(false); + }) + .catch((error) => { + console.error('Error al obtener el perfil:', error); + setError(error.message || 'Ha ocurrido un error al obtener el perfil'); + setLoading(false); + }); }, []); return ( From abb7c8a742dfb30b8bcce21283da54adc596b888 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 28 Mar 2024 16:34:48 +0100 Subject: [PATCH 3/3] =?UTF-8?q?A=C3=B1adido=20historial=20de=20partidas=20?= =?UTF-8?q?recientes=20en=20el=20perfil=20del=20usuario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gatewayservice/gateway-service.js | 15 ++++++++++++ users/userservice/user-model.js | 1 + users/userservice/user-service.js | 22 +++++++++++++++++ webapp/src/pages/Bateria/Bateria.js | 6 +++++ webapp/src/pages/Clasico/Clasico.js | 6 +++++ webapp/src/pages/Perfil/Perfil.js | 38 ++++++++++++++++++++++++++++- webapp/src/pages/Stats/Stats.js | 1 - 7 files changed, 87 insertions(+), 2 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 8afb9258..c8de682a 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -68,6 +68,21 @@ app.get("/userInfo", async (req, res) => { } }); +app.post("/saveGameList", async (req, res) => { + try { + // Forward the save game request to the stats service + const gameResponse = await axios.post( + userServiceUrl + "/saveGameList", + req.body + ); + res.json(gameResponse.data); + } catch (error) { + res + .status(error.response.status) + .json({ error: error.response.data.error }); + } +}); + app.get("/questions", async (req, res) => { try { // Forward the question request to the question service diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 3add662e..5204bc65 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -14,6 +14,7 @@ const userSchema = new mongoose.Schema({ default: Date.now, }, games: [{ + gamemode: String, correctAnswers: Number, incorrectAnswers: Number, points: Number, diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index c1f9d6a9..4a483b8c 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -54,6 +54,28 @@ app.get('/userInfo', async (req, res) => { res.status(400).json({ error: error.message }); }}); +app.post("/saveGameList", async (req, res) => { + try { + const username = req.body.username; + const gamemode = req.body.gameMode; + const gameData = req.body.gameData; + + let user = await User.findOne({ username: username }); + + if (!user) { + return res.status(404).json({ error: "Usuario no encontrado" }); + } + const gameDataWithGamemode = { ...gameData, gamemode }; + user.games.push(gameDataWithGamemode); + + await user.save(); + + res.json({ message: "Partida guardada exitosamente" }); + } catch (error) { + res.status(400).json({ error: "Error al guardar partida en la lista: " + error.message }); + } +}); + const server = app.listen(port, () => { console.log(`User Service listening at http://localhost:${port}`); }); diff --git a/webapp/src/pages/Bateria/Bateria.js b/webapp/src/pages/Bateria/Bateria.js index 707cfe9a..57ea4a54 100644 --- a/webapp/src/pages/Bateria/Bateria.js +++ b/webapp/src/pages/Bateria/Bateria.js @@ -87,6 +87,12 @@ const JuegoPreguntas = () => { } catch (error) { console.error('Error al guardar el juego:', error); } + try { + const response = await axios.post(URL + "/saveGameList", newGame); + console.log("Solicitud exitosa:", response.data); + } catch (error) { + console.error("Error al guardar el juego:", error); + } } useEffect(() => { diff --git a/webapp/src/pages/Clasico/Clasico.js b/webapp/src/pages/Clasico/Clasico.js index a4464053..f35c5b32 100644 --- a/webapp/src/pages/Clasico/Clasico.js +++ b/webapp/src/pages/Clasico/Clasico.js @@ -170,6 +170,12 @@ const JuegoPreguntas = () => { }, }; + try { + const response = await axios.post(URL + "/saveGameList", newGame); + console.log("Solicitud exitosa:", response.data); + } catch (error) { + console.error("Error al guardar el juego en la lista de partidas:", error); + } try { const response = await axios.post(URL + "/saveGame", newGame); console.log("Solicitud exitosa:", response.data); diff --git a/webapp/src/pages/Perfil/Perfil.js b/webapp/src/pages/Perfil/Perfil.js index c583810b..3c5f4219 100644 --- a/webapp/src/pages/Perfil/Perfil.js +++ b/webapp/src/pages/Perfil/Perfil.js @@ -1,5 +1,7 @@ -import { Box, VStack, Heading, Text, Center, Spinner } from "@chakra-ui/react"; +import { Box, VStack, Heading, Text, Center, Spinner, Table, Thead, Tbody, Tr, Th, Td } from "@chakra-ui/react"; import React, { useEffect, useState } from "react"; +import Nav from "../../components/Nav/Nav.js"; +import Footer from "../../components/Footer/Footer.js"; import axios from "axios"; const Perfil = () => { @@ -24,6 +26,8 @@ const Perfil = () => { }, []); return ( + <> +