Skip to content

Commit aee5983

Browse files
committed
Merge pull request angular-fullstack#1034 from kingcody/fix/endpoint-controller-model
fix(endpoint): fully support sequelize models
2 parents f4f0b5a + df82d17 commit aee5983

6 files changed

+58
-27
lines changed

endpoint/templates/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var controller = require('./<%= name %>.controller');
55

66
var router = express.Router();
77

8-
router.get('/', controller.index);<% if(filters.mongoose) { %>
8+
router.get('/', controller.index);<% if (filters.models) { %>
99
router.get('/:id', controller.show);
1010
router.post('/', controller.create);
1111
router.put('/:id', controller.update);

endpoint/templates/index.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
var proxyquire = require('proxyquire').noPreserveCache();
44

55
var <%= cameledName %>CtrlStub = {
6-
index: '<%= name %>Ctrl.index'<% if(filters.mongoose) { %>,
6+
index: '<%= name %>Ctrl.index'<% if(filters.models) { %>,
77
show: '<%= name %>Ctrl.show',
88
create: '<%= name %>Ctrl.create',
99
update: '<%= name %>Ctrl.update',
1010
destroy: '<%= name %>Ctrl.destroy'<% } %>
1111
};
1212

1313
var routerStub = {
14-
get: sinon.spy()<% if(filters.mongoose) { %>,
14+
get: sinon.spy()<% if(filters.models) { %>,
1515
put: sinon.spy(),
1616
patch: sinon.spy(),
1717
post: sinon.spy(),
@@ -42,7 +42,7 @@ describe('<%= classedName %> API Router:', function() {
4242
.should.have.been.calledOnce;
4343
});
4444

45-
});<% if(filters.mongoose) { %>
45+
});<% if(filters.models) { %>
4646

4747
describe('GET <%= route %>/:id', function() {
4848

endpoint/templates/name.controller.js

+51-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
'use strict';<% if(filters.mongoose) { %>
1+
/**
2+
* Using Rails-like standard naming convention for endpoints.
3+
* GET <%= route %> -> index<% if (filters.models) { %>
4+
* POST <%= route %> -> create
5+
* GET <%= route %>/:id -> show
6+
* PUT <%= route %>/:id -> update
7+
* DELETE <%= route %>/:id -> destroy<% } %>
8+
*/
29

3-
var _ = require('lodash');
4-
var <%= classedName %> = require('./<%= name %>.model');
10+
'use strict';<% if (filters.models) { %>
11+
12+
var _ = require('lodash');<% if (filters.mongooseModels) { %>
13+
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
14+
var sqldb = require('../../sqldb');
15+
var <%= classedName %> = sqldb.<%= classedName %>;<% } %>
516

617
function handleError(res, statusCode) {
718
statusCode = statusCode || 500;
@@ -14,7 +25,7 @@ function responseWithResult(res, statusCode) {
1425
statusCode = statusCode || 200;
1526
return function(entity) {
1627
if (entity) {
17-
return res.status(statusCode).json(entity);
28+
res.status(statusCode).json(entity);
1829
}
1930
};
2031
}
@@ -31,9 +42,11 @@ function handleEntityNotFound(res) {
3142

3243
function saveUpdates(updates) {
3344
return function(entity) {
34-
var updated = _.merge(entity, updates);
45+
<% if (filters.mongooseModels) { %>var updated = _.merge(entity, updates);
3546
return updated.saveAsync()
36-
.spread(function(updated) {
47+
.spread(function(updated) {<% }
48+
if (filters.sequelizeModels) { %>return entity.updateAttributes(updates)
49+
.then(function(updated) {<% } %>
3750
return updated;
3851
});
3952
};
@@ -42,52 +55,70 @@ function saveUpdates(updates) {
4255
function removeEntity(res) {
4356
return function(entity) {
4457
if (entity) {
45-
return entity.removeAsync()
58+
<% if (filters.mongooseModels) { %>return entity.removeAsync()<% }
59+
if (filters.sequelizeModels) { %>return entity.destroy()<% } %>
4660
.then(function() {
4761
res.status(204).end();
4862
});
4963
}
5064
};
5165
}<% } %>
5266

53-
// Get list of <%= name %>s
54-
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
55-
res.json([]);<% } %><% if (filters.mongoose) { %>
56-
<%= classedName %>.findAsync()
67+
// Gets a list of <%= name %>s
68+
exports.index = function(req, res) {<% if (!filters.models) { %>
69+
res.json([]);<% } else { %>
70+
<% if (filters.mongooseModels) { %><%= classedName %>.findAsync()<% }
71+
if (filters.sequelizeModels) { %><%= classedName %>.findAll()<% } %>
5772
.then(responseWithResult(res))
5873
.catch(handleError(res));<% } %>
59-
};<% if (filters.mongoose) { %>
74+
};<% if (filters.models) { %>
6075

61-
// Gets a single <%= name %> from the DB.
76+
// Gets a single <%= name %> from the DB
6277
exports.show = function(req, res) {
63-
<%= classedName %>.findByIdAsync(req.params.id)
78+
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
79+
if (filters.sequelizeModels) { %><%= classedName %>.find({
80+
where: {
81+
_id: req.params.id
82+
}
83+
})<% } %>
6484
.then(handleEntityNotFound(res))
6585
.then(responseWithResult(res))
6686
.catch(handleError(res));
6787
};
6888

69-
// Creates a new <%= name %> in the DB.
89+
// Creates a new <%= name %> in the DB
7090
exports.create = function(req, res) {
71-
<%= classedName %>.createAsync(req.body)
91+
<% if (filters.mongooseModels) { %><%= classedName %>.createAsync(req.body)<% }
92+
if (filters.sequelizeModels) { %><%= classedName %>.create(req.body)<% } %>
7293
.then(responseWithResult(res, 201))
7394
.catch(handleError(res));
7495
};
7596

76-
// Updates an existing <%= name %> in the DB.
97+
// Updates an existing <%= name %> in the DB
7798
exports.update = function(req, res) {
7899
if (req.body._id) {
79100
delete req.body._id;
80101
}
81-
<%= classedName %>.findByIdAsync(req.params.id)
102+
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
103+
if (filters.sequelizeModels) { %><%= classedName %>.find({
104+
where: {
105+
_id: req.params.id
106+
}
107+
})<% } %>
82108
.then(handleEntityNotFound(res))
83109
.then(saveUpdates(req.body))
84110
.then(responseWithResult(res))
85111
.catch(handleError(res));
86112
};
87113

88-
// Deletes a <%= name %> from the DB.
114+
// Deletes a <%= name %> from the DB
89115
exports.destroy = function(req, res) {
90-
<%= classedName %>.findByIdAsync(req.params.id)
116+
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
117+
if (filters.sequelizeModels) { %><%= classedName %>.find({
118+
where: {
119+
_id: req.params.id
120+
}
121+
})<% } %>
91122
.then(handleEntityNotFound(res))
92123
.then(removeEntity(res))
93124
.catch(handleError(res));

endpoint/templates/name.integration.js

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

33
var app = require('../../app');
4-
var request = require('supertest');<% if(filters.mongoose) { %>
4+
var request = require('supertest');<% if(filters.models) { %>
55

66
var new<%= classedName %>;<% } %>
77

@@ -28,7 +28,7 @@ describe('<%= classedName %> API:', function() {
2828
<%= cameledName %>s.should.be.instanceOf(Array);
2929
});
3030

31-
});<% if(filters.mongoose) { %>
31+
});<% if(filters.models) { %>
3232

3333
describe('POST <%= route %>', function() {
3434
beforeEach(function(done) {
@@ -130,7 +130,7 @@ describe('<%= classedName %> API:', function() {
130130
});
131131
});
132132

133-
it('should respond with 404 when <%= name %> does not exsist', function(done) {
133+
it('should respond with 404 when <%= name %> does not exist', function(done) {
134134
request(app)
135135
.delete('<%= route %>/' + new<%= classedName %>._id)
136136
.expect(404)

0 commit comments

Comments
 (0)