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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ sailsProject

a [Sails](http://sailsjs.org) application

> 須使用 npm@3 安裝相依模組

gem install
-----------

Expand Down
46 changes: 46 additions & 0 deletions api/controllers/FriendController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module.exports = {
create: async (req, res) => {
try {
let friend = await Friend.create(req.body);
res.ok(friend);
} catch (e) {
res.serverError(e);
}
},

findAll: async (req, res) => {
try {
let friends = await Friend.findAll();
res.ok({friends});
} catch (e) {
res.serverError(e);
}
},

update: async (req, res) => {
try {
let result = await Friend.update(req.body,{
where: {
id: req.params.id
}
});
res.ok(result);
} catch (e) {
res.serverError(e);
}
},

destroy: async (req, res) => {
try {
let result = await Friend.destroy({
where: {
id: req.params.id
}
});
res.ok(true);
} catch (e) {
res.serverError(e);
}
},

}
14 changes: 14 additions & 0 deletions api/models/Friend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
attributes: {
name: Sequelize.STRING,
email: Sequelize.STRING,
facebookId: Sequelize.INTEGER
},
associations: function() {
},
options: {
classMethods: {},
instanceMethods: {},
hooks: {}
}
};
6 changes: 6 additions & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports.policies = {
'index': [],
'find': []
},
'FriendController': {
'create': [],
'findAll': [],
'update': [],
'destroy': []
},
'DashboardController': {
'*': []
}
Expand Down
6 changes: 6 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ module.exports.routes = {
'get /api/arrow/authOfFirstPost': 'DashboardController.get_arrow',
'get /api/async/authOfFirstPost': 'DashboardController.get_async',

'post /friend/create': 'FriendController.create',
'get /friend/find': 'FriendController.findAll',
'put /friend/update/:id': 'FriendController.update',
'delete /friend/destroy/:id': 'FriendController.destroy',


/***************************************************************************
* *
* Custom routes here... *
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"author": "spooky",
"license": "",
"devDependencies": {
"babel-cli": "^6.11.4",
"chai": "^3.4.1",
"grunt-bower-task": "^0.4.0",
"grunt-cli": "^0.1.13",
Expand Down
81 changes: 81 additions & 0 deletions test/unit/controllers/FriendController.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe('test Friend CRUD', () => {
let friendId = null;
before(async (done) => {
try {
const newFriend = {
name: 'wahaha',
email: 'wahaha@gm.com',
facebookId: '1234567'
};
const result = await Friend.create(newFriend);
friendId = result.id;
done();
}catch (e){
done();
}
});

it('create a friend', async (done) => {
try{
const newFriend = {
name: 'wahaha',
email: 'wahaha@gm.com',
facebookId: '1234567'
}
let res = await request(sails.hooks.http.app)
.post(`/friend/create`)
.send(newFriend).expect(200);
res.body.should.have.keys('id', 'name', 'email', 'facebookId', 'updatedAt', 'createdAt');
done();
}catch(e){
done();
}
});

it('get friends', async (done) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果這邊 it.only 會錯吧?
要在 before 建立一個 friend 比較保險

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

還有我發現你的 測試裡面都沒

try {

} catch(e) {
  done(e)
}

這樣子如果測試有寫錯會卡住

try{
let res = await request(sails.hooks.http.app).get(`/friend/find`).expect(200);
let {friends} = res.body;
friends.should.be.Array;
friends[0].should.have.keys('id', 'name', 'email', 'facebookId', 'updatedAt', 'createdAt');
done();
}catch(e){
done();
}
});

it('update a friend', async (done) => {
try{
let newInfo = {
name: 'haha'
}
let res = await request(sails.hooks.http.app)
.put(`/friend/update/${friendId}`)
.send(newInfo)
.expect(200);
res.body.should.be.Array;
res.body[0].should.be.eq(1);

let friend = await Friend.findById(friendId);
friend.name.should.be.eq('haha');
done();
}catch(e){
done();
}
});

it('delete a friend', async (done) => {
try{
let res = await request(sails.hooks.http.app)
.delete(`/friend/destroy/${friendId}`)
.expect(200);

let friend = await Friend.findById(friendId);
(friend === null).should.be.true;
done();
}catch(e){
done();
}
});

});