Skip to content
Open
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
82 changes: 82 additions & 0 deletions api/controllers/FriendController.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
22 changes: 22 additions & 0 deletions api/models/Friend.js
Original file line number Diff line number Diff line change
@@ -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"
},
}
};
7 changes: 7 additions & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ module.exports.policies = {
},
'DashboardController': {
'*': []
},

'FriendController': {
'index': [],
'create': [],
'update': [],
'destroy': []
}
/***************************************************************************
* *
Expand Down
6 changes: 6 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
113 changes: 113 additions & 0 deletions test/unit/controllers/FriendCntroller.spec.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});