generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Arquisoft/develop-QuestionGenerator
Prototype 1
- Loading branch information
Showing
42 changed files
with
23,405 additions
and
3,484 deletions.
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
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
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/historial | ||
|
||
# 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 8004 | ||
|
||
# Define the command to run your app | ||
CMD ["node", "historial-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,20 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const gameSchema = new mongoose.Schema({ | ||
correctAnswer: String, | ||
answers: [String], | ||
title: String, | ||
answeredRight: Boolean, | ||
selectedAnswer: String, | ||
}); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
createdAt: Date, | ||
games: { type: [gameSchema], default: [] }, | ||
}); | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
module.exports = User; |
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,87 @@ | ||
// questions-service.js | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
const User = require('./auth-model'); | ||
|
||
const app = express(); | ||
const port = 8004; | ||
|
||
// Middleware to parse JSON in request body | ||
app.use(express.json()); | ||
|
||
//Connect to MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; | ||
mongoose.connect(mongoUri); | ||
|
||
|
||
//to respond to the /saveHistorial request | ||
app.post('/saveHistorial', async (req,res) => { | ||
try { | ||
|
||
//extract the infrmation from the request to save it in the user | ||
const { question, answersArray, correctAnswer, selectedAnswer, correct, username2 } = req.body; | ||
|
||
const username = username2; | ||
|
||
//search for the user with username=user in the bd | ||
const user = await User.findOne({ username }); | ||
|
||
//creamos la pregunta para guardarla en el historial | ||
const partida = { | ||
correctAnswer: correctAnswer, | ||
answers: answersArray, | ||
title: question, | ||
answeredRight: correct, | ||
selectedAnswer: selectedAnswer | ||
} | ||
|
||
console.log(partida.selectedAnswer); | ||
//guardamos la partida en el array de preguntas del usuario | ||
user.games.push(partida); | ||
|
||
//guardamos los cambios realizados en el usuario | ||
await user.save(); | ||
|
||
res.json({ msg: "Saves the data correctly" }); | ||
|
||
} catch(error){ | ||
res.status(500).json({ error: 'Internal Server Error inside service' }); | ||
} | ||
}); | ||
|
||
//to respond to the /saveHistorial request | ||
app.post('/getHistorial', async (req,res) => { | ||
try { | ||
|
||
//extract the infrmation from the request to save it in the user | ||
const { username2 } = req.body; | ||
|
||
const username = username2; | ||
|
||
//search for the user with username=user in the bd | ||
const user = await User.findOne({ username }); | ||
|
||
const gamesUser = user.games; | ||
|
||
//respond with the user's games | ||
res.json({ games: gamesUser }); | ||
|
||
} catch(error){ | ||
res.status(500).json({ error: 'Internal Server Error inside service' }); | ||
} | ||
}); | ||
|
||
|
||
|
||
const server = app.listen(port, () => { | ||
console.log(`Historial Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
server.on('close', () => { | ||
// Close the Mongoose connection | ||
mongoose.connection.close(); | ||
}); | ||
|
||
|
||
module.exports = server |
Oops, something went wrong.