diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 427bd2f1..c8de682a 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -53,6 +53,36 @@ 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.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/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-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 17ac1134..4a483b8c 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -46,6 +46,36 @@ 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({username:req.query.user}); + res.json(user); + } catch (error) { + 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/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/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 new file mode 100644 index 00000000..3c5f4219 --- /dev/null +++ b/webapp/src/pages/Perfil/Perfil.js @@ -0,0 +1,90 @@ +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 = () => { + 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(() => { + 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 ( + <> +