Skip to content

Commit 3c7e8bf

Browse files
authored
Merge pull request #86 from Arquisoft/Develop
Develop
2 parents 62488b1 + 3a09314 commit 3c7e8bf

27 files changed

+18322
-1738
lines changed

gameservice/game-model.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ const mongoose = require('mongoose');
55
const gameSchema = new mongoose.Schema({
66
user: {
77
type: mongoose.Schema.Types.ObjectId,
8+
ref: 'User',
89
required: true,
910
},
10-
questions: {
11-
type: mongoose.Schema.Types.ObjectId,
11+
questions: {
12+
type: [mongoose.Schema.Types.ObjectId],
1213
ref: 'Question',
1314
required: true,
1415
},
1516
answers: [
1617
{
1718
response: {
1819
type: String,
19-
required: true,
20+
required: true,
2021
},
2122
isCorrect: {
2223
type: Boolean,

gameservice/game-service.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const port = 8005; // Puerto para el servicio de juegos
77

88
app.use(express.json());
99

10+
// Connect to MongoDB
11+
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/gamesdb';
12+
mongoose.connect(mongoUri);
13+
1014
// Función para validar campos requeridos
1115
const validateRequiredFields = (req, fields) => {
1216
for (const field of fields) {
@@ -34,9 +38,9 @@ app.post('/addgame', async (req, res) => {
3438
// Guarda el nuevo juego en la base de datos
3539
const savedGame = await newGame.save();
3640

37-
res.status(201).json(savedGame);
41+
res.status(200).json(savedGame);
3842
} catch (error) {
39-
res.status(500).json({ error: 'Internal Server Error' });
43+
res.status(500).json({ error: error.message });
4044
}
4145
});
4246

@@ -77,12 +81,13 @@ app.get('/getParticipation/:userId', async (req, res) => {
7781
}
7882
});
7983

80-
// Conecta a la base de datos de juegos
81-
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/gamesdb';
82-
mongoose.connect(mongoUri);
83-
8484
const server = app.listen(port, () => {
8585
console.log(`Games Service listening at http://localhost:${port}`);
8686
});
8787

88+
server.on('close', () => {
89+
// Close the Mongoose connection
90+
mongoose.connection.close();
91+
});
92+
8893
module.exports = server;

gameservice/game-service.test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
const request = require('supertest');
22
const { MongoMemoryServer } = require('mongodb-memory-server');
3-
const mongoose = require('mongoose');
4-
const app = require('./game-service');
53

64
let mongoServer;
5+
let app;
76

87
beforeAll(async () => {
98
mongoServer = await MongoMemoryServer.create();
109
const mongoUri = mongoServer.getUri();
1110
process.env.MONGODB_URI = mongoUri;
12-
mongoose.connect(mongoUri);
11+
app = require('./game-service');
1312
});
1413

1514
afterAll(async () => {
16-
await mongoose.disconnect();
15+
app.close();
1716
await mongoServer.stop();
1817
});
1918

2019
describe('Game Service', () => {
2120
it('should add a new game on POST /addgame', async () => {
2221
const newGame = {
23-
user: mongoose.Types.ObjectId(), // ID de usuario simulado
24-
questions: mongoose.Types.ObjectId(), // ID de pregunta simulado
22+
user: '609c6e365308ce1a1c2658d1',
23+
questions: [
24+
"609c6e365308ce1a1c2658d2", "609c6e365308ce1a1c2658d3"
25+
],
2526
answers: [
2627
{
2728
response: 'User response',
@@ -32,7 +33,7 @@ describe('Game Service', () => {
3233
};
3334

3435
const response = await request(app).post('/addgame').send(newGame);
35-
expect(response.status).toBe(201);
36+
expect(response.status).toBe(200);
3637
expect(response.body).toHaveProperty('user', newGame.user.toString());
3738
});
3839
});

0 commit comments

Comments
 (0)