generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68ddf85
commit a0936fe
Showing
13 changed files
with
351 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use an official Node.js runtime as a parent image | ||
FROM node:20 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/questionservice | ||
|
||
# Copy package.json and package-lock.json to the working directory | ||
COPY package*.json ./ | ||
|
||
# Install app dependencies | ||
RUN npm install | ||
|
||
# Copy the app source code to the working directory | ||
COPY . . | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 8003 | ||
|
||
# Define the command to run your app | ||
CMD ["node", "question-service.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "questionservice", | ||
"version": "1.0.0", | ||
"description": "Question service, in charge of generating questions in the application", | ||
"main": "service.js", | ||
"scripts": { | ||
"start": "node question-service.js", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Arquisoft/wiq_7.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/Arquisoft/wiq_7/issues" | ||
}, | ||
"homepage": "https://github.com/Arquisoft/wiq_7#readme", | ||
"dependencies": { | ||
"bcrypt": "^5.1.1", | ||
"body-parser": "^1.20.2", | ||
"express": "^4.18.2", | ||
"mongoose": "^8.0.4" | ||
}, | ||
"devDependencies": { | ||
"jest": "^29.7.0", | ||
"mongodb-memory-server": "^9.1.5", | ||
"supertest": "^6.3.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const questionSchema = new mongoose.Schema({ | ||
type: { | ||
type: String, | ||
required: true, | ||
}, | ||
path: { | ||
type: String, | ||
required: true, | ||
}, | ||
right: { | ||
type: String, | ||
required: true, | ||
}, | ||
wrong1: { | ||
type: String, | ||
// required: true, | ||
}, | ||
wrong2: { | ||
type: String, | ||
// required: true, | ||
}, | ||
wrong3: { | ||
type: String, | ||
// required: true, | ||
}, | ||
}); | ||
|
||
const Question = mongoose.model('Question', questionSchema); | ||
|
||
module.exports = Question; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// question-service.js | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
const Question = require('./question-model'); | ||
|
||
const app = express(); | ||
const port = 8003; | ||
|
||
// Middleware to parse JSON in request body | ||
app.use(bodyParser.json()); | ||
|
||
// Connect to MongoDB | ||
const mongoUri = | ||
process.env.MONGODB_URI || 'mongodb://localhost:27017/questiondb'; | ||
mongoose.connect(mongoUri); | ||
|
||
app.post('/addquestion', async (req, res) => { | ||
try { | ||
const newQuestion = new Question({ | ||
type: req.body.type, | ||
path: req.body.path, | ||
right: req.body.right, | ||
// wrong1: req.body.wrong1, | ||
// wrong2: req.body.wrong2, | ||
// wrong3: req.body.wrong3, | ||
}); | ||
await newQuestion.save(); | ||
res.json(newQuestion); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
app.get('/questions', async (req, res) => { | ||
try { | ||
const questions = await Question.find(); // Fetch all questions | ||
res.json(questions); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
const server = app.listen(port, () => { | ||
console.log(`Question Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
// Listen for the 'close' event on the Express.js server | ||
server.on('close', () => { | ||
// Close the Mongoose connection | ||
mongoose.connection.close(); | ||
}); | ||
|
||
module.exports = server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import styled from 'styled-components'; | ||
|
||
const Wrapper = styled.section` | ||
display: grid; | ||
row-gap: 2rem; | ||
@media (min-width: 768px) { | ||
grid-template-columns: 1fr 1fr; | ||
column-gap: 1rem; | ||
} | ||
@media (min-width: 1120px) { | ||
grid-template-columns: 1fr 1fr 1fr; | ||
} | ||
.generateDb { | ||
display: grid; | ||
grid-template-columns: 1fr; | ||
row-gap: 2rem; | ||
} | ||
`; | ||
export default Wrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import Wrapper from '../assets/wrappers/AddQuestionContainer.js'; | ||
import { Snackbar } from '@mui/material'; | ||
import SPARQLQueryDispatcher from '../utils/SPARQLQueryDispatcher'; | ||
import artworksQuery from '../utils/artworksQuery'; | ||
|
||
const endpointUrl = 'https://query.wikidata.org/sparql'; | ||
const queryDispatcher = new SPARQLQueryDispatcher(endpointUrl); | ||
const apiEndpoint = | ||
process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; | ||
|
||
const AddQuestionContainer = () => { | ||
const [error, setError] = useState(''); | ||
const [openSnackbar, setOpenSnackbar] = useState(false); | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
||
const addQuestion = async ({ type, path, right }) => { | ||
try { | ||
await axios.post(`${apiEndpoint}/addquestion`, { type, path, right }); | ||
setOpenSnackbar(true); | ||
} catch (error) { | ||
console.log(error); | ||
setError(error.response?.data?.error); | ||
} | ||
}; | ||
|
||
const generateArtworks = async () => { | ||
setIsSubmitting(true); | ||
try { | ||
const query = await queryDispatcher.query(artworksQuery); | ||
// handle results | ||
const bindings = query.results.bindings; | ||
for (const result of bindings) { | ||
// Accedemos a cada propiedad | ||
const workLabel = result.workLabel.value; // Título de la obra | ||
const creator = result.creatorLabel.value; // Nombre del creador | ||
const imageUrl = result.image.value; // URL de la imagen | ||
const sitelinks = result.sitelinks.value; // Número de sitelinks | ||
|
||
// Muestra los resultados en la consola | ||
if (!workLabel.startsWith('http') && !creator.startsWith('http')) { | ||
await addQuestion({ | ||
type: 'artwork', | ||
path: workLabel, | ||
right: creator, | ||
}); | ||
} | ||
} | ||
setOpenSnackbar(true); | ||
} catch (error) { | ||
console.log('error'); | ||
setError(error.response?.data?.error); | ||
} finally { | ||
setIsSubmitting(false); | ||
} | ||
}; | ||
|
||
const handleCloseSnackbar = () => { | ||
setOpenSnackbar(false); | ||
}; | ||
|
||
return ( | ||
<Wrapper> | ||
<div className="generateDb"> | ||
<h4>Update "Por su obra..."</h4> | ||
<button | ||
type="submit" | ||
className="btn btn-block" | ||
onClick={generateArtworks} | ||
disabled={isSubmitting} // Deshabilitar el botón mientras está enviando | ||
> | ||
{isSubmitting ? 'updating...' : 'update DB'} | ||
</button> | ||
</div> | ||
|
||
<Snackbar | ||
open={openSnackbar} | ||
autoHideDuration={6000} | ||
onClose={handleCloseSnackbar} | ||
message="Questions generated successfully" | ||
/> | ||
{error && ( | ||
<Snackbar | ||
open={!!error} | ||
autoHideDuration={6000} | ||
onClose={() => setError('')} | ||
message={`Error: ${error}`} | ||
/> | ||
)} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default AddQuestionContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { AddQuestionContainer } from '../components'; | ||
|
||
const Admin = () => { | ||
return <h1>Admin Page</h1>; | ||
return <AddQuestionContainer />; | ||
}; | ||
|
||
export default Admin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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()); | ||
} | ||
} | ||
|
||
export default SPARQLQueryDispatcher; |
Oops, something went wrong.