Skip to content

Commit

Permalink
Merge pull request #91 from IGME-Research-Studio/board-service-refactor
Browse files Browse the repository at this point in the history
Board Service Wooh!
  • Loading branch information
dropofwill committed Oct 29, 2015
2 parents 7a898d4 + a23be38 commit 63ec122
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 35 deletions.
77 changes: 49 additions & 28 deletions api/controllers/BoardController.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
/**
* RoomController
*
* @description :: Server-side logic for managing rooms
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
* BoardController
*
* @description :: Server-side logic for managing rooms
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

const boardService = require('../services/BoardService.js');

module.exports = {

create: function(req, res) {

const userSocketId = req.socket;
boardService.create(req.body)

// cannot subscribe if the request is not through socket.io
if (!req.isSocket) {

return res.badRequest('Request Error: Only a client socket can subscribe to a board.');
}

Board.create(req.body).exec(function(err, created) {
.then(function(created) {

const boardId = created.boardId;

res.json({
res.json(200, {

message: 'Server: Board created with board id: ' + boardId,
boardId: created.boardId,
});
})
.catch(function(err) {

sails.sockets.join(userSocketId, boardId);
if (err) {

// subscribe the user to the room (works)
Board.subscribe(req, [boardId]);
Board.publishUpdate(boardId);
res.json(500, {

sails.sockets.broadcast(boardId, 'boarJoined', {message: 'User with socket id: ' + userSocketId.id + ' has joined the room!'});
message: 'Server: An error occurred: ' + err,
});
}
});
},

destroy: function(req, res) {

const boardId = req.param('boardId');

boardService.destroy(boardId)

.then(function(deleted) {

res.json(200, {

message: 'Server: Board with boardId: ' + deleted.boardId + ' was destroyed.',
});
})
.catch(function(err) {

if (err) {

res.json(500, {

message: 'Server: An error occurred: ' + err,
});
}
});
},

join: function(req, res) {

const userSocketId = req.socket;
const boardId = req.body.boardIdentifier;
const boardId = req.param('boardId');

// cannot subscribe if the request is not through socket.io
if (!req.isSocket) {
Expand All @@ -52,9 +75,7 @@ module.exports = {

sails.sockets.broadcast(boardId, 'boardJoined', {message: 'User with socket id: ' + userSocketId.id + ' has joined the board!'});

Board.subscribe(req, [boardId]);

res.json({
res.json(200, {

message: 'User ' + userSocketId.id + ' subscribed to board with board id: ' + boardId,
});
Expand All @@ -63,19 +84,19 @@ module.exports = {
leave: function(req, res) {

const userSocketId = req.socket;
const boardId = req.body.boardIdentifier;
const boardId = req.param('boardId');

// cannot subscribe if the request is not through socket.io
if (!req.isSocket) {

return res.badRequest('Request Error: Only a client socket can subscribe to a board.');
}

sails.sockets.leave(userSocketId, boardId);

res.json({
res.json(200, {

message: 'Server: User with socket id: ' + userSocketId.id + ' left board with board id: ' + boardId,
message: 'Server: User with socket id: ' + req.socket.id + ' left board with board id: ' + req.body.boardIdentifier,
});

sails.sockets.leave(userSocketId, boardId);
},
};
226 changes: 226 additions & 0 deletions api/services/BoardService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Board Service Functionality
const boardService = {};
const Promise = require('bluebird');

// Create a board in the database
boardService.create = function(boardObj) {

return Board.create(boardObj);
};

// Remove a board from the database
boardService.destroy = function(boardId) {

return Board.destroy({boardId: boardId});
};

// Add a user to the board
boardService.addUser = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'users')

.then(function(found) {

found.users.add(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Remove a user from the board
boardService.removeUser = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'users')

.then(function(found) {

found.users.remove(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Add an admin to the board
boardService.addAdmin = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'admins')

.then(function(found) {

found.admins.add(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Remove an admin to the board
boardService.removeAdmin = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'admins')

.then(function(found) {

found.admins.remove(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Add a pending user to the board
boardService.addPendingUser = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'pendingUsers')

.then(function(found) {

found.pendingUsers.add(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Remove a pending user from the board
boardService.removePendingUser = function(boardId, userId) {

return boardService.findBoardAndPopulate(boardId, 'pendingUsers')

.then(function(found) {

found.pendingUsers.remove(userId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Add an idea to the board
boardService.addIdea = function(boardId, ideaId) {

return boardService.findBoardAndPopulate(boardId, 'ideas')

.then(function(found) {

found.ideas.add(ideaId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Remove an idea to the board
boardService.removeIdea = function(boardId, ideaId) {

return boardService.findBoardAndPopulate(boardId, 'ideas')

.then(function(found) {

found.ideas.remove(ideaId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Add an idea collection to the board
boardService.addIdeaCollection = function(boardId, ideaCollectionId) {

return boardService.findBoardAndPopulate(boardId, 'collections')

.then(function(found) {

found.collections.add(ideaCollectionId);

return found.save();
})
.catch((err) => {

throw new Error(err);
});
};

// Remove an idea collection from the board
boardService.removeIdeaCollection = function(boardId, ideaCollectionId) {

return boardService.findBoardAndPopulate(boardId, 'collections')

.then(function(found) {

found.collections.remove(ideaCollectionId);

return found.save();
})
.catch(function(err) {

throw new Error(err);
});
};

// Return all idea collections for a board
// @Note Does not populate User objects on Idea objects in a collection
boardService.getIdeaCollections = function(boardId) {
return Board.findOne({boardId: boardId})
.populate('collections')
.then(function(board) {
return board.collections;
}).then(function(allCollections) {
const collections = [];
const collectionPromises = [];

allCollections.forEach(function(collection) {

collectionPromises.push( IdeaCollection.findOne({id: collection.id}).populate('ideas').then(function(ideaCollection) {
collections.push(ideaCollection);
})
);

});

return Promise.all(collectionPromises).then(function() {
return collections;
});
});
};


// Find a board
boardService.findBoard = function(boardId) {

return Board.findOne({boardId: boardId});
};

// Find and populate a board with collection string
boardService.findBoardAndPopulate = function(boardId, collection) {

return Board.findOne({boardId: boardId}).populate(collection);
};

module.exports = boardService;
8 changes: 2 additions & 6 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ module.exports.routes = {

//'post /createUser': 'UserController.createUser',

'post /board/create': 'BoardController.create',
'post /board/join': 'BoardController.join',
'post /board/leave': 'BoardController.leave',

'post /auth/local': 'AuthController.callback',
'post /auth/local/:action': 'AuthController.callback',
'post /board/:boardId/create': 'BoardController.create',
'post /board/:boardId/destroy': 'BoardController.destroy',

'get /auth/:provider': 'AuthController.provider',
'get /auth/:provider/callback': 'AuthController.callback',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "StormServer",
"version": "0.0.7",
"version": "0.0.8",
"description": "Storm app server",
"scripts": {
"start": "sails lift",
Expand All @@ -21,6 +21,7 @@
"dependencies": {
"babel": "^5.8.29",
"bcryptjs": "^2.2.2",
"bluebird": "^3.0.1",
"ejs": "~0.8.4",
"grunt": "0.4.2",
"grunt-contrib-clean": "~0.5.0",
Expand Down
3 changes: 3 additions & 0 deletions test/client_demo/client.tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ io.sails.url = 'http://localhost:1337';

console.log(data);
});

// add a user to the board
io.socket.post('/board/addUser', {})
});
});

Expand Down
Loading

0 comments on commit 63ec122

Please sign in to comment.