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

Merge game service 1 a gameLogic #42

Merged
merged 5 commits into from
Mar 5, 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
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