generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #42 from Arquisoft/gameService1
Merge game service 1 a gameLogic
- Loading branch information
Showing
4 changed files
with
871 additions
and
639 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
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 |
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,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 |
Oops, something went wrong.