Skip to content

Commit

Permalink
Merge pull request #32 from Arquisoft/groupsService1
Browse files Browse the repository at this point in the history
Group Service
  • Loading branch information
pelazas authored Mar 13, 2024
2 parents d524cc6 + b762889 commit 2d65837
Show file tree
Hide file tree
Showing 19 changed files with 6,423 additions and 58 deletions.
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ services:
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

groupservice:
container_name: groupservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en2a/groupservice:latest
profiles: ["dev", "prod"]
build: ./game/groupservice
depends_on:
- mongodb
ports:
- "8005:8005"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en2a/gatewayservice:latest
Expand All @@ -78,6 +92,7 @@ services:
- authservice
- questiongeneratorservice
- gameservice
- groupservice
ports:
- "8000:8000"
networks:
Expand All @@ -87,6 +102,7 @@ services:
USER_SERVICE_URL: http://userservice:8001
QG_SERVICE_URL: http://questiongeneratorservice:8003
GAME_SERVICE_URL: http://gameservice:8004
GROUP_SERVICE_URL: http://groupservice:8005

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
2 changes: 2 additions & 0 deletions game/groupservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
20 changes: 20 additions & 0 deletions game/groupservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/groupservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the app source code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 8005

# Define the command to run your app
CMD ["node", "group-service.js"]
158 changes: 158 additions & 0 deletions game/groupservice/GroupController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const uuid = require('uuid');
const generateJoinCode = require('./util/GenerateJoinCode');
const Group = require('./group-model');

const maxNumUsers = 30;

let GroupController = {
joinGroup: async (req,res) => {
try{
requiredFields = ['uuid','groupName']
validateRequiredFields(req, requiredFields);
const group = await getGroupByName(req.body.groupName);

if(group.members.includes(req.body.uuid)){
res.json({ message: 'User is already in this group' });
return;
}

if(group.members.length == maxNumUsers){
res.json({ message: 'This group is full' });
return;
}

if(!group.isPublic){
if(group.joinCode != req.body.joinCode){
res.json({ message: 'Invalid join code' });
return;
}
}

group.members.push(req.body.uuid);

const response = await group.save()

res.json(response);

}catch(error){
console.log(error)
res.status(400).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;
}

group.members = group.members.filter(member => member != req.body.expelledUUID)

if(group.members.length == 0){
await group.deleteOne()
res.json({ message: 'Group deleted' });
return;
}

if(group.admin == req.body.expelledUUID){
group.admin = group.members.at(0);
}

const response = await group.save()
res.json(response);

}catch(error){
console.log(error)
res.status(500).json({error: error.message})
}
},
createGroup: async (req,res) =>{
try{

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

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

newGroup = new Group({
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
maxNumUsers: maxNumUsers,
description: req.body.description,
isPublic: true,
creationDate: Date(),
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);

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

},
getGroup: async (req, res) => {
try{
const uuid = req.params.uuid
const group = await Group.findOne({uuid: uuid})
if(!group){
res.status(404).json({error: 'Group not found'})
return;
}
res.json(group)
} catch(error){
res.status(500).json({error: error.message})
}
}
}

async function getGroupByName(name) {
try {
const group = await Group.findOne({ groupName: name });

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

return group;
} catch (error) {
// Handle the error here
throw new Error(error.message);
}
}

function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
}
}
}

module.exports = GroupController;
45 changes: 45 additions & 0 deletions game/groupservice/group-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const mongoose = require('mongoose');

const groupSchema = new mongoose.Schema({
uuid: {
type: String,
required: true,
},
groupName: {
type: String,
required: true,
unique: true,
},
admin: {
type: String,
required: true,
},
members:[{
type: String,
required: true,
}],
maxNumUsers: {
type: Number,
required: true,
},
description: {
type: String,
required: false,
},
isPublic: {
type: Boolean,
required: true,
},
joinCode: {
type: String,
required: false,
},
creationDate: {
type: Date,
default: Date.now,
}
});

const Group = mongoose.model('Group', groupSchema);

module.exports = Group;
32 changes: 32 additions & 0 deletions game/groupservice/group-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express');
const mongoose = require('mongoose');
const GroupController = require('./GroupController');

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

app.use(express.json());

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

app.get('/', (req, res) => {
res.json({ message: 'Welcome to group service module' });
});

app.post('/joinGroup', GroupController.joinGroup);
app.post('/leaveGroup', GroupController.leaveGroup);
app.post('/createGroup', GroupController.createGroup);
app.get('/getGroup/:uuid', GroupController.getGroup);

const server = app.listen(port, () => {
console.log(`Group Service listening at http://localhost:${port}`);
});

server.on('close', () => {
// Close the Mongoose connection
mongoose.connection.close();
});

module.exports = server
Loading

0 comments on commit 2d65837

Please sign in to comment.