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

Store question service #56

Merged
merged 6 commits into from
Mar 13, 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
9 changes: 0 additions & 9 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ app.post('/adduser', async (req, res) => {
}
});

app.post('/history/question', async (req, res) => {
try {
const response = await axios.post(storeQuestionsServiceUrl+'/history/question', req.body);
res.json(response.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
}
})

app.get('/history/questions', async (req, res) => {
try {
const response = await axios.get(storeQuestionsServiceUrl+'/history/questions');
Expand Down
57 changes: 28 additions & 29 deletions storeQuestionService/store-q-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/storedque
mongoose.connect(mongoUri);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
function validateRequiredFields(body, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
if (!(field in body)) {
throw new Error(`Missing required field: ${field}`);
}
}
Expand All @@ -24,7 +24,7 @@ function validateRequiredFields(req, requiredFields) {
app.post('/history/question', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['pregunta', 'respuesta_correcta','respuestas_incorrectas']);
validateRequiredFields(req.body, ['pregunta', 'respuesta_correcta', 'respuestas_incorrectas']);

const newQuestion = new Question({
pregunta: req.body.pregunta,
Expand All @@ -41,33 +41,32 @@ app.post('/history/question', async (req, res) => {
});

app.post('/history/questions', async (req, res) => {
try {
// Check if required fields are present in the request body
if (!Array.isArray(req.body)) {
throw new Error('Invalid request format. Expected an array of questions.');
}
for (const question of req.body) {
validateRequiredFields(question, ['pregunta', 'respuesta_correcta','respuestas_incorrectas']);
}

const newQuestions = [];

for (const questionData of req.body) {
const newQuestion = new Question({
pregunta: req.body.pregunta,
respuesta_correcta: req.body.respuesta_correcta,
respuestas_incorrectas: req.body.respuestas_incorrectas,
createdAt: req.body.createdAt
});
try {
// Check if required fields are present in the request body
if (!Array.isArray(req.body)) {
throw new Error('Invalid request format. Expected an array of questions.');
}
for (const question of req.body) {
validateRequiredFields(question, ['pregunta', 'respuesta_correcta', 'respuestas_incorrectas']);
}
const newQuestions = [];

for (const questionData of req.body) {
const newQuestion = new Question({
pregunta: questionData.pregunta,
respuesta_correcta: questionData.respuesta_correcta,
respuestas_incorrectas: questionData.respuestas_incorrectas,
createdAt: questionData.createdAt
});

await newQuestion.save();
newQuestions.push(newQuestion);
}

await newQuestion.save();
newQuestions.push(newQuestion);
}

res.json(newQuestions);
} catch (error) {
res.status(400).json({ error: error.message });
}
res.json(newQuestions);
} catch (error) {
res.status(400).json({ error: error.message });
}
});


Expand Down
6 changes: 3 additions & 3 deletions webapp/src/storeQuestion/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ function App(){

const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';


useEffect(() => {

const obtenerPreguntas = async () => {
try {
const response = await axios.get(`${apiEndpoint}/questions`)
console.log(response)
const response = await axios.get(`${apiEndpoint}/history/questions`)
setPreguntas(response.data);
} catch (error) {
console.error('Error al obtener las preguntas:', error.response.data.error);
}
};

obtenerPreguntas();
//eslint-disable-next-line
}, []);


return (
<>
<h2>Almacén de preguntas</h2>
Expand Down
13 changes: 10 additions & 3 deletions webapp/src/storeQuestion/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ function Question(props) {

return (
<div className='question'>
<h3>{newQuestion.question}</h3>
<h3>{newQuestion.pregunta}</h3>
<div className='grid'>
<p className='right'>{newQuestion.c_answer}</p>
{newQuestion.w_answers.map((answer, index) => (
<div className='container'>
<p className='right'>{newQuestion.respuesta_correcta}</p>
</div>
{newQuestion.respuestas_incorrectas.map((answer, index) => (
<div className='container'>
<p>{answer}</p>
</div>
))}
</div>
<div className='container footer'>
<footer>{newQuestion.createdAt.substring(0,10)}</footer>
</div>
</div>
);
}
Expand Down
15 changes: 15 additions & 0 deletions webapp/src/storeQuestion/css/questions.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,24 @@ h2{
text-align: center;
}

h3{
text-align: center;
}

#root main{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(40%, 1fr));
padding: 5px;
grid-gap: 10px;
}

.container{
display: flex;
justify-content: center;
}

.container.footer{
display: flex;
justify-content: right;
margin-right: 1em;
}