Skip to content

Commit

Permalink
question population spain
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Feb 23, 2024
1 parent d9bf7b4 commit e454103
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion game/qgservice/qg-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const { usaPopulationQuery } = require('./queries');
const { usaPopulationQuery, spainPopulationQuery } = require('./queries');
const { generateQuestionPopulation } = require('./questiongenerator');

const app = express();
Expand Down Expand Up @@ -48,6 +48,24 @@ app.get('/usaPopulation', async (req, res) => {
}
});

app.get('/spainPopulation', async (req, res) => {
try {
const sparqlResult = await executeSparqlQuery(spainPopulationQuery);
const cityPopulation = new Map();

sparqlResult.results.bindings.forEach(entry => {
const cityLabel = entry.cityLabel.value;
const population = parseFloat(entry.population.value);
cityPopulation.set(cityLabel, population);
});

const question = generateQuestionPopulation(cityPopulation);
res.json(question);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

app.get('/', (req, res) => {
res.json({
"hi": "question generator"
Expand Down
13 changes: 13 additions & 0 deletions game/qgservice/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ SELECT DISTINCT ?city ?cityLabel ?population ?country ?countryLabel
}
`;

const spainPopulationQuery = `
SELECT DISTINCT ?city ?cityLabel ?population ?country ?countryLabel
WHERE {
?city wdt:P31/wdt:P279* wd:Q515.
?city wdt:P17 ?country.
?country wdt:P17 wd:Q29.
OPTIONAL { ?city wdt:P1082 ?population. }
FILTER (?population > 100000) # Filter for cities with population > 1,000,000
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`

module.exports = {
usaPopulationQuery,
spainPopulationQuery
};

0 comments on commit e454103

Please sign in to comment.