Skip to content

Commit

Permalink
Merge pull request #86 from Arquisoft/Develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
UO287687 authored Mar 5, 2024
2 parents 62488b1 + 3a09314 commit 3c7e8bf
Show file tree
Hide file tree
Showing 27 changed files with 18,322 additions and 1,738 deletions.
7 changes: 4 additions & 3 deletions gameservice/game-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ const mongoose = require('mongoose');
const gameSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
questions: {
type: mongoose.Schema.Types.ObjectId,
questions: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'Question',
required: true,
},
answers: [
{
response: {
type: String,
required: true,
required: true,
},
isCorrect: {
type: Boolean,
Expand Down
17 changes: 11 additions & 6 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const port = 8005; // Puerto para el servicio de juegos

app.use(express.json());

// Connect to MongoDB
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/gamesdb';
mongoose.connect(mongoUri);

// Función para validar campos requeridos
const validateRequiredFields = (req, fields) => {
for (const field of fields) {
Expand Down Expand Up @@ -34,9 +38,9 @@ app.post('/addgame', async (req, res) => {
// Guarda el nuevo juego en la base de datos
const savedGame = await newGame.save();

res.status(201).json(savedGame);
res.status(200).json(savedGame);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
res.status(500).json({ error: error.message });
}
});

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

// Conecta a la base de datos de juegos
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/gamesdb';
mongoose.connect(mongoUri);

const server = app.listen(port, () => {
console.log(`Games Service listening at http://localhost:${port}`);
});

server.on('close', () => {
// Close the Mongoose connection
mongoose.connection.close();
});

module.exports = server;
15 changes: 8 additions & 7 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');
const app = require('./game-service');

let mongoServer;
let app;

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
mongoose.connect(mongoUri);
app = require('./game-service');
});

afterAll(async () => {
await mongoose.disconnect();
app.close();
await mongoServer.stop();
});

describe('Game Service', () => {
it('should add a new game on POST /addgame', async () => {
const newGame = {
user: mongoose.Types.ObjectId(), // ID de usuario simulado
questions: mongoose.Types.ObjectId(), // ID de pregunta simulado
user: '609c6e365308ce1a1c2658d1',
questions: [
"609c6e365308ce1a1c2658d2", "609c6e365308ce1a1c2658d3"
],
answers: [
{
response: 'User response',
Expand All @@ -32,7 +33,7 @@ describe('Game Service', () => {
};

const response = await request(app).post('/addgame').send(newGame);
expect(response.status).toBe(201);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('user', newGame.user.toString());
});
});
Loading

0 comments on commit 3c7e8bf

Please sign in to comment.