Skip to content

Commit 83eef83

Browse files
committed
Merge branch 'canary' of github.com:DaftMonk/generator-angular-fullstack into pr/543
Conflicts: endpoint/templates/name.controller.js
2 parents 7362080 + 09f92c9 commit 83eef83

16 files changed

+406
-279
lines changed

Diff for: app/templates/_package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"lodash": "~2.4.1",<% if(filters.jade) { %>
1616
"jade": "~1.2.0",<% } %><% if(filters.html) { %>
1717
"ejs": "~0.8.4",<% } %><% if(filters.mongoose) { %>
18-
"mongoose": "~3.8.8",<% } %><% if(filters.auth) { %>
18+
"mongoose": "~3.8.8",
19+
"mongoose-bird": "~0.0.1",
20+
<% } %><% if(filters.auth) { %>
1921
"jsonwebtoken": "^0.3.0",
2022
"express-jwt": "^0.1.3",
2123
"passport": "~0.2.0",
@@ -46,7 +48,7 @@
4648
"grunt-contrib-watch": "~0.6.1",<% if(filters.coffee) { %>
4749
"grunt-contrib-coffee": "^0.10.1",<% } %><% if(filters.jade) { %>
4850
"grunt-contrib-jade": "^0.11.0",<% } %><% if(filters.less) { %>
49-
"grunt-contrib-less": "^0.11.0",<% } %>
51+
"grunt-contrib-less": "^0.11.4",<% } %>
5052
"grunt-google-cdn": "~0.4.0",
5153
"grunt-newer": "~0.7.0",
5254
"grunt-ng-annotate": "^0.2.3",

Diff for: app/templates/server/api/thing/index.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,47 @@ describe('Thing API Router:', function() {
3737
describe('GET /api/things', function() {
3838

3939
it('should route to thing.controller.index', function() {
40-
return router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
40+
router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
4141
});
4242

4343
});<% if(filters.mongoose) { %>
4444

4545
describe('GET /api/things/:id', function() {
4646

4747
it('should route to thing.controller.show', function() {
48-
return router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
48+
router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
4949
});
5050

5151
});
5252

5353
describe('POST /api/things', function() {
5454

5555
it('should route to thing.controller.create', function() {
56-
return router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
56+
router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
5757
});
5858

5959
});
6060

6161
describe('PUT /api/things/:id', function() {
6262

6363
it('should route to thing.controller.update', function() {
64-
return router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
64+
router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
6565
});
6666

6767
});
6868

6969
describe('PATCH /api/things/:id', function() {
7070

7171
it('should route to thing.controller.update', function() {
72-
return router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
72+
router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
7373
});
7474

7575
});
7676

7777
describe('DELETE /api/things/:id', function() {
7878

7979
it('should route to thing.controller.destroy', function() {
80-
return router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
80+
router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
8181
});
8282

8383
});<% } %>

Diff for: app/templates/server/api/thing/thing.controller.js

+74-40
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,57 @@
77
* DELETE /things/:id -> destroy
88
*/
99

10-
'use strict';
10+
'use strict';<% if (filters.mongoose) { %>
1111

12-
var _ = require('lodash');<% if (filters.mongoose) { %>
13-
var Thing = require('./thing.model');<% } %>
12+
var _ = require('lodash');
13+
var Thing = require('./thing.model');
14+
15+
function handleError(res, statusCode){
16+
statusCode = statusCode || 500;
17+
return function(err){
18+
res.send(statusCode, err);
19+
};
20+
}
21+
22+
function responseWithResult(res, statusCode){
23+
statusCode = statusCode || 200;
24+
return function(entity){
25+
if(entity){
26+
return res.json(statusCode, entity);
27+
}
28+
};
29+
}
30+
31+
function handleEntityNotFound(res){
32+
return function(entity){
33+
if(!entity){
34+
res.send(404);
35+
return null;
36+
}
37+
return entity;
38+
};
39+
}
40+
41+
function saveUpdates(updates){
42+
return function(entity){
43+
var updated = _.merge(entity, updates);
44+
return updated.saveAsync()
45+
.then(function () {
46+
return updated;
47+
});
48+
};
49+
}
50+
51+
function removeEntity(res){
52+
return function (entity) {
53+
if(entity){
54+
return entity.removeAsync()
55+
.then(function() {
56+
return res.send(204);
57+
});
58+
}
59+
};
60+
}<% } %>
1461

1562
// Get list of things
1663
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
@@ -34,56 +81,43 @@ exports.index = function(req, res) {<% if (!filters.mongoose) { %>
3481
name : 'Deployment Ready',
3582
info : 'Easily deploy your app to Heroku or Openshift with the heroku and openshift subgenerators'
3683
}
37-
]);<% } %><% if (filters.mongoose) { %>
38-
Thing.find(function (err, things) {
39-
if(err) { return handleError(res, err); }
40-
return res.json(200, things);
41-
});<% } %>
84+
]);<% } if (filters.mongoose) { %>
85+
Thing.findAsync()
86+
.then(responseWithResult(res))
87+
.catch(handleError(res));<% } %>
4288
};<% if (filters.mongoose) { %>
4389

4490
// Get a single thing
4591
exports.show = function(req, res) {
46-
Thing.findById(req.params.id, function (err, thing) {
47-
if(err) { return handleError(res, err); }
48-
if(!thing) { return res.send(404); }
49-
return res.json(thing);
50-
});
92+
Thing.findByIdAsync(req.params.id)
93+
.then(handleEntityNotFound(res))
94+
.then(responseWithResult(res))
95+
.catch(handleError(res));
5196
};
5297

5398
// Creates a new thing in the DB.
5499
exports.create = function(req, res) {
55-
Thing.create(req.body, function(err, thing) {
56-
if(err) { return handleError(res, err); }
57-
return res.json(201, thing);
58-
});
100+
Thing.createAsync(req.body)
101+
.then(responseWithResult(res, 201))
102+
.catch(handleError(res));
59103
};
60104

61105
// Updates an existing thing in the DB.
62106
exports.update = function(req, res) {
63-
if(req.body._id) { delete req.body._id; }
64-
Thing.findById(req.params.id, function (err, thing) {
65-
if (err) { return handleError(res, err); }
66-
if(!thing) { return res.send(404); }
67-
var updated = _.merge(thing, req.body);
68-
updated.save(function (err) {
69-
if (err) { return handleError(res, err); }
70-
return res.json(200, thing);
71-
});
72-
});
107+
if(req.body._id) {
108+
delete req.body._id;
109+
}
110+
Thing.findByIdAsync(req.params.id)
111+
.then(handleEntityNotFound(res))
112+
.then(saveUpdates(req.body))
113+
.then(responseWithResult(res))
114+
.catch(handleError(res));
73115
};
74116

75117
// Deletes a thing from the DB.
76118
exports.destroy = function(req, res) {
77-
Thing.findById(req.params.id, function (err, thing) {
78-
if(err) { return handleError(res, err); }
79-
if(!thing) { return res.send(404); }
80-
thing.remove(function(err) {
81-
if(err) { return handleError(res, err); }
82-
return res.send(204);
83-
});
84-
});
85-
};
86-
87-
function handleError(res, err) {
88-
return res.send(500, err);
89-
}<% } %>
119+
Thing.findByIdAsync(req.params.id)
120+
.then(handleEntityNotFound(res))
121+
.then(removeEntity(res))
122+
.catch(handleError(res));
123+
};<% } %>

Diff for: app/templates/server/api/thing/thing.model(mongoose).js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var mongoose = require('mongoose'),
3+
var mongoose = require('mongoose-bird')(),
44
Schema = mongoose.Schema;
55

66
var ThingSchema = new Schema({

Diff for: app/templates/server/api/user(auth)/index.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,47 @@ describe('User API Router:', function() {
4747
describe('GET /api/users', function() {
4848

4949
it('should verify admin role and route to user.controller.index', function() {
50-
return router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
50+
router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
5151
});
5252

5353
});
5454

5555
describe('DELETE /api/users/:id', function() {
5656

5757
it('should verify admin role and route to user.controller.destroy', function() {
58-
return router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
58+
router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
5959
});
6060

6161
});
6262

6363
describe('GET /api/users/me', function() {
6464

6565
it('should be authenticated and route to user.controller.me', function() {
66-
return router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
66+
router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
6767
});
6868

6969
});
7070

7171
describe('PUT /api/users/:id/password', function() {
7272

7373
it('should be authenticated and route to user.controller.changePassword', function() {
74-
return router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
74+
router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
7575
});
7676

7777
});
7878

7979
describe('GET /api/users/:id', function() {
8080

8181
it('should be authenticated and route to user.controller.show', function() {
82-
return router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
82+
router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
8383
});
8484

8585
});
8686

8787
describe('POST /api/users', function() {
8888

8989
it('should route to user.controller.create', function() {
90-
return router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
90+
router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
9191
});
9292

9393
});

0 commit comments

Comments
 (0)