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

Group service tests #142

Merged
merged 3 commits into from
May 1, 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
8 changes: 3 additions & 5 deletions game/gameservice/GameController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
let Game = require('./game-model');
const { createGame } = require('./queries/CreateGame');
const mongoose = require('mongoose');

let GameController = {
/* HACER EN USER - GET LAST GAME BY USER
Expand All @@ -11,10 +10,9 @@ let GameController = {
},*/
create: async (req, res) => {
try{
const { questions, players } = req.body;
console.log(questions, players)
const game = await createGame(questions, players);
res.json(game);
const { questions, players } = req.body;
const game = await createGame(questions, players);
res.json(game);
} catch(error){
res.status(500).json({ message: error.message });
}
Expand Down
10 changes: 6 additions & 4 deletions game/gameservice/gameservice.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// gameservice.js
const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');
const { createGame } = require('./queries/CreateGame');
const bodyParser = require('body-parser');
const GameController = require('./GameController');

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

// app.use(bodyParser.json());
app.use(express.json());
app.use(bodyParser.json());

const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);
Expand All @@ -29,4 +27,8 @@ const server = app.listen(port, () => {
console.log(`Question generator Service listening at http://localhost:${port}`);
});

server.on('close', () => {
mongoose.connection.close();
});

module.exports = server;
4 changes: 2 additions & 2 deletions game/gameservice/queries/CreateGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const uuid = require('uuid')
async function createGame(questions, players) {
try {
// Create a new Game instance
if(players.length == 0){
if(players.length === 0){
throw new Error('No players found')
}
if(players[0].uuid == null || players[0].uuid == undefined){
if(players[0].uuid === null || players[0].uuid === undefined){
throw new Error('No players found')
}
const game = new Game({
Expand Down
52 changes: 22 additions & 30 deletions game/groupservice/GroupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ let GroupController = {
res.json(response);

}catch(error){
console.log(error)
res.status(400).json({error: error.message})
res.status(500).json({error: error.message})
}

},
leaveGroup: async (req,res) => {
try{
console.log(req.body)
requiredFields = ['expelledUUID','groupName', 'adminUUID']
validateRequiredFields(req, requiredFields);
const group = await getGroupByName(req.body.groupName);
console.log(req.body.adminUUID +" - "+ req.body.expelledUUID)

if(req.body.adminUUID != group.admin && req.body.adminUUID != req.body.expelledUUID){
console.log("entra en la condicion")
res.json({ message: 'User is unable to perform this operation' });
return;
}
Expand Down Expand Up @@ -76,12 +73,11 @@ let GroupController = {
createGroup: async (req,res) =>{
try{

requiredFields =['groupName','creatorUUID','description','isPublic']
let requiredFields =['groupName','creatorUUID','description','isPublic']
validateRequiredFields(req,requiredFields);

let newGroup;
if(req.body.isPublic){

if(req.body.isPublic){
newGroup = new Group({
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
Expand All @@ -92,25 +88,22 @@ let GroupController = {
groupName: req.body.groupName,
uuid: uuid.v4(),
})
await newGroup.save();

} else {
const joinCode = generateJoinCode();

newGroup = new Group({
groupName: req.body.groupName,
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
maxNumUsers: maxNumUsers,
description: req.body.description,
isPublic: false,
joinCode: joinCode,
creationDate: Date(),
uuid: uuid.v4(),
});
await newGroup.save();
}
res.json(newGroup);
} else {
const joinCode = generateJoinCode();
newGroup = new Group({
groupName: req.body.groupName,
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
maxNumUsers: maxNumUsers,
description: req.body.description,
isPublic: false,
joinCode: joinCode,
creationDate: Date(),
uuid: uuid.v4(),
});
}
const savedGroup = await newGroup.save()
res.json(savedGroup);

} catch(error){
res.status(500).json({error: error.message})
Expand Down Expand Up @@ -144,9 +137,8 @@ let GroupController = {
async function getGroupByName(name) {
try {
const group = await Group.findOne({ groupName: name });

if (!group) {
throw new Error('This group does not exist');
throw new Error('This group does not exist');
}

return group;
Expand All @@ -164,4 +156,4 @@ function validateRequiredFields(req, requiredFields) {
}
}

module.exports = GroupController;
module.exports = {GroupController, getGroupByName};
2 changes: 1 addition & 1 deletion game/groupservice/group-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const mongoose = require('mongoose');
const GroupController = require('./GroupController');
const {GroupController} = require('./GroupController');

const app = express();
const port = 8005;
Expand Down
Loading
Loading