Skip to content

Commit

Permalink
Merge pull request #42 from Arquisoft/gameService1
Browse files Browse the repository at this point in the history
Merge game service 1 a gameLogic
  • Loading branch information
UO277274 authored Mar 5, 2024
2 parents 43f96fe + 536a32a commit a198682
Show file tree
Hide file tree
Showing 4 changed files with 871 additions and 639 deletions.
26 changes: 23 additions & 3 deletions game/gameservice/game-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
const mongoose = require('mongoose');



const gameSchema = new mongoose.Schema({
// ONE TO MANY CON QUESTIONS
// ONE TO MANY CON USERS
id: {
type: String,
required: true,
},
players:[{
type: mongoose.Schema.Types.ObjectId,ref:'User',
required: true,
}],
/*
player2:{
type: mongoose.Schema.Types.ObjectId,ref:'User'
},
player3:{
type: mongoose.Schema.Types.ObjectId,ref:'User'
},
*/
questions:[
{
type: mongoose.Schema.Types.ObjectId,ref:'Question4Answers'
}
],
});


const Game = mongoose.model('Game', gameSchema);

module.exports = Game
55 changes: 55 additions & 0 deletions game/gameservice/game-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// user-service.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Game = require('./game-model')

const app = express();
const port = 8004;

// Middleware to parse JSON in request body
app.use(bodyParser.json());

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



// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
}
}
}

app.post('/creategame', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['player']);

const newGame = new Game({
//id: TODO: generate unique id
player1: req.body.player,
questions: req.body.questions,
});

await newGame.save();
res.json(newGame);
} catch (error) {
res.status(400).json({ error: error.message });
}});

const server = app.listen(port, () => {
console.log(`User 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
Loading

0 comments on commit a198682

Please sign in to comment.