Skip to content

Commit

Permalink
Merge pull request #44 from Arquisoft/generacionPreguntasSara
Browse files Browse the repository at this point in the history
planteamiento de preguntas
  • Loading branch information
holmess23 authored Feb 29, 2024
2 parents b3a8bca + 765d4c0 commit 962c85d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 52 deletions.
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

0 comments on commit 962c85d

Please sign in to comment.