Skip to content

Commit

Permalink
add _id property to schemas and create game
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Mar 7, 2024
1 parent 7045dd9 commit 9da71eb
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ services:
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
GQ_SERVICE_URL: http://questiongeneratorservice:8003
QG_SERVICE_URL: http://questiongeneratorservice:8003
GAME_SERVICE_URL: http://gameservice:8004

webapp:
Expand Down
5 changes: 1 addition & 4 deletions game/gameservice/game-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ const mongoose = require('mongoose');


const gameSchema = new mongoose.Schema({
id: {
type: String,
required: true,
},
_id: mongoose.Schema.Types.ObjectId,
players:[{
type: mongoose.Schema.Types.ObjectId,ref:'User',
required: true,
Expand Down
3 changes: 2 additions & 1 deletion game/gameservice/gameLogic.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const Game = require("./game-model")
const mongoose = require('mongoose');

async function createGame(questions, users) {
try {
// Create a new Game instance
const game = new Game({
id: mongoose.Types.ObjectId(),
_id: new mongoose.Types.ObjectId(),
players: users,
questions: questions,
});
Expand Down
6 changes: 4 additions & 2 deletions game/qgservice/MathQuestions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Question4Answers = require("./Question4Answers");
const mongoose = require("mongoose");

function generateRandomMathQuestion() {
const operators = ['+', '-', '*', '/'];
Expand Down Expand Up @@ -26,6 +27,7 @@ function generateRandomMathQuestion() {
}

return {
_id: new mongoose.Types.ObjectId(),
question,
correctAnswer,
incorrectAnswer1: incorrectAnswers[0],
Expand All @@ -34,7 +36,7 @@ function generateRandomMathQuestion() {
};
}

async function saveMathQuestions(numberOfQuestions) {
async function createMathQuestions(numberOfQuestions) {
const questions = [];

for (let i = 0; i < numberOfQuestions; i++) {
Expand All @@ -51,4 +53,4 @@ function generateRandomMathQuestion() {
}
}

module.exports = { saveMathQuestions };
module.exports = { createMathQuestions };
1 change: 1 addition & 0 deletions game/qgservice/Question4Answers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');

const question4AnswersSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
question: {
type: String,
required: true,
Expand Down
5 changes: 3 additions & 2 deletions game/qgservice/qg-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const axios = require('axios');
const mongoose = require('mongoose');
const { worldPopulationQuery, spainPopulationQuery, spainCapitalQuery, worldCapitalQuery } = require('./queries');
const { generateQuestionPopulation, generateQuestionCapital } = require('./questiongenerator');
const { saveMathQuestions } = require('./MathQuestions');
const { createMathQuestions } = require('./MathQuestions');

const app = express();
const port = 8003;
Expand Down Expand Up @@ -101,12 +101,13 @@ app.get('/game', async (req, res) => {
questions.push(question);
}

const mathquestions = await saveMathQuestions(5)
const mathquestions = await createMathQuestions(5)
questions.push(...mathquestions)


res.json(questions);
} catch (error) {
console.log(error)
res.status(500).json({ error: 'Internal Server Error' });
}
})
Expand Down
3 changes: 3 additions & 0 deletions game/qgservice/questiongenerator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Question4Answers = require('./Question4Answers');
const mongoose = require('mongoose');

function generateQuestionPopulation(cityPopulationMap) {
const cityPopulationArray = Array.from(cityPopulationMap);
Expand All @@ -17,6 +18,7 @@ function generateQuestionPopulation(cityPopulationMap) {

// Create the question object
const question = {
_id: new mongoose.Types.ObjectId(),
question: `What is the population of ${city}?`,
correctAnswer: population.toString(),
incorrectAnswer1: incorrectAnswers[0].toString(),
Expand Down Expand Up @@ -54,6 +56,7 @@ function generateQuestionCapital(countryCapitalMap) {

// Create the question object
const question = {
_id: new mongoose.Types.ObjectId(),
question: `What is the capital of ${country}?`,
correctAnswer: capital,
incorrectAnswer1: incorrectAnswers[0],
Expand Down
8 changes: 5 additions & 3 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const port = 8000;

const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002';
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
const qgServiceUrl = process.env.GQ_SERVICE_URL || 'http://localhost:8003';
const gameServiceUrl = process.env.GQ_SERVICE_URL || 'http://localhost:8004';
const qgServiceUrl = process.env.QG_SERVICE_URL || 'http://localhost:8003';
const gameServiceUrl = process.env.GAME_SERVICE_URL || 'http://localhost:8004';

app.use(cors());
app.use(express.json());
Expand Down Expand Up @@ -47,6 +47,8 @@ app.post('/adduser', async (req, res) => {
app.get('/questionsGame', async (req, res) => {
try {
const response = await axios.get( qgServiceUrl+"/game");
// coger los usuarios y crear el game con preguntas y usuarios

res.json(response.data);
} catch (error) {
console.error(error);
Expand All @@ -56,7 +58,7 @@ app.get('/questionsGame', async (req, res) => {

app.post('/addgame', async (req, res) => {
try {
const response = await axios.get(gameServiceUrl+'/addgame', req.body);
const response = await axios.post(gameServiceUrl+'/createGame', req.body);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
Expand Down
1 change: 1 addition & 0 deletions users/authservice/auth-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
username: String,
password: String,
createdAt: Date,
Expand Down
1 change: 1 addition & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ app.post('/adduser', async (req, res) => {
const hashedPassword = await bcrypt.hash(req.body.password, 10);

const newUser = new User({
_id: new mongoose.Types.ObjectId(),
username: req.body.username,
password: hashedPassword,
});
Expand Down

0 comments on commit 9da71eb

Please sign in to comment.