From 20874f0fb2c7e75f7a9c2d45d7995570fcb7f67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Wed, 6 Mar 2024 17:46:10 +0100 Subject: [PATCH] Added question service and linked it with the gateway service --- gatewayservice/gateway-service.js | 21 +++++ questionservice/package-lock.json | 20 +++++ questionservice/package.json | 5 ++ questionservice/question-service.js | 126 ++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 questionservice/package-lock.json create mode 100644 questionservice/package.json create mode 100644 questionservice/question-service.js diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 88b84c8..7dd7eca 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -8,6 +8,7 @@ const port = 8000; const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002'; const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001'; +const questionServiceUrl = process.env.QUESTION_SERVICE_URL || 'http://localhost:8010'; app.use(cors()); app.use(express.json()); @@ -41,6 +42,26 @@ app.post('/adduser', async (req, res) => { } }); +app.get('/flags/question', async (req, res) => { + try { + // Forward the request to the question service + const questionResponse = await axios.get(questionServiceUrl+'/flags/question', req.body); + res.json(questionResponse.data); + } catch (error) { + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); + +app.get('/flags/answer', async (req, res) => { + try { + // Forward the request to the question service + const questionResponse = await axios.get(questionServiceUrl+'/flags/answer', req.body); + res.json(questionResponse.data); + } catch (error) { + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); + // Start the gateway service const server = app.listen(port, () => { console.log(`Gateway Service listening at http://localhost:${port}`); diff --git a/questionservice/package-lock.json b/questionservice/package-lock.json new file mode 100644 index 0000000..9a22c15 --- /dev/null +++ b/questionservice/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "questionservice", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "wikibase-sdk": "^8.1.1" + } + }, + "node_modules/wikibase-sdk": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/wikibase-sdk/-/wikibase-sdk-8.1.1.tgz", + "integrity": "sha512-1NjMnfNQ4OaLh0dFAeTMvV3vGAq6HXsNKGfYUJYOVyBPGBDMunlY3QZ8+72hLV5FiKmc6Bzg1xbI0jCHfHmIew==", + "engines": { + "node": ">= 10.0.0" + } + } + } +} diff --git a/questionservice/package.json b/questionservice/package.json new file mode 100644 index 0000000..bbadfd7 --- /dev/null +++ b/questionservice/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "wikibase-sdk": "^8.1.1" + } +} diff --git a/questionservice/question-service.js b/questionservice/question-service.js new file mode 100644 index 0000000..350aa64 --- /dev/null +++ b/questionservice/question-service.js @@ -0,0 +1,126 @@ +const WBK = require('wikibase-sdk') +const wbk = WBK({ + instance: 'https://www.wikidata.org', + sparqlEndpoint: 'https://query.wikidata.org/sparql' // Required to use `sparqlQuery` and `getReverseClaims` functions, optional otherwise +}) +const express = require('express'); +const app = express(); +const port = 8010; + +app.use(express.static('public')); + +app.use(express.text()); + +//Correct image +var correctAnswerFlag +//Associates flags with their countries +var flagToCountryMap = new Map() + +class WIQ_API{ + /** + * + * @returns JSON with the question and the flags + */ + async getQuestionAndCountryFlags() { + //Reset the map for the new question + flagToCountryMap = new Map() + + //Num of fetched countries + const countriesNum = 100 + + //Required by wikidata to accept the request + const headers = new Headers(); + headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' + +' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'); + + const sparql = `SELECT ?país ?paísLabel ?imagen_de_la_bandera WHERE { + SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } + ?país wdt:P31 wd:Q6256. + OPTIONAL { ?país wdt:P41 ?imagen_de_la_bandera. } + } + LIMIT ${countriesNum}` + + //Constructing the url for the wikidata request + var url = wbk.sparqlQuery(sparql); + + const response = await fetch(url, { headers }); + const data = await response.json() + + var chosenNums = []; + const numOfChosen = 4 + // Generate n random numbers + for (let i = 0; i < numOfChosen; i++) { + this.#getRandomNumNotInSetAndUpdate(countriesNum, chosenNums) + } + + const countries = [] + const imgs = [] + for(var i=0;i { + const question = await wiq.getQuestionAndCountryFlags() + res.json(question); +}); + +/** + * Gets a response indicating if the chosen flag img was correct or not + * @param {string} req - Flag img url selected by the player + * @param {Object} res - JSON containing whether the answer was correct "true" + * or not "false". In case it was incorrect, the chosen + * country will be returned as well +*/ +app.get('flags/answer', (req, res) => { + const answeredFlag = req.body + if(correctAnswerFlag==answeredFlag){ + res.json({ + correct: "true" + }) + } else { + res.json({ + correct: "false", + country: `${flagToCountryMap.get(answeredFlag)}` + }) + } +}); + +app.listen(port, () => { + console.log(`Questions service listening on http://localhost:${port}`); +}); \ No newline at end of file