diff --git a/api/controllers/FriendController.js b/api/controllers/FriendController.js new file mode 100644 index 0000000..d5599ed --- /dev/null +++ b/api/controllers/FriendController.js @@ -0,0 +1,82 @@ +/** + * FriendController + * + * @description :: Server-side logic for managing friends + * @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers + */ + +module.exports = { + index: async(req , res) => { + try{ + let friends = await Friend.findAll(); + res.ok(friends); + res.end(); + } + catch(e){ + throw e; + } + + }, + + create: async(req , res) => { + try{ + let name = req.body.name, + email = req.body.email, + facebookId = req.body.facebookId; + + let newFriend = await Friend.create({ + name: name, + email: email, + facebookId: facebookId + }); + + res.ok(newFriend); + res.end(); + } + catch(e){ + throw e; + } + }, + + update: async(req , res) => { + try{ + let id = req.body.id, + email = req.body.email, + name = req.body.name; + + await Friend.update({ + name: name, + email: email + },{ + where:{id: id} + }); + + let updateInfo = await Friend.findById(id); + + res.ok(updateInfo); + res.end(); + } + catch(e){ + throw e; + } + }, + + destroy: async(req , res) => { + try{ + console.log('friend delete...'); + let id = req.params['id']; + + let delFriend = await Friend.destroy({ + where:{ + id: id + } + }); + + res.ok(); + res.end(); + } + catch(e){ + throw e; + } + } +}; diff --git a/api/models/Friend.js b/api/models/Friend.js new file mode 100644 index 0000000..e3aed92 --- /dev/null +++ b/api/models/Friend.js @@ -0,0 +1,22 @@ +/** + * Friend.js + * + * @description :: TODO: You might write a short summary of how this model works and what it represents here. + * @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models + */ + +module.exports = { + + attributes: { + name: { + type: Sequelize.STRING + }, + facebookId: { + type: Sequelize.STRING + }, + email: { + type: Sequelize.STRING, + defaultValue: "nuknown@email.com" + }, + } +}; diff --git a/config/policies.js b/config/policies.js index fd8e96e..e21ece0 100644 --- a/config/policies.js +++ b/config/policies.js @@ -28,6 +28,13 @@ module.exports.policies = { }, 'DashboardController': { '*': [] + }, + + 'FriendController': { + 'index': [], + 'create': [], + 'update': [], + 'destroy': [] } /*************************************************************************** * * diff --git a/config/routes.js b/config/routes.js index 9a6885d..b21d7f4 100644 --- a/config/routes.js +++ b/config/routes.js @@ -35,6 +35,12 @@ module.exports.routes = { '/': { view: 'index' }, + // Friend routes + 'get /friend': 'FriendController.index', + + 'post /friend/create': 'FriendController.create', + 'put /friend/update': 'FriendController.update', + 'delete /friend/delete/:id': 'FriendController.destroy', 'get /login': 'AuthController.login', 'get /logout': 'AuthController.logout', diff --git a/package.json b/package.json index 4e4c810..31bed4f 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "description": "a Sails application", "keywords": [], "dependencies": { + "babel-cli": "^6.11.4", "babel-plugin-syntax-async-functions": "^6.1.4", "babel-plugin-transform-regenerator": "^6.1.4", "babel-plugin-transform-runtime": "^6.9.0", diff --git a/test/unit/controllers/FriendCntroller.spec.js b/test/unit/controllers/FriendCntroller.spec.js new file mode 100644 index 0000000..56dbca4 --- /dev/null +++ b/test/unit/controllers/FriendCntroller.spec.js @@ -0,0 +1,113 @@ +describe.only('Test Restful api CRUD by friend model', function(){ + let friend; + before(async (done) => { + try{ + friend = await Friend.create({ + name:'MADAO', + email: 'madao@gintama.com', + facebookId:'13149487' + }); + done(); + } + catch(e){ + done(e); + } + }); + + it('List Friends', async function(done){ + try{ + // http get friend index page + let result = await request(sails.hooks.http.app) + .get(`/friend`); + + console.log(result); + result.status.should.be.eq(200); + result.body[0]['name'].should.be.eq('MADAO'); + done(); + } + catch(e){ + done(e); + } + }); + + it('Create a new Friend', async function(done){ + try{ + let newFriend_info = { + name:'Iron Man', + email: 'tony.stark@stack.com', + facebookId:'20151425' + }; + // http create + let result = await request(sails.hooks.http.app) + .post(`/friend/create`) + .send(newFriend_info); + + //database select + let f = await Friend.findOne({where: { facebookId: newFriend_info.facebookId } }); + + //http check + result.status.should.be.eq(200); + result.body['name'].should.be.eq('Iron Man'); + + //db check + f.name.should.be.eq('Iron Man'); + + done(); + } + catch(e){ + done(e); + } + }); + + it('Update an Friend information', async function(done){ + try{ + let friend_info_update = { + id: 1, + name: "Winner", + email: "yeah@gintama.com" + } + // http update + let result = await request(sails.hooks.http.app) + .put(`/friend/update`) + .send(friend_info_update); + + //database select + let f = await Friend.findById(friend_info_update.id); + + //http check + result.status.should.be.eq(200); + //result.body['name'].should.be.eq('Winner'); + + //database check + f.name.should.be.equal('Winner'); + + done(); + } + catch(e){ + done(e); + } + }); + + it('Destroyyyyyyyyyyyy an Friend', async function(done){ + try{ + let del_friend_id = 1; + //http delete + let result = await request(sails.hooks.http.app) + .delete(`/friend/delete/${del_friend_id}`); + + //database select + let f = await Friend.findById(del_friend_id); + + //http check + result.status.should.be.eq(200); + //result.body['name'].should.be.eq('Winner'); + + ( f === null ).should.be.true; + + done(); + } + catch(e){ + done(e); + } + }); +});