Skip to content

Commit

Permalink
answerservice v1
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Mar 6, 2024
1 parent 1cd7294 commit f027edd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
18 changes: 17 additions & 1 deletion questions/answerservice/answer-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
15 changes: 10 additions & 5 deletions questions/answerservice/answer-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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' });
}
Expand Down
51 changes: 48 additions & 3 deletions webapp/src/components/AddAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions webapp/src/components/GetAnswer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down

0 comments on commit f027edd

Please sign in to comment.