From f027edd60efc8aa949791fdc285c1bbfa59fdb2c Mon Sep 17 00:00:00 2001 From: angelalvaigle Date: Thu, 7 Mar 2024 00:27:34 +0100 Subject: [PATCH] answerservice v1 --- questions/answerservice/answer-model.js | 18 +++++++- questions/answerservice/answer-service.js | 15 ++++--- webapp/src/components/AddAnswer.js | 51 +++++++++++++++++++++-- webapp/src/components/GetAnswer.js | 11 ++--- 4 files changed, 81 insertions(+), 14 deletions(-) diff --git a/questions/answerservice/answer-model.js b/questions/answerservice/answer-model.js index 9b998ce9..e6c8bb8f 100644 --- a/questions/answerservice/answer-model.js +++ b/questions/answerservice/answer-model.js @@ -5,7 +5,23 @@ const answerSchema = new mongoose.Schema({ type: String, required: true, }, - value: { + attribute: { + type: String, + required: true, + }, + right: { + type: String, + required: true, + }, + wrong1: { + type: String, + required: true, + }, + wrong2: { + type: String, + required: true, + }, + wrong3: { type: String, required: true, }, diff --git a/questions/answerservice/answer-service.js b/questions/answerservice/answer-service.js index 70c65d36..62b41ea4 100644 --- a/questions/answerservice/answer-service.js +++ b/questions/answerservice/answer-service.js @@ -32,7 +32,11 @@ app.post('/addanswer', async (req, res) => { // const hashedPassword = await bcrypt.hash(req.body.password, 10); const newAnswer = new Answer({ type: req.body.type, - value: req.body.value, + attribute: req.body.attribute, + right: req.body.right, + wrong1: req.body.wrong1, + wrong2: req.body.wrong2, + wrong3: req.body.wrong3, }); await newAnswer.save(); @@ -45,17 +49,18 @@ app.post('/addanswer', async (req, res) => { app.post('/getanswer', async (req, res) => { try { // Check if required fields are present in the request body - validateRequiredFields(req, ['type', 'value']); + validateRequiredFields(req, ['type', 'attribute']); - const { type, value } = req.body; + const { type, attribute } = req.body; // Find the answer by type in the database - const answer = await Answer.findOne({ value }); + const answer = await Answer.findOne({ attribute }); // Check if the answer exists if (answer) { // Respond with the answer information - res.json({ type: answer.type, value: answer.value }); + res.json({ type: answer.type, attribute: answer.attribute, right: answer.right, + wrong1: answer.wrong1, wrong2: answer.wrong2, wrong3: answer.wrong3, }); } else { res.status(401).json({ error: 'Answer not found' }); } diff --git a/webapp/src/components/AddAnswer.js b/webapp/src/components/AddAnswer.js index 112ea300..c2651e47 100644 --- a/webapp/src/components/AddAnswer.js +++ b/webapp/src/components/AddAnswer.js @@ -12,12 +12,57 @@ const AddAnswer = () => { const [openSnackbar, setOpenSnackbar] = useState(false); const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; + + const endpointUrl = 'https://query.wikidata.org/sparql'; + const sparqlQuery = `#List of present-day countries and capital(s) + SELECT DISTINCT ?country ?countryLabel ?capital ?capitalLabel + WHERE + { + ?country wdt:P31 wd:Q3624078 . + #not a former country + FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} + #and no an ancient civilisation (needed to exclude ancient Egypt) + FILTER NOT EXISTS {?country wdt:P31 wd:Q28171280} + OPTIONAL { ?country wdt:P36 ?capital } . + + SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } + } + ORDER BY ?countryLabel`; + + const generateAnswer = async () => { + const fullUrl = endpointUrl + '?query=' + encodeURIComponent( sparqlQuery ); + const headers = { 'Accept': 'application/sparql-results+json' }; + const response = await fetch( fullUrl, { headers } ); + const jsonResponse = await response.json(); + + const index = Math.floor(Math.random() * 200); + console.log(jsonResponse.results.bindings[index].capitalLabel.value + + ' es la capital de ' + + jsonResponse.results.bindings[index].countryLabel.value ); + const answer = { + type: 'capital', + attribute: jsonResponse.results.bindings[index].countryLabel.value, + right: jsonResponse.results.bindings[index].capitalLabel.value, + wrong1: jsonResponse.results.bindings[index+1].capitalLabel.value, + wrong2: jsonResponse.results.bindings[index+2].capitalLabel.value, + wrong3: jsonResponse.results.bindings[index+3].capitalLabel.value, + }; + return answer; + }; + const addAnswer = async () => { - const type = 'capital'; - const value = 'Francia'; + + const answer = await generateAnswer(); + const type = answer.type; + const attribute = answer.attribute; + const right = answer.right; + const wrong1 = answer.wrong1; + const wrong2 = answer.wrong2; + const wrong3 = answer.wrong3; + console.log('AddAnswer'); try { - await axios.post(`${apiEndpoint}/addanswer`, { type, value }); + await axios.post(`${apiEndpoint}/addanswer`, { type, attribute, right, wrong1, wrong2, wrong3 }); setOpenSnackbar(true); } catch (error) { setError(error.response.data.error); diff --git a/webapp/src/components/GetAnswer.js b/webapp/src/components/GetAnswer.js index 47207ee2..2fd23a0b 100644 --- a/webapp/src/components/GetAnswer.js +++ b/webapp/src/components/GetAnswer.js @@ -1,4 +1,4 @@ -// src/components/Login.js +// src/components/GetAnswer.js import React, { useState } from 'react'; import axios from 'axios'; import { Container, Typography, TextField, Button, Snackbar } from '@mui/material'; @@ -14,11 +14,12 @@ const GetAnswer = () => { const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; const getAnswer = async () => { const type = 'capital'; - const value = 'Francia'; - console.log('GetAnswer'); + const attribute = 'Russia'; + console.log('getAnswer'); try { - const response = await axios.post(`${apiEndpoint}/getanswer`, { type, value } ); - console.log('La ' + response.data.type + ' de ' + response.data.value + ' es...'); + const response = await axios.post(`${apiEndpoint}/getanswer`, { type, attribute } ); + console.log('La ' + response.data.type + ' de ' + response.data.attribute + ' es ' + response.data.right); + console.log('y ' + response.data.wrong1 + ', ' + response.data.wrong2 + ' y ' + response.data.wrong3 + ' no lo son ;-)'); setOpenSnackbar(true); } catch (error) { setError(error.response.data.error);