Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planteamiento de preguntas #44

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions questions/SPARQLQueryDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class SPARQLQueryDispatcher {
constructor( endpoint ) {
this.endpoint = endpoint;
}

query( sparqlQuery ) {
const fullUrl = this.endpoint + '?query=' + encodeURIComponent( sparqlQuery );
const headers = { 'Accept': 'application/sparql-results+json' };

return fetch( fullUrl, { headers } ).then( body => body.json() );
}
}
45 changes: 22 additions & 23 deletions questions/createqservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser');
const User = require('./question-model')
const User = require('./question-model');

import SPARQLQueryDispatcher from '../SPARQLQueryDispatcher.js';

const app = express();
const port = 8001;
Expand All @@ -16,35 +18,32 @@ const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);


const preguntas = new Map();
setPreguntas();

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
}
}
}
const endpointUrl = 'https://query.wikidata.org/sparql';
const queryDispatcher = new SPARQLQueryDispatcher( endpointUrl );

app.post('/adduser', async (req, res) => {
app.post('/question', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['username', 'password']);

// Encrypt the password before saving it
const hashedPassword = await bcrypt.hash(req.body.password, 10);

const newUser = new User({
username: req.body.username,
password: hashedPassword,
});

await newUser.save();
res.json(newUser);
getAtributo();
} catch (error) {
res.status(400).json({ error: error.message });
}});

function setPreguntas(){
preguntas.set("capital","Cuál es la capital de");
}

function getAtributo(){
const sparqlQuery = `SELECT ?capitalLabel WHERE {
?capital wdt:P31 wd:Q6256;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}`;

queryDispatcher.query( sparqlQuery ).then( console.log );
}

const server = app.listen(port, () => {
console.log(`User Service listening at http://localhost:${port}`);
});
Expand Down
15 changes: 12 additions & 3 deletions webapp/src/components/GamesPanel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import React from 'react';
import Grid from '@mui/material/Grid';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import {Typography, Button} from '@mui/material';
import axios from 'axios';

function Game({ title }) {
const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';
const question = async () => {
const response = await axios.post(`${apiEndpoint}/question`);
};

return (
<Grid item xs={4}>
<Paper sx={{ p: 2, bgcolor: 'red' }}>
<Typography variant="h6">{title}</Typography>
<Button variant="contained" color="primary" onClick={question}>
Question
</Button>
</Paper>
</Grid>
);
Expand All @@ -17,8 +26,8 @@ function GamesPanel() {
return (
<Grid container spacing={2}>
<Game title="Saber y ganar" />
<Game title="Elemento 2" />
<Game title="Elemento 3" />
<Game title=" " />
<Game title=" " />
</Grid>
);
}
Expand Down
Loading