diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 7c9ac4d7..95cfe8e7 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -1,6 +1,6 @@ // user-service.js const express = require("express"); -const cors = require('cors'); +const cors = require("cors"); const bodyParser = require("body-parser"); const cron = require("node-cron"); const GeneratorChooser = require("./questionGen/GeneratorChooser"); @@ -14,12 +14,19 @@ const MAX_QUESTIONS = 10000; // Middleware to parse JSON in request body app.use(bodyParser.json()); +// support encoded bodies +app.use(bodyParser.urlencoded({ extended: true })); app.use(cors()); app.use((req, res, next) => { if (!generadoresCargados) { - return res.status(500).json({ error: "Los generadores de preguntas aún no se han cargado. Por favor, inténtalo de nuevo más tarde." }); + return res + .status(500) + .json({ + error: + "Los generadores de preguntas aún no se han cargado. Por favor, inténtalo de nuevo más tarde.", + }); } next(); }); @@ -43,17 +50,21 @@ app.get("/questions", async (req, res) => { }); app.post("/questions", async (req, res) => { - if (req.body.n > MAX_QUESTIONS) { + const { tematicas, n } = req.body.body; + if (!n || n > MAX_QUESTIONS) { res .status(400) .json({ error: `El límite de preguntas son ${MAX_QUESTIONS}` }); + return; } try { - console.log(req.body.tematicas) - var temp = JSON.parse(req.body.tematicas) - var tematicas = temp.length !== 0 ? temp : ["paises", "literatura", "cine", "arte", "programacion"]; - var n = req.body.n ? req.body.n : 10; - var data = gen.getQuestionsPost(tematicas, n); + const temas = tematicas ? JSON.parse(tematicas) : []; + const tematicasValidas = + temas.length !== 0 + ? temas + : ["paises", "literatura", "cine", "arte", "programacion"]; + const cantidadPreguntas = parseInt(n, 10); + const data = gen.getQuestionsPost(tematicasValidas, cantidadPreguntas); res.json(data); } catch (error) { res.status(400).json({ error: error.message }); @@ -62,14 +73,15 @@ app.post("/questions", async (req, res) => { const server = app.listen(port, async () => { console.log(`Question Service listening at http://localhost:${port}`); - gen.loadGenerators() - .then(() => { - console.log("Generators loaded successfully!"); - generadoresCargados = true; - }) - .catch((error) => { - console.error("Error al cargar los generadores de preguntas:", error); - }); + gen + .loadGenerators() + .then(() => { + console.log("Generators loaded successfully!"); + generadoresCargados = true; + }) + .catch((error) => { + console.error("Error al cargar los generadores de preguntas:", error); + }); }); cron.schedule("0 3 * * *", async () => { diff --git a/webapp/src/index.js b/webapp/src/index.js index 1d0cfe4e..79736ad5 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -8,6 +8,9 @@ var r = document.getElementById('root'); r.setAttribute("data-theme", "light") const root = ReactDOM.createRoot(r); localStorage.setItem("selectedThemes", JSON.stringify(["paises", "literatura", "cine", "arte", "programacion"])); +localStorage.setItem("clasicoTime", 10); +localStorage.setItem("clasicoPreguntas", 10); +localStorage.setItem("bateriaTime", 180); root.render( // diff --git a/webapp/src/pages/Bateria/Bateria.js b/webapp/src/pages/Bateria/Bateria.js index 5474b919..dba44bd2 100644 --- a/webapp/src/pages/Bateria/Bateria.js +++ b/webapp/src/pages/Bateria/Bateria.js @@ -6,7 +6,7 @@ import { Link, useNavigate } from "react-router-dom"; const JuegoPreguntas = () => { const URL = process.env.REACT_APP_API_ENDPOINT || "http://localhost:8000" - const TIME = 180; + const TIME = localStorage.getItem("bateriaTime"); const [isLoading, setIsLoading] = useState(true); const [indicePregunta, setIndicePregunta] = useState(0); @@ -19,11 +19,17 @@ const JuegoPreguntas = () => { const navigate = useNavigate(); useEffect(() => { - fetch(URL + "/questions?tematica=all&n=9000") + fetch(URL + "/questions", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ tematicas: localStorage.getItem("selectedThemes"), n: 9000 }), + }) .then((response) => { if (!response.ok) { navigate("/home?error=1"); - return; + throw new Error("Error en la solicitud"); } return response.json(); }) @@ -36,8 +42,8 @@ const JuegoPreguntas = () => { console.error("Error al obtener las preguntas:", error); navigate("/home?error=1"); }); - // eslint-disable-next-line - },[]); + // eslint-disable-next-line + }, []); useEffect(() => { if (tiempoRestante === 0) { diff --git a/webapp/src/pages/Clasico/Clasico.js b/webapp/src/pages/Clasico/Clasico.js index b86e017d..74578981 100644 --- a/webapp/src/pages/Clasico/Clasico.js +++ b/webapp/src/pages/Clasico/Clasico.js @@ -6,7 +6,7 @@ import Footer from "../../components/Footer/Footer.js"; const JuegoPreguntas = () => { const URL = process.env.REACT_APP_API_ENDPOINT || "http://localhost:8000"; - const SECS_PER_QUESTION = useMemo(localStorage.getItem("clasicoTime")); + const SECS_PER_QUESTION = useMemo(() => localStorage.getItem("clasicoTime")); const [isLoading, setIsLoading] = useState(true); const [indicePregunta, setIndicePregunta] = useState(0); @@ -33,7 +33,7 @@ const JuegoPreguntas = () => { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ tematicas: localStorage.getItem("selectedThemes"), n: 10 }), + body: JSON.stringify({ tematicas: localStorage.getItem("selectedThemes"), n: localStorage.getItem("clasicoPreguntas") }) }) .then((response) => { if (!response.ok) { diff --git a/webapp/src/pages/Config/Config.js b/webapp/src/pages/Config/Config.js index af16e82b..80123765 100644 --- a/webapp/src/pages/Config/Config.js +++ b/webapp/src/pages/Config/Config.js @@ -6,8 +6,15 @@ import Footer from "../../components/Footer/Footer.js"; const Config = () => { const [error, setError] = useState(""); const [info, setInfo] = useState(""); - const [clasicoTime, setClasicoTime] = useState(10); - const [bateriaTime, setBateriaTime] = useState(180); + const [clasicoTime, setClasicoTime] = useState( + localStorage.getItem("clasicoTime") + ); + const [clasicoPreguntas, setClasicoPreguntas] = useState( + localStorage.getItem("clasicoPreguntas") + ); + const [bateriaTime, setBateriaTime] = useState( + localStorage.getItem("bateriaTime") + ); const handleConfig = () => { const checkboxes = document.querySelectorAll( @@ -26,6 +33,7 @@ const Config = () => { } else { localStorage.setItem("selectedThemes", JSON.stringify(selectedThemes)); localStorage.setItem("clasicoTime", clasicoTime); + localStorage.setItem("clasicoPreguntas", clasicoPreguntas); localStorage.setItem("bateriaTime", bateriaTime); alert("Cambios realizados satisfactoriamente"); @@ -36,6 +44,10 @@ const Config = () => { setClasicoTime(parseInt(event.target.value)); }; + const handleClasicoPreguntas = (event) => { + setClasicoPreguntas(parseInt(event.target.value)); + }; + const handleBateriaChange = (event) => { setBateriaTime(parseInt(event.target.value)); }; @@ -62,9 +74,8 @@ const Config = () => { -

Tiempo

- + { max="20" onChange={handleClasicoChange} /> - - + + +