From 19d5987c6319df2d9dae102613007de4b46636ae Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 15:42:18 +0200 Subject: [PATCH 01/22] Endpoint generator updated --- .../client/assets/images/!yeoman.png | Bin 12331 -> 12330 bytes endpoint/index.js | 32 +++--- endpoint/templates/index.js | 16 +-- endpoint/templates/name.controller.js | 103 +++++++++++------- endpoint/templates/name.model(mongoose).js | 12 -- endpoint/templates/name.model.js | 20 ++++ endpoint/templates/name.socket(socketio).js | 24 ---- endpoint/templates/name.spec.js | 77 ++++++++++--- endpoint/templatesJson/names.json | 1 + 9 files changed, 173 insertions(+), 112 deletions(-) delete mode 100644 endpoint/templates/name.model(mongoose).js create mode 100644 endpoint/templates/name.model.js delete mode 100644 endpoint/templates/name.socket(socketio).js create mode 100644 endpoint/templatesJson/names.json diff --git a/app/templates/client/assets/images/!yeoman.png b/app/templates/client/assets/images/!yeoman.png index 7d0a1ac7120ab1e1b06598a02f2670f52aa00829..fe2b154b05d602f4fa0ea5400e550093d680c221 100644 GIT binary patch delta 13 UcmZ3TuquJ2Gr-S%BdeML03~b$lmGw# delta 14 VcmZ3LusVUYGr-TCcO#3M0RSmF1fc)` diff --git a/endpoint/index.js b/endpoint/index.js index 2b3d7eb22..7d54fe439 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -16,7 +16,7 @@ Generator.prototype.askFor = function askFor() { var name = this.name; var base = this.config.get('routesBase') || '/api/'; - if(base.charAt(base.length-1) !== '/') { + if (base.charAt(base.length - 1) !== '/') { base = base + '/'; } @@ -34,7 +34,7 @@ Generator.prototype.askFor = function askFor() { ]; this.prompt(prompts, function (props) { - if(props.route.charAt(0) !== '/') { + if (props.route.charAt(0) !== '/') { props.route = '/' + props.route; } @@ -44,28 +44,24 @@ Generator.prototype.askFor = function askFor() { }; Generator.prototype.registerEndpoint = function registerEndpoint() { - if(this.config.get('insertRoutes')) { + if (this.config.get('insertRoutes')) { var routeConfig = { file: this.config.get('registerRoutesFile'), needle: this.config.get('routesNeedle'), splicable: [ - "app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));" + "app.use(\'" + this.route + "\', require(\'./api/" + this.name + "\'));" ] }; ngUtil.rewriteFile(routeConfig); - } - if (this.filters.socketio) { - if(this.config.get('insertSockets')) { - var socketConfig = { - file: this.config.get('registerSocketsFile'), - needle: this.config.get('socketsNeedle'), - splicable: [ - "require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);" - ] - }; - ngUtil.rewriteFile(socketConfig); - } + var rolesConfig = { + file: this.config.get('configRolesFile'), + needle: this.config.get('configRolesNeedle'), + splicable: [ + ", " + this.name + "s-index, " + this.name + "s-show, " + this.name + "s-update, " + this.name + "s-create, " + this.name + "s-destroy" + ] + }; + ngUtil.rewriteFile(rolesConfig); } }; @@ -73,4 +69,8 @@ Generator.prototype.createFiles = function createFiles() { var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name; this.sourceRoot(path.join(__dirname, './templates')); ngUtil.processDirectory(this, '.', dest); + + var destSharedModels = 'shared/models'; + this.sourceRoot(path.join(__dirname, './templatesJson/')); + ngUtil.processDirectory(this, '.', destSharedModels); }; diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 03fdc9779..1ad549633 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -2,14 +2,16 @@ var express = require('express'); var controller = require('./<%= name %>.controller'); +var config = require('../../config/environment'); +var auth = require('../../auth/auth.service'); var router = express.Router(); -router.get('/', controller.index);<% if(filters.mongoose) { %> -router.get('/:id', controller.show); -router.post('/', controller.create); -router.put('/:id', controller.update); -router.patch('/:id', controller.update); -router.delete('/:id', controller.destroy);<% } %> +router.get('/', auth.hasRole('<%= name %>s-index'), controller.index); +router.get('/:id', auth.hasRole('<%= name %>s-show'), controller.show); +router.post('/', auth.hasRole('<%= name %>s-create'), controller.create); +router.put('/:id', auth.hasRole('<%= name %>s-update'), controller.update); +router.patch('/:id', auth.hasRole('<%= name %>s-update'), controller.update); +router.delete('/:id', auth.hasRole('<%= name %>s-destroy'), controller.destroy); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 3d46b2ad4..0269ba80d 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -1,60 +1,83 @@ 'use strict'; -var _ = require('lodash');<% if (filters.mongoose) { %> -var <%= classedName %> = require('./<%= name %>.model');<% } %> +var _ = require('lodash'); +var <%= name %> = require('./<%= name %>.model'); // Get list of <%= name %>s -exports.index = function(req, res) {<% if (!filters.mongoose) { %> - res.json([]);<% } %><% if (filters.mongoose) { %> - <%= classedName %>.find(function (err, <%= name %>s) { - if(err) { return handleError(res, err); } - return res.status(200).json(<%= name %>s); - });<% } %> -};<% if (filters.mongoose) { %> +exports.index = function (req, res) { + <%= name %>.find(function (err, <%= name %> + s) { + if (err) { + return handleError(res, err); + } + return res.status(200).json(<%= name %> + s); + }); +}; // Get a single <%= name %> -exports.show = function(req, res) { - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if(err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - return res.json(<%= name %>); - }); +exports.show = function (req, res) { + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + return res.json(<%= name %>); + }); }; // Creates a new <%= name %> in the DB. -exports.create = function(req, res) { - <%= classedName %>.create(req.body, function(err, <%= name %>) { - if(err) { return handleError(res, err); } - return res.status(201).json(<%= name %>); - }); +exports.create = function (req, res) { + <%= name %>.create(req.body, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + return res.status(201).json(<%= name %>); + }); }; // Updates an existing <%= name %> in the DB. -exports.update = function(req, res) { - if(req.body._id) { delete req.body._id; } - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if (err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - var updated = _.merge(<%= name %>, req.body); - updated.save(function (err) { - if (err) { return handleError(res, err); } - return res.status(200).json(<%= name %>); +exports.update = function (req, res) { + if (req.body._id) { + delete req.body._id; + } + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + var updated = _.merge(<%= name %>, req.body); + updated.save(function (err) { + if (err) { + return handleError(res, err); + } + return res.status(200).json(<%= name %>); + }); }); - }); }; // Deletes a <%= name %> from the DB. -exports.destroy = function(req, res) { - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if(err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - <%= name %>.remove(function(err) { - if(err) { return handleError(res, err); } - return res.status(204).send('No Content'); +exports.destroy = function (req, res) { + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + <%= name %>.remove(function (err) { + if (err) { + return handleError(res, err); + } + return res.status(204).send('No Content'); + }); }); - }); }; function handleError(res, err) { - return res.status(500).send(err); -}<% } %> \ No newline at end of file + return res.status(500).send(err); +} diff --git a/endpoint/templates/name.model(mongoose).js b/endpoint/templates/name.model(mongoose).js deleted file mode 100644 index 89e0dfaa7..000000000 --- a/endpoint/templates/name.model(mongoose).js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var mongoose = require('mongoose'), - Schema = mongoose.Schema; - -var <%= classedName %>Schema = new Schema({ - name: String, - info: String, - active: Boolean -}); - -module.exports = mongoose.model('<%= classedName %>', <%= classedName %>Schema); \ No newline at end of file diff --git a/endpoint/templates/name.model.js b/endpoint/templates/name.model.js new file mode 100644 index 000000000..635460512 --- /dev/null +++ b/endpoint/templates/name.model.js @@ -0,0 +1,20 @@ +'use strict'; + +// Dependencies +var mongoose = require('mongoose'); +var mongoose_delete = require('mongoose-delete'); +var mongoose_timestamp = require('mongoose-timestamp'); + +// Model dependencies + +// Schema +var schema = new mongoose.Schema(require('./../../../shared/models/<%= name%>s.json')); + +// Schema plugins +schema.plugin(mongoose_delete, { + deletedAt: true +}); +schema.plugin(mongoose_timestamp); + +// Return model +module.exports = mongoose.model('<%= name %>', schema); diff --git a/endpoint/templates/name.socket(socketio).js b/endpoint/templates/name.socket(socketio).js deleted file mode 100644 index 886f585ee..000000000 --- a/endpoint/templates/name.socket(socketio).js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Broadcast updates to client when the model changes - */ - -'use strict'; - -var <%= classedName %> = require('./<%= name %>.model'); - -exports.register = function(socket) { - <%= classedName %>.schema.post('save', function (doc) { - onSave(socket, doc); - }); - <%= classedName %>.schema.post('remove', function (doc) { - onRemove(socket, doc); - }); -} - -function onSave(socket, doc, cb) { - socket.emit('<%= name %>:save', doc); -} - -function onRemove(socket, doc, cb) { - socket.emit('<%= name %>:remove', doc); -} \ No newline at end of file diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index fcad73ebd..dbe32bf45 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -4,17 +4,68 @@ var should = require('should'); var app = require('../../app'); var request = require('supertest'); -describe('GET <%= route %>', function() { +describe('GET /api/<%= name %>s', function () { + var id = 0; - it('should respond with JSON array', function(done) { - request(app) - .get('<%= route %>') - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) return done(err); - res.body.should.be.instanceof(Array); - done(); - }); - }); -}); \ No newline at end of file + it('should respond with JSON array', function (done) { + request(app) + .get('/api/<%= name%>s') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.be.instanceof(Array); + res.body.should.have.length(0); + done(); + }); + }); + + it('should create a new <%= name %>', function (done) { + request(app) + .post('/api/<%= name%>s') + .send() + .expect(201) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_id'); + id = res.body._id; + done(); + }); + }); + + it('should update a <%= name %>', function (done) { + request(app) + .put('/api/<%= name%>s/' + id) + .send({}) + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_2id'); + done(); + }); + }); + + it('should get a <%= name %>', function (done) { + request(app) + .get('/api/<%= name%>s/' + id) + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_id'); + done(); + }); + }); + + it('should delete a <%= name %>', function (done) { + request(app) + .delete('/api/<%= name%>s/' + id) + .expect(204) + .end(function (err, res) { + if (err) return done(err); + done(); + }); + }); +}); diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/endpoint/templatesJson/names.json @@ -0,0 +1 @@ +{} \ No newline at end of file From 7eb7c6b3764dd9bd0d6b7a525ae4c93a5c90ba4f Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 16:19:44 +0200 Subject: [PATCH 02/22] Some correction --- endpoint/index.js | 2 +- endpoint/templates/name.controller.js | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 7d54fe439..477e3c40e 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -58,7 +58,7 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { file: this.config.get('configRolesFile'), needle: this.config.get('configRolesNeedle'), splicable: [ - ", " + this.name + "s-index, " + this.name + "s-show, " + this.name + "s-update, " + this.name + "s-create, " + this.name + "s-destroy" + "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," ] }; ngUtil.rewriteFile(rolesConfig); diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 0269ba80d..d0bff2ab5 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -5,13 +5,11 @@ var <%= name %> = require('./<%= name %>.model'); // Get list of <%= name %>s exports.index = function (req, res) { - <%= name %>.find(function (err, <%= name %> - s) { + <%= name %>.find(function (err, <%= name %>s) { if (err) { return handleError(res, err); } - return res.status(200).json(<%= name %> - s); + return res.status(200).json(<%= name %>s); }); }; From f6d7e4e07964a82f4451bf0691e92881a98e8ea2 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 17:07:39 +0200 Subject: [PATCH 03/22] Test correction --- endpoint/templates/name.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index dbe32bf45..298f0f8af 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -42,7 +42,7 @@ describe('GET /api/<%= name %>s', function () { .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_2id'); + res.body.should.have.property('_id'); done(); }); }); From 5fcef1d54f72369c3e4abcb3aafa13c4d8590fe8 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 12:53:45 +0200 Subject: [PATCH 04/22] Json messages, authenticated user requests --- endpoint/index.js | 9 +++++---- endpoint/templates/index.js | 1 - endpoint/templates/name.controller.js | 10 ++++----- endpoint/templates/name.spec.js | 29 +++++++++++++++++++++------ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 477e3c40e..2b604164d 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -54,14 +54,15 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { }; ngUtil.rewriteFile(routeConfig); - var rolesConfig = { + var rolesUserConfig = { file: this.config.get('configRolesFile'), - needle: this.config.get('configRolesNeedle'), + needle: this.config.get('configUserRolesNeedle'), splicable: [ - "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," + "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + + this.name + "s-create', '" + this.name + "s-destroy'," ] }; - ngUtil.rewriteFile(rolesConfig); + ngUtil.rewriteFile(rolesUserConfig); } }; diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 1ad549633..0f470ba2d 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -11,7 +11,6 @@ router.get('/', auth.hasRole('<%= name %>s-index'), controller.index); router.get('/:id', auth.hasRole('<%= name %>s-show'), controller.show); router.post('/', auth.hasRole('<%= name %>s-create'), controller.create); router.put('/:id', auth.hasRole('<%= name %>s-update'), controller.update); -router.patch('/:id', auth.hasRole('<%= name %>s-update'), controller.update); router.delete('/:id', auth.hasRole('<%= name %>s-destroy'), controller.destroy); module.exports = router; diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index d0bff2ab5..801f37a6e 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -20,7 +20,7 @@ exports.show = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } return res.json(<%= name %>); }); @@ -46,7 +46,7 @@ exports.update = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } var updated = _.merge(<%= name %>, req.body); updated.save(function (err) { @@ -65,17 +65,17 @@ exports.destroy = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } <%= name %>.remove(function (err) { if (err) { return handleError(res, err); } - return res.status(204).send('No Content'); + return res.status(204).json('No Content'); }); }); }; function handleError(res, err) { - return res.status(500).send(err); + return res.status(500).json(err); } diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 298f0f8af..922e8d496 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -3,13 +3,30 @@ var should = require('should'); var app = require('../../app'); var request = require('supertest'); +var test = require('../../test'); +var <%= name %> = require('./<%= name %>.model'); + +describe('<%= name %>s endpoint', function () { -describe('GET /api/<%= name %>s', function () { var id = 0; + var token = ''; + + before(function (done) { + test.auth(request, function (accessToken) { + token = '?access_token=' + accessToken; + done(); + }); + }); + + after(function (done) { + <%= name %>.remove().exec().then(function () { + done(); + }); + }); it('should respond with JSON array', function (done) { request(app) - .get('/api/<%= name%>s') + .get('/api/<%= name%>s' + token) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { @@ -22,7 +39,7 @@ describe('GET /api/<%= name %>s', function () { it('should create a new <%= name %>', function (done) { request(app) - .post('/api/<%= name%>s') + .post('/api/<%= name%>s' + token) .send() .expect(201) .expect('Content-Type', /json/) @@ -36,7 +53,7 @@ describe('GET /api/<%= name %>s', function () { it('should update a <%= name %>', function (done) { request(app) - .put('/api/<%= name%>s/' + id) + .put('/api/<%= name%>s/' + id + token) .send({}) .expect(200) .expect('Content-Type', /json/) @@ -49,7 +66,7 @@ describe('GET /api/<%= name %>s', function () { it('should get a <%= name %>', function (done) { request(app) - .get('/api/<%= name%>s/' + id) + .get('/api/<%= name%>s/' + id + token) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { @@ -61,7 +78,7 @@ describe('GET /api/<%= name %>s', function () { it('should delete a <%= name %>', function (done) { request(app) - .delete('/api/<%= name%>s/' + id) + .delete('/api/<%= name%>s/' + id + token) .expect(204) .end(function (err, res) { if (err) return done(err); From a5b95e00b1f3682bb74dcbc373d08d59d860e05d Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 16:13:47 +0200 Subject: [PATCH 05/22] Added some sample data to models and tests also --- endpoint/templates/name.spec.js | 55 ++++++++++++++++++++++++++++--- endpoint/templatesJson/names.json | 31 ++++++++++++++++- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 922e8d496..b186032e2 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -24,7 +24,7 @@ describe('<%= name %>s endpoint', function () { }); }); - it('should respond with JSON array', function (done) { + it('should respond with an empty JSON array', function (done) { request(app) .get('/api/<%= name%>s' + token) .expect(200) @@ -40,12 +40,37 @@ describe('<%= name %>s endpoint', function () { it('should create a new <%= name %>', function (done) { request(app) .post('/api/<%= name%>s' + token) - .send() + .send({ + name: 'Test name', + age: 30, + living: true, + mixed: { + any: { + thing: 'i want' + } + }, + array: [1], + ofString: ["strings!"], + ofNumber: [1, 2, 3, 4], + ofMixed: [1, [], 'three', { + four: 5 + }], + nested: { + stuff: 'Good ' + } + }) .expect(201) .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id'); + res.body.should.have.property('name', 'Test name'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "good" + } + }); id = res.body._id; done(); }); @@ -54,12 +79,25 @@ describe('<%= name %>s endpoint', function () { it('should update a <%= name %>', function (done) { request(app) .put('/api/<%= name%>s/' + id + token) - .send({}) + .send({ + name: 'Test name updated', + age: 40, + nested: { + stuff: 'bad' + } + }) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_id'); + res.body.should.have.property('_id', id); + res.body.should.have.property('name', 'Test name updated'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "bad" + } + }); done(); }); }); @@ -71,7 +109,14 @@ describe('<%= name %>s endpoint', function () { .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_id'); + res.body.should.have.property('_id', id); + res.body.should.have.property('name', 'Test name updated'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "bad" + } + }); done(); }); }); diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 9e26dfeeb..196806171 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1 +1,30 @@ -{} \ No newline at end of file +{ + "name": "String", + "binary": "Buffer", + "living": "Boolean", + "updated": { + "type": "Date" + }, + "age": { + "type": "Number", + "min": 18, + "max": 65 + }, + "mixed": "Mixed", + "_someId": "ObjectId", + "array": [], + "ofString": ["String"], + "ofNumber": ["Number"], + "ofDates": ["Date"], + "ofBuffer": ["Buffer"], + "ofBoolean": ["Boolean"], + "ofMixed": ["Mixed"], + "ofObjectId": ["ObjectId"], + "nested": { + "stuff": { + "type": "String", + "lowercase": true, + "trim": true + } + } +} From 77eccf2351bb754bc7e91ec889301b3cc7b2c35a Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 16:55:37 +0200 Subject: [PATCH 06/22] Sample model updated --- endpoint/templates/name.spec.js | 26 +++++++++++++------------- endpoint/templatesJson/names.json | 15 ++++++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index b186032e2..216747c7f 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -41,17 +41,17 @@ describe('<%= name %>s endpoint', function () { request(app) .post('/api/<%= name%>s' + token) .send({ - name: 'Test name', + string: 'Test string', age: 30, - living: true, + boolean: true, mixed: { - any: { - thing: 'i want' + a: { + b: 's', + c: true } }, array: [1], - ofString: ["strings!"], - ofNumber: [1, 2, 3, 4], + ofString: ["String1", "String2"], ofMixed: [1, [], 'three', { four: 5 }], @@ -64,8 +64,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id'); - res.body.should.have.property('name', 'Test name'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "good" @@ -80,7 +80,7 @@ describe('<%= name %>s endpoint', function () { request(app) .put('/api/<%= name%>s/' + id + token) .send({ - name: 'Test name updated', + string: 'Test string updated', age: 40, nested: { stuff: 'bad' @@ -91,8 +91,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id', id); - res.body.should.have.property('name', 'Test name updated'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string updated'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "bad" @@ -110,8 +110,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id', id); - res.body.should.have.property('name', 'Test name updated'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string updated'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "bad" diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 196806171..5f6b0ddad 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1,25 +1,22 @@ { - "name": "String", + "string": "String", "binary": "Buffer", - "living": "Boolean", + "boolean": "Boolean", "updated": { - "type": "Date" + "type": "Date", + "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)" }, "age": { "type": "Number", "min": 18, - "max": 65 + "max": 65, + "required": true }, "mixed": "Mixed", "_someId": "ObjectId", "array": [], "ofString": ["String"], - "ofNumber": ["Number"], - "ofDates": ["Date"], - "ofBuffer": ["Buffer"], - "ofBoolean": ["Boolean"], "ofMixed": ["Mixed"], - "ofObjectId": ["ObjectId"], "nested": { "stuff": { "type": "String", From 2b501bcc1a88a70ae80d7a6130b30af1cb1819ff Mon Sep 17 00:00:00 2001 From: zsapkagy Date: Wed, 5 Aug 2015 00:26:22 +0200 Subject: [PATCH 07/22] New config provider subgenerator The generator-ng-components dependency has been replaced to our forked and modified version --- configprovider/index.js | 10 ++++++++++ package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 configprovider/index.js diff --git a/configprovider/index.js b/configprovider/index.js new file mode 100644 index 000000000..0e0c50ea7 --- /dev/null +++ b/configprovider/index.js @@ -0,0 +1,10 @@ +'use strict'; +var yeoman = require('yeoman-generator'); + +var Generator = yeoman.generators.Base.extend({ + compose: function() { + this.composeWith('ng-component:configprovider', {arguments: this.arguments}, { local: require.resolve('generator-ng-component/configprovider') }); + } +}); + +module.exports = Generator; diff --git a/package.json b/package.json index 23d469194..6e17af926 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "yeoman-generator": "~0.17.0", "chalk": "~0.4.0", "wiredep": "~0.4.2", - "generator-ng-component": "~0.0.4" + "generator-ng-component": "https://github.com/ennosol/generator-ng-component.git" }, "peerDependencies": { "yo": ">=1.2.0" From 5c91b709f8b5793724813a916602d9b056ef47af Mon Sep 17 00:00:00 2001 From: laco1221 Date: Wed, 5 Aug 2015 10:36:08 +0200 Subject: [PATCH 08/22] Add roles multiple places --- endpoint/index.js | 9 ++++---- util.js | 56 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 2b604164d..88f361230 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -54,15 +54,16 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { }; ngUtil.rewriteFile(routeConfig); - var rolesUserConfig = { + var rolesConfig = { file: this.config.get('configRolesFile'), - needle: this.config.get('configUserRolesNeedle'), + needle: this.config.get('configRolesNeedle'), splicable: [ "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," - ] + ], + all: true }; - ngUtil.rewriteFile(rolesUserConfig); + ngUtil.rewriteFile(rolesConfig); } }; diff --git a/util.js b/util.js index 7544f8c8e..06eb092d5 100644 --- a/util.js +++ b/util.js @@ -23,7 +23,7 @@ function escapeRegExp (str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } -function rewrite (args) { +function rewrite(args) { // check if splicable is already in the body text var re = new RegExp(args.splicable.map(function (line) { return '\s*' + escapeRegExp(line); @@ -36,27 +36,49 @@ function rewrite (args) { var lines = args.haystack.split('\n'); var otherwiseLineIndex = -1; - lines.forEach(function (line, i) { - if (line.indexOf(args.needle) !== -1) { - otherwiseLineIndex = i; + if (args.all) { + // Adds the code everywhere the needle is found + lines.forEach(function (line, i) { + if (line.indexOf(args.needle) !== -1) { + otherwiseLineIndex = i; + var spaces = 0; + while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { + spaces += 1; + } + + var spaceStr = ''; + while ((spaces -= 1) >= 0) { + spaceStr += ' '; + } + + lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { + return spaceStr + line; + }).join('\n')); + } + }); + } else { + lines.forEach(function (line, i) { + if (line.indexOf(args.needle) !== -1) { + otherwiseLineIndex = i; + } + }); + if (otherwiseLineIndex === -1) return lines.join('\n'); + + var spaces = 0; + while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { + spaces += 1; } - }); - if(otherwiseLineIndex === -1) return lines.join('\n'); - var spaces = 0; - while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { - spaces += 1; - } + var spaceStr = ''; + while ((spaces -= 1) >= 0) { + spaceStr += ' '; + } - var spaceStr = ''; - while ((spaces -= 1) >= 0) { - spaceStr += ' '; + lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { + return spaceStr + line; + }).join('\n')); } - lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { - return spaceStr + line; - }).join('\n')); - return lines.join('\n'); } From 853c67332e0eabcf179597e7be6d3c3979d616f2 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Thu, 6 Aug 2015 11:55:28 +0200 Subject: [PATCH 09/22] Removed @api comment, added formly fields --- .../server/api/user(auth)/user.model.js | 5 +- endpoint/templatesJson/names.json | 60 +++++++++++++++++-- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/templates/server/api/user(auth)/user.model.js b/app/templates/server/api/user(auth)/user.model.js index cc8d59263..c96d14b0f 100644 --- a/app/templates/server/api/user(auth)/user.model.js +++ b/app/templates/server/api/user(auth)/user.model.js @@ -108,7 +108,7 @@ UserSchema }); /** - * Methods + * Public api methods */ UserSchema.methods = { /** @@ -116,7 +116,6 @@ UserSchema.methods = { * * @param {String} plainText * @return {Boolean} - * @api public */ authenticate: function(plainText) { return this.encryptPassword(plainText) === this.hashedPassword; @@ -126,7 +125,6 @@ UserSchema.methods = { * Make salt * * @return {String} - * @api public */ makeSalt: function() { return crypto.randomBytes(16).toString('base64'); @@ -137,7 +135,6 @@ UserSchema.methods = { * * @param {String} password * @return {String} - * @api public */ encryptPassword: function(password) { if (!password || !this.salt) return ''; diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 5f6b0ddad..5ad71d57d 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1,16 +1,64 @@ { - "string": "String", - "binary": "Buffer", - "boolean": "Boolean", + "string": { + "type": "String", + "formly": { + "key": "string", + "type": "input", + "templateOptions": { + "label": "Formly input text", + "placeholder": "Formly input text" + } + } + }, + "binary": { + "type": "Buffer" + }, + "boolean": { + "type": "Boolean", + "formly": { + "key": "boolean", + "type": "checkbox", + "templateOptions": { + "label": "Formly checkbox" + } + } + }, "updated": { "type": "Date", - "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)" + "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)", + "formly": { + "key": "updated", + "type": "input", + "defaultValue": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)", + "templateOptions": { + "label": "Updated date", + "placeholder": "Formly input text" + } + } }, "age": { "type": "Number", "min": 18, - "max": 65, - "required": true + "max": 25, + "required": true, + "formly": { + "key": "age", + "type": "select", + "templateOptions": { + "required": true, + "label": "Age - Formly select", + "options": [ + {"name": "18", "value": 18}, + {"name": "19", "value": 19}, + {"name": "20", "value": 20}, + {"name": "21", "value": 21}, + {"name": "22", "value": 22}, + {"name": "23", "value": 23}, + {"name": "24", "value": 24}, + {"name": "25", "value": 25} + ] + } + } }, "mixed": "Mixed", "_someId": "ObjectId", From 9204eba2424b90b658b71071256be83c4974bad1 Mon Sep 17 00:00:00 2001 From: zsapkagy Date: Thu, 6 Aug 2015 12:22:55 +0200 Subject: [PATCH 10/22] yoeman.png repair --- .../assets/images/{!yeoman.png => yeoman.png} | Bin 12331 -> 12330 bytes 1 file changed, 0 insertions(+), 0 deletions(-) rename app/templates/client/assets/images/{!yeoman.png => yeoman.png} (99%) diff --git a/app/templates/client/assets/images/!yeoman.png b/app/templates/client/assets/images/yeoman.png similarity index 99% rename from app/templates/client/assets/images/!yeoman.png rename to app/templates/client/assets/images/yeoman.png index 7d0a1ac7120ab1e1b06598a02f2670f52aa00829..fe2b154b05d602f4fa0ea5400e550093d680c221 100644 GIT binary patch delta 13 UcmZ3TuquJ2Gr-S%BdeML03~b$lmGw# delta 14 VcmZ3LusVUYGr-TCcO#3M0RSmF1fc)` From 55041fc6bdd9d588846e471e2027126698c5010c Mon Sep 17 00:00:00 2001 From: laco1221 Date: Thu, 6 Aug 2015 13:15:20 +0200 Subject: [PATCH 11/22] Tests corrected --- endpoint/templates/name.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 216747c7f..68faf76e0 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -42,7 +42,7 @@ describe('<%= name %>s endpoint', function () { .post('/api/<%= name%>s' + token) .send({ string: 'Test string', - age: 30, + age: 20, boolean: true, mixed: { a: { @@ -66,6 +66,7 @@ describe('<%= name %>s endpoint', function () { res.body.should.have.property('_id'); res.body.should.have.property('string', 'Test string'); res.body.should.have.property('boolean', true); + res.body.should.have.property('age', 20); res.body.should.have.properties({ "nested": { "stuff": "good" @@ -81,7 +82,7 @@ describe('<%= name %>s endpoint', function () { .put('/api/<%= name%>s/' + id + token) .send({ string: 'Test string updated', - age: 40, + age: 22, nested: { stuff: 'bad' } @@ -93,6 +94,7 @@ describe('<%= name %>s endpoint', function () { res.body.should.have.property('_id', id); res.body.should.have.property('string', 'Test string updated'); res.body.should.have.property('boolean', true); + res.body.should.have.property('age', 22); res.body.should.have.properties({ "nested": { "stuff": "bad" From 280b65bc10fd031f918dcfaceeb2aebd86f10e40 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 15:42:18 +0200 Subject: [PATCH 12/22] Endpoint generator updated --- endpoint/index.js | 32 +++--- endpoint/templates/index.js | 16 +-- endpoint/templates/name.controller.js | 103 ++++++++++++-------- endpoint/templates/name.model(mongoose).js | 12 --- endpoint/templates/name.model.js | 20 ++++ endpoint/templates/name.socket(socketio).js | 24 ----- endpoint/templates/name.spec.js | 77 ++++++++++++--- endpoint/templatesJson/names.json | 1 + 8 files changed, 173 insertions(+), 112 deletions(-) delete mode 100644 endpoint/templates/name.model(mongoose).js create mode 100644 endpoint/templates/name.model.js delete mode 100644 endpoint/templates/name.socket(socketio).js create mode 100644 endpoint/templatesJson/names.json diff --git a/endpoint/index.js b/endpoint/index.js index 2b3d7eb22..7d54fe439 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -16,7 +16,7 @@ Generator.prototype.askFor = function askFor() { var name = this.name; var base = this.config.get('routesBase') || '/api/'; - if(base.charAt(base.length-1) !== '/') { + if (base.charAt(base.length - 1) !== '/') { base = base + '/'; } @@ -34,7 +34,7 @@ Generator.prototype.askFor = function askFor() { ]; this.prompt(prompts, function (props) { - if(props.route.charAt(0) !== '/') { + if (props.route.charAt(0) !== '/') { props.route = '/' + props.route; } @@ -44,28 +44,24 @@ Generator.prototype.askFor = function askFor() { }; Generator.prototype.registerEndpoint = function registerEndpoint() { - if(this.config.get('insertRoutes')) { + if (this.config.get('insertRoutes')) { var routeConfig = { file: this.config.get('registerRoutesFile'), needle: this.config.get('routesNeedle'), splicable: [ - "app.use(\'" + this.route +"\', require(\'./api/" + this.name + "\'));" + "app.use(\'" + this.route + "\', require(\'./api/" + this.name + "\'));" ] }; ngUtil.rewriteFile(routeConfig); - } - if (this.filters.socketio) { - if(this.config.get('insertSockets')) { - var socketConfig = { - file: this.config.get('registerSocketsFile'), - needle: this.config.get('socketsNeedle'), - splicable: [ - "require(\'../api/" + this.name + '/' + this.name + ".socket\').register(socket);" - ] - }; - ngUtil.rewriteFile(socketConfig); - } + var rolesConfig = { + file: this.config.get('configRolesFile'), + needle: this.config.get('configRolesNeedle'), + splicable: [ + ", " + this.name + "s-index, " + this.name + "s-show, " + this.name + "s-update, " + this.name + "s-create, " + this.name + "s-destroy" + ] + }; + ngUtil.rewriteFile(rolesConfig); } }; @@ -73,4 +69,8 @@ Generator.prototype.createFiles = function createFiles() { var dest = this.config.get('endpointDirectory') || 'server/api/' + this.name; this.sourceRoot(path.join(__dirname, './templates')); ngUtil.processDirectory(this, '.', dest); + + var destSharedModels = 'shared/models'; + this.sourceRoot(path.join(__dirname, './templatesJson/')); + ngUtil.processDirectory(this, '.', destSharedModels); }; diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 03fdc9779..1ad549633 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -2,14 +2,16 @@ var express = require('express'); var controller = require('./<%= name %>.controller'); +var config = require('../../config/environment'); +var auth = require('../../auth/auth.service'); var router = express.Router(); -router.get('/', controller.index);<% if(filters.mongoose) { %> -router.get('/:id', controller.show); -router.post('/', controller.create); -router.put('/:id', controller.update); -router.patch('/:id', controller.update); -router.delete('/:id', controller.destroy);<% } %> +router.get('/', auth.hasRole('<%= name %>s-index'), controller.index); +router.get('/:id', auth.hasRole('<%= name %>s-show'), controller.show); +router.post('/', auth.hasRole('<%= name %>s-create'), controller.create); +router.put('/:id', auth.hasRole('<%= name %>s-update'), controller.update); +router.patch('/:id', auth.hasRole('<%= name %>s-update'), controller.update); +router.delete('/:id', auth.hasRole('<%= name %>s-destroy'), controller.destroy); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 3d46b2ad4..0269ba80d 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -1,60 +1,83 @@ 'use strict'; -var _ = require('lodash');<% if (filters.mongoose) { %> -var <%= classedName %> = require('./<%= name %>.model');<% } %> +var _ = require('lodash'); +var <%= name %> = require('./<%= name %>.model'); // Get list of <%= name %>s -exports.index = function(req, res) {<% if (!filters.mongoose) { %> - res.json([]);<% } %><% if (filters.mongoose) { %> - <%= classedName %>.find(function (err, <%= name %>s) { - if(err) { return handleError(res, err); } - return res.status(200).json(<%= name %>s); - });<% } %> -};<% if (filters.mongoose) { %> +exports.index = function (req, res) { + <%= name %>.find(function (err, <%= name %> + s) { + if (err) { + return handleError(res, err); + } + return res.status(200).json(<%= name %> + s); + }); +}; // Get a single <%= name %> -exports.show = function(req, res) { - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if(err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - return res.json(<%= name %>); - }); +exports.show = function (req, res) { + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + return res.json(<%= name %>); + }); }; // Creates a new <%= name %> in the DB. -exports.create = function(req, res) { - <%= classedName %>.create(req.body, function(err, <%= name %>) { - if(err) { return handleError(res, err); } - return res.status(201).json(<%= name %>); - }); +exports.create = function (req, res) { + <%= name %>.create(req.body, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + return res.status(201).json(<%= name %>); + }); }; // Updates an existing <%= name %> in the DB. -exports.update = function(req, res) { - if(req.body._id) { delete req.body._id; } - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if (err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - var updated = _.merge(<%= name %>, req.body); - updated.save(function (err) { - if (err) { return handleError(res, err); } - return res.status(200).json(<%= name %>); +exports.update = function (req, res) { + if (req.body._id) { + delete req.body._id; + } + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + var updated = _.merge(<%= name %>, req.body); + updated.save(function (err) { + if (err) { + return handleError(res, err); + } + return res.status(200).json(<%= name %>); + }); }); - }); }; // Deletes a <%= name %> from the DB. -exports.destroy = function(req, res) { - <%= classedName %>.findById(req.params.id, function (err, <%= name %>) { - if(err) { return handleError(res, err); } - if(!<%= name %>) { return res.status(404).send('Not Found'); } - <%= name %>.remove(function(err) { - if(err) { return handleError(res, err); } - return res.status(204).send('No Content'); +exports.destroy = function (req, res) { + <%= name %>.findById(req.params.id, function (err, <%= name %>) { + if (err) { + return handleError(res, err); + } + if (!<%= name %>) { + return res.status(404).send('Not Found'); + } + <%= name %>.remove(function (err) { + if (err) { + return handleError(res, err); + } + return res.status(204).send('No Content'); + }); }); - }); }; function handleError(res, err) { - return res.status(500).send(err); -}<% } %> \ No newline at end of file + return res.status(500).send(err); +} diff --git a/endpoint/templates/name.model(mongoose).js b/endpoint/templates/name.model(mongoose).js deleted file mode 100644 index 89e0dfaa7..000000000 --- a/endpoint/templates/name.model(mongoose).js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var mongoose = require('mongoose'), - Schema = mongoose.Schema; - -var <%= classedName %>Schema = new Schema({ - name: String, - info: String, - active: Boolean -}); - -module.exports = mongoose.model('<%= classedName %>', <%= classedName %>Schema); \ No newline at end of file diff --git a/endpoint/templates/name.model.js b/endpoint/templates/name.model.js new file mode 100644 index 000000000..635460512 --- /dev/null +++ b/endpoint/templates/name.model.js @@ -0,0 +1,20 @@ +'use strict'; + +// Dependencies +var mongoose = require('mongoose'); +var mongoose_delete = require('mongoose-delete'); +var mongoose_timestamp = require('mongoose-timestamp'); + +// Model dependencies + +// Schema +var schema = new mongoose.Schema(require('./../../../shared/models/<%= name%>s.json')); + +// Schema plugins +schema.plugin(mongoose_delete, { + deletedAt: true +}); +schema.plugin(mongoose_timestamp); + +// Return model +module.exports = mongoose.model('<%= name %>', schema); diff --git a/endpoint/templates/name.socket(socketio).js b/endpoint/templates/name.socket(socketio).js deleted file mode 100644 index 886f585ee..000000000 --- a/endpoint/templates/name.socket(socketio).js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Broadcast updates to client when the model changes - */ - -'use strict'; - -var <%= classedName %> = require('./<%= name %>.model'); - -exports.register = function(socket) { - <%= classedName %>.schema.post('save', function (doc) { - onSave(socket, doc); - }); - <%= classedName %>.schema.post('remove', function (doc) { - onRemove(socket, doc); - }); -} - -function onSave(socket, doc, cb) { - socket.emit('<%= name %>:save', doc); -} - -function onRemove(socket, doc, cb) { - socket.emit('<%= name %>:remove', doc); -} \ No newline at end of file diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index fcad73ebd..dbe32bf45 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -4,17 +4,68 @@ var should = require('should'); var app = require('../../app'); var request = require('supertest'); -describe('GET <%= route %>', function() { +describe('GET /api/<%= name %>s', function () { + var id = 0; - it('should respond with JSON array', function(done) { - request(app) - .get('<%= route %>') - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) return done(err); - res.body.should.be.instanceof(Array); - done(); - }); - }); -}); \ No newline at end of file + it('should respond with JSON array', function (done) { + request(app) + .get('/api/<%= name%>s') + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.be.instanceof(Array); + res.body.should.have.length(0); + done(); + }); + }); + + it('should create a new <%= name %>', function (done) { + request(app) + .post('/api/<%= name%>s') + .send() + .expect(201) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_id'); + id = res.body._id; + done(); + }); + }); + + it('should update a <%= name %>', function (done) { + request(app) + .put('/api/<%= name%>s/' + id) + .send({}) + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_2id'); + done(); + }); + }); + + it('should get a <%= name %>', function (done) { + request(app) + .get('/api/<%= name%>s/' + id) + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) return done(err); + res.body.should.have.property('_id'); + done(); + }); + }); + + it('should delete a <%= name %>', function (done) { + request(app) + .delete('/api/<%= name%>s/' + id) + .expect(204) + .end(function (err, res) { + if (err) return done(err); + done(); + }); + }); +}); diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/endpoint/templatesJson/names.json @@ -0,0 +1 @@ +{} \ No newline at end of file From dc9137e19c8838106a232999a84099960d9bca2a Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 16:19:44 +0200 Subject: [PATCH 13/22] Some correction --- endpoint/index.js | 2 +- endpoint/templates/name.controller.js | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 7d54fe439..477e3c40e 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -58,7 +58,7 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { file: this.config.get('configRolesFile'), needle: this.config.get('configRolesNeedle'), splicable: [ - ", " + this.name + "s-index, " + this.name + "s-show, " + this.name + "s-update, " + this.name + "s-create, " + this.name + "s-destroy" + "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," ] }; ngUtil.rewriteFile(rolesConfig); diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 0269ba80d..d0bff2ab5 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -5,13 +5,11 @@ var <%= name %> = require('./<%= name %>.model'); // Get list of <%= name %>s exports.index = function (req, res) { - <%= name %>.find(function (err, <%= name %> - s) { + <%= name %>.find(function (err, <%= name %>s) { if (err) { return handleError(res, err); } - return res.status(200).json(<%= name %> - s); + return res.status(200).json(<%= name %>s); }); }; From 4cd549391c5f392ccad61f0ca07d9d6f7a05b315 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Mon, 3 Aug 2015 17:07:39 +0200 Subject: [PATCH 14/22] Test correction --- endpoint/templates/name.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index dbe32bf45..298f0f8af 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -42,7 +42,7 @@ describe('GET /api/<%= name %>s', function () { .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_2id'); + res.body.should.have.property('_id'); done(); }); }); From 86f40986b50516a340fa15e524a9e4745490ff0d Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 12:53:45 +0200 Subject: [PATCH 15/22] Json messages, authenticated user requests --- endpoint/index.js | 9 +++++---- endpoint/templates/index.js | 1 - endpoint/templates/name.controller.js | 10 ++++----- endpoint/templates/name.spec.js | 29 +++++++++++++++++++++------ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 477e3c40e..2b604164d 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -54,14 +54,15 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { }; ngUtil.rewriteFile(routeConfig); - var rolesConfig = { + var rolesUserConfig = { file: this.config.get('configRolesFile'), - needle: this.config.get('configRolesNeedle'), + needle: this.config.get('configUserRolesNeedle'), splicable: [ - "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," + "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + + this.name + "s-create', '" + this.name + "s-destroy'," ] }; - ngUtil.rewriteFile(rolesConfig); + ngUtil.rewriteFile(rolesUserConfig); } }; diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 1ad549633..0f470ba2d 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -11,7 +11,6 @@ router.get('/', auth.hasRole('<%= name %>s-index'), controller.index); router.get('/:id', auth.hasRole('<%= name %>s-show'), controller.show); router.post('/', auth.hasRole('<%= name %>s-create'), controller.create); router.put('/:id', auth.hasRole('<%= name %>s-update'), controller.update); -router.patch('/:id', auth.hasRole('<%= name %>s-update'), controller.update); router.delete('/:id', auth.hasRole('<%= name %>s-destroy'), controller.destroy); module.exports = router; diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index d0bff2ab5..801f37a6e 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -20,7 +20,7 @@ exports.show = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } return res.json(<%= name %>); }); @@ -46,7 +46,7 @@ exports.update = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } var updated = _.merge(<%= name %>, req.body); updated.save(function (err) { @@ -65,17 +65,17 @@ exports.destroy = function (req, res) { return handleError(res, err); } if (!<%= name %>) { - return res.status(404).send('Not Found'); + return res.status(404).json('Not Found'); } <%= name %>.remove(function (err) { if (err) { return handleError(res, err); } - return res.status(204).send('No Content'); + return res.status(204).json('No Content'); }); }); }; function handleError(res, err) { - return res.status(500).send(err); + return res.status(500).json(err); } diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 298f0f8af..922e8d496 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -3,13 +3,30 @@ var should = require('should'); var app = require('../../app'); var request = require('supertest'); +var test = require('../../test'); +var <%= name %> = require('./<%= name %>.model'); + +describe('<%= name %>s endpoint', function () { -describe('GET /api/<%= name %>s', function () { var id = 0; + var token = ''; + + before(function (done) { + test.auth(request, function (accessToken) { + token = '?access_token=' + accessToken; + done(); + }); + }); + + after(function (done) { + <%= name %>.remove().exec().then(function () { + done(); + }); + }); it('should respond with JSON array', function (done) { request(app) - .get('/api/<%= name%>s') + .get('/api/<%= name%>s' + token) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { @@ -22,7 +39,7 @@ describe('GET /api/<%= name %>s', function () { it('should create a new <%= name %>', function (done) { request(app) - .post('/api/<%= name%>s') + .post('/api/<%= name%>s' + token) .send() .expect(201) .expect('Content-Type', /json/) @@ -36,7 +53,7 @@ describe('GET /api/<%= name %>s', function () { it('should update a <%= name %>', function (done) { request(app) - .put('/api/<%= name%>s/' + id) + .put('/api/<%= name%>s/' + id + token) .send({}) .expect(200) .expect('Content-Type', /json/) @@ -49,7 +66,7 @@ describe('GET /api/<%= name %>s', function () { it('should get a <%= name %>', function (done) { request(app) - .get('/api/<%= name%>s/' + id) + .get('/api/<%= name%>s/' + id + token) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { @@ -61,7 +78,7 @@ describe('GET /api/<%= name %>s', function () { it('should delete a <%= name %>', function (done) { request(app) - .delete('/api/<%= name%>s/' + id) + .delete('/api/<%= name%>s/' + id + token) .expect(204) .end(function (err, res) { if (err) return done(err); From 4de296c034c0b7c22dd2ba906e6afc2121f3ee18 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 16:13:47 +0200 Subject: [PATCH 16/22] Added some sample data to models and tests also --- endpoint/templates/name.spec.js | 55 ++++++++++++++++++++++++++++--- endpoint/templatesJson/names.json | 31 ++++++++++++++++- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 922e8d496..b186032e2 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -24,7 +24,7 @@ describe('<%= name %>s endpoint', function () { }); }); - it('should respond with JSON array', function (done) { + it('should respond with an empty JSON array', function (done) { request(app) .get('/api/<%= name%>s' + token) .expect(200) @@ -40,12 +40,37 @@ describe('<%= name %>s endpoint', function () { it('should create a new <%= name %>', function (done) { request(app) .post('/api/<%= name%>s' + token) - .send() + .send({ + name: 'Test name', + age: 30, + living: true, + mixed: { + any: { + thing: 'i want' + } + }, + array: [1], + ofString: ["strings!"], + ofNumber: [1, 2, 3, 4], + ofMixed: [1, [], 'three', { + four: 5 + }], + nested: { + stuff: 'Good ' + } + }) .expect(201) .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id'); + res.body.should.have.property('name', 'Test name'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "good" + } + }); id = res.body._id; done(); }); @@ -54,12 +79,25 @@ describe('<%= name %>s endpoint', function () { it('should update a <%= name %>', function (done) { request(app) .put('/api/<%= name%>s/' + id + token) - .send({}) + .send({ + name: 'Test name updated', + age: 40, + nested: { + stuff: 'bad' + } + }) .expect(200) .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_id'); + res.body.should.have.property('_id', id); + res.body.should.have.property('name', 'Test name updated'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "bad" + } + }); done(); }); }); @@ -71,7 +109,14 @@ describe('<%= name %>s endpoint', function () { .expect('Content-Type', /json/) .end(function (err, res) { if (err) return done(err); - res.body.should.have.property('_id'); + res.body.should.have.property('_id', id); + res.body.should.have.property('name', 'Test name updated'); + res.body.should.have.property('living', true); + res.body.should.have.properties({ + "nested": { + "stuff": "bad" + } + }); done(); }); }); diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 9e26dfeeb..196806171 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1 +1,30 @@ -{} \ No newline at end of file +{ + "name": "String", + "binary": "Buffer", + "living": "Boolean", + "updated": { + "type": "Date" + }, + "age": { + "type": "Number", + "min": 18, + "max": 65 + }, + "mixed": "Mixed", + "_someId": "ObjectId", + "array": [], + "ofString": ["String"], + "ofNumber": ["Number"], + "ofDates": ["Date"], + "ofBuffer": ["Buffer"], + "ofBoolean": ["Boolean"], + "ofMixed": ["Mixed"], + "ofObjectId": ["ObjectId"], + "nested": { + "stuff": { + "type": "String", + "lowercase": true, + "trim": true + } + } +} From 694c781dac2b6ea7fac1f05a6650b76272cf2291 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Tue, 4 Aug 2015 16:55:37 +0200 Subject: [PATCH 17/22] Sample model updated --- endpoint/templates/name.spec.js | 26 +++++++++++++------------- endpoint/templatesJson/names.json | 15 ++++++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index b186032e2..216747c7f 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -41,17 +41,17 @@ describe('<%= name %>s endpoint', function () { request(app) .post('/api/<%= name%>s' + token) .send({ - name: 'Test name', + string: 'Test string', age: 30, - living: true, + boolean: true, mixed: { - any: { - thing: 'i want' + a: { + b: 's', + c: true } }, array: [1], - ofString: ["strings!"], - ofNumber: [1, 2, 3, 4], + ofString: ["String1", "String2"], ofMixed: [1, [], 'three', { four: 5 }], @@ -64,8 +64,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id'); - res.body.should.have.property('name', 'Test name'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "good" @@ -80,7 +80,7 @@ describe('<%= name %>s endpoint', function () { request(app) .put('/api/<%= name%>s/' + id + token) .send({ - name: 'Test name updated', + string: 'Test string updated', age: 40, nested: { stuff: 'bad' @@ -91,8 +91,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id', id); - res.body.should.have.property('name', 'Test name updated'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string updated'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "bad" @@ -110,8 +110,8 @@ describe('<%= name %>s endpoint', function () { .end(function (err, res) { if (err) return done(err); res.body.should.have.property('_id', id); - res.body.should.have.property('name', 'Test name updated'); - res.body.should.have.property('living', true); + res.body.should.have.property('string', 'Test string updated'); + res.body.should.have.property('boolean', true); res.body.should.have.properties({ "nested": { "stuff": "bad" diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 196806171..5f6b0ddad 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1,25 +1,22 @@ { - "name": "String", + "string": "String", "binary": "Buffer", - "living": "Boolean", + "boolean": "Boolean", "updated": { - "type": "Date" + "type": "Date", + "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)" }, "age": { "type": "Number", "min": 18, - "max": 65 + "max": 65, + "required": true }, "mixed": "Mixed", "_someId": "ObjectId", "array": [], "ofString": ["String"], - "ofNumber": ["Number"], - "ofDates": ["Date"], - "ofBuffer": ["Buffer"], - "ofBoolean": ["Boolean"], "ofMixed": ["Mixed"], - "ofObjectId": ["ObjectId"], "nested": { "stuff": { "type": "String", From e623b94829a114bd4ec144a6f6b935455c61593e Mon Sep 17 00:00:00 2001 From: laco1221 Date: Wed, 5 Aug 2015 10:36:08 +0200 Subject: [PATCH 18/22] Add roles multiple places --- endpoint/index.js | 9 ++++---- util.js | 56 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/endpoint/index.js b/endpoint/index.js index 2b604164d..88f361230 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -54,15 +54,16 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { }; ngUtil.rewriteFile(routeConfig); - var rolesUserConfig = { + var rolesConfig = { file: this.config.get('configRolesFile'), - needle: this.config.get('configUserRolesNeedle'), + needle: this.config.get('configRolesNeedle'), splicable: [ "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + this.name + "s-create', '" + this.name + "s-destroy'," - ] + ], + all: true }; - ngUtil.rewriteFile(rolesUserConfig); + ngUtil.rewriteFile(rolesConfig); } }; diff --git a/util.js b/util.js index 7544f8c8e..06eb092d5 100644 --- a/util.js +++ b/util.js @@ -23,7 +23,7 @@ function escapeRegExp (str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } -function rewrite (args) { +function rewrite(args) { // check if splicable is already in the body text var re = new RegExp(args.splicable.map(function (line) { return '\s*' + escapeRegExp(line); @@ -36,27 +36,49 @@ function rewrite (args) { var lines = args.haystack.split('\n'); var otherwiseLineIndex = -1; - lines.forEach(function (line, i) { - if (line.indexOf(args.needle) !== -1) { - otherwiseLineIndex = i; + if (args.all) { + // Adds the code everywhere the needle is found + lines.forEach(function (line, i) { + if (line.indexOf(args.needle) !== -1) { + otherwiseLineIndex = i; + var spaces = 0; + while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { + spaces += 1; + } + + var spaceStr = ''; + while ((spaces -= 1) >= 0) { + spaceStr += ' '; + } + + lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { + return spaceStr + line; + }).join('\n')); + } + }); + } else { + lines.forEach(function (line, i) { + if (line.indexOf(args.needle) !== -1) { + otherwiseLineIndex = i; + } + }); + if (otherwiseLineIndex === -1) return lines.join('\n'); + + var spaces = 0; + while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { + spaces += 1; } - }); - if(otherwiseLineIndex === -1) return lines.join('\n'); - var spaces = 0; - while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { - spaces += 1; - } + var spaceStr = ''; + while ((spaces -= 1) >= 0) { + spaceStr += ' '; + } - var spaceStr = ''; - while ((spaces -= 1) >= 0) { - spaceStr += ' '; + lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { + return spaceStr + line; + }).join('\n')); } - lines.splice(otherwiseLineIndex + 1, 0, args.splicable.map(function (line) { - return spaceStr + line; - }).join('\n')); - return lines.join('\n'); } From aaa0989980ffe185f28e09b362f79e5498acc75e Mon Sep 17 00:00:00 2001 From: laco1221 Date: Thu, 6 Aug 2015 11:55:28 +0200 Subject: [PATCH 19/22] Removed @api comment, added formly fields --- .../server/api/user(auth)/user.model.js | 5 +- endpoint/templatesJson/names.json | 60 +++++++++++++++++-- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/templates/server/api/user(auth)/user.model.js b/app/templates/server/api/user(auth)/user.model.js index cc8d59263..c96d14b0f 100644 --- a/app/templates/server/api/user(auth)/user.model.js +++ b/app/templates/server/api/user(auth)/user.model.js @@ -108,7 +108,7 @@ UserSchema }); /** - * Methods + * Public api methods */ UserSchema.methods = { /** @@ -116,7 +116,6 @@ UserSchema.methods = { * * @param {String} plainText * @return {Boolean} - * @api public */ authenticate: function(plainText) { return this.encryptPassword(plainText) === this.hashedPassword; @@ -126,7 +125,6 @@ UserSchema.methods = { * Make salt * * @return {String} - * @api public */ makeSalt: function() { return crypto.randomBytes(16).toString('base64'); @@ -137,7 +135,6 @@ UserSchema.methods = { * * @param {String} password * @return {String} - * @api public */ encryptPassword: function(password) { if (!password || !this.salt) return ''; diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/names.json index 5f6b0ddad..5ad71d57d 100644 --- a/endpoint/templatesJson/names.json +++ b/endpoint/templatesJson/names.json @@ -1,16 +1,64 @@ { - "string": "String", - "binary": "Buffer", - "boolean": "Boolean", + "string": { + "type": "String", + "formly": { + "key": "string", + "type": "input", + "templateOptions": { + "label": "Formly input text", + "placeholder": "Formly input text" + } + } + }, + "binary": { + "type": "Buffer" + }, + "boolean": { + "type": "Boolean", + "formly": { + "key": "boolean", + "type": "checkbox", + "templateOptions": { + "label": "Formly checkbox" + } + } + }, "updated": { "type": "Date", - "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)" + "default": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)", + "formly": { + "key": "updated", + "type": "input", + "defaultValue": "Tue Aug 04 2015 16:47:28 GMT+0200 (CEST)", + "templateOptions": { + "label": "Updated date", + "placeholder": "Formly input text" + } + } }, "age": { "type": "Number", "min": 18, - "max": 65, - "required": true + "max": 25, + "required": true, + "formly": { + "key": "age", + "type": "select", + "templateOptions": { + "required": true, + "label": "Age - Formly select", + "options": [ + {"name": "18", "value": 18}, + {"name": "19", "value": 19}, + {"name": "20", "value": 20}, + {"name": "21", "value": 21}, + {"name": "22", "value": 22}, + {"name": "23", "value": 23}, + {"name": "24", "value": 24}, + {"name": "25", "value": 25} + ] + } + } }, "mixed": "Mixed", "_someId": "ObjectId", From 1589dce273b3b03b529f0112334be7bbdd52ab30 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Thu, 6 Aug 2015 13:15:20 +0200 Subject: [PATCH 20/22] Tests corrected --- endpoint/templates/name.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 216747c7f..68faf76e0 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -42,7 +42,7 @@ describe('<%= name %>s endpoint', function () { .post('/api/<%= name%>s' + token) .send({ string: 'Test string', - age: 30, + age: 20, boolean: true, mixed: { a: { @@ -66,6 +66,7 @@ describe('<%= name %>s endpoint', function () { res.body.should.have.property('_id'); res.body.should.have.property('string', 'Test string'); res.body.should.have.property('boolean', true); + res.body.should.have.property('age', 20); res.body.should.have.properties({ "nested": { "stuff": "good" @@ -81,7 +82,7 @@ describe('<%= name %>s endpoint', function () { .put('/api/<%= name%>s/' + id + token) .send({ string: 'Test string updated', - age: 40, + age: 22, nested: { stuff: 'bad' } @@ -93,6 +94,7 @@ describe('<%= name %>s endpoint', function () { res.body.should.have.property('_id', id); res.body.should.have.property('string', 'Test string updated'); res.body.should.have.property('boolean', true); + res.body.should.have.property('age', 22); res.body.should.have.properties({ "nested": { "stuff": "bad" From 2cb142f8241ce1e6c7313c054d3b21268f204650 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Thu, 6 Aug 2015 14:31:04 +0200 Subject: [PATCH 21/22] Style updated --- endpoint/templates/name.controller.js | 24 ++++++------ endpoint/templates/name.model.js | 8 ++-- endpoint/templates/name.spec.js | 54 +++++++++++++-------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 801f37a6e..7e1aea276 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -4,8 +4,8 @@ var _ = require('lodash'); var <%= name %> = require('./<%= name %>.model'); // Get list of <%= name %>s -exports.index = function (req, res) { - <%= name %>.find(function (err, <%= name %>s) { +exports.index = function(req, res) { + <%= name %>.find(function(err, <%= name %>s) { if (err) { return handleError(res, err); } @@ -14,8 +14,8 @@ exports.index = function (req, res) { }; // Get a single <%= name %> -exports.show = function (req, res) { - <%= name %>.findById(req.params.id, function (err, <%= name %>) { +exports.show = function(req, res) { + <%= name %>.findById(req.params.id, function(err, <%= name %>) { if (err) { return handleError(res, err); } @@ -27,8 +27,8 @@ exports.show = function (req, res) { }; // Creates a new <%= name %> in the DB. -exports.create = function (req, res) { - <%= name %>.create(req.body, function (err, <%= name %>) { +exports.create = function(req, res) { + <%= name %>.create(req.body, function(err, <%= name %>) { if (err) { return handleError(res, err); } @@ -37,11 +37,11 @@ exports.create = function (req, res) { }; // Updates an existing <%= name %> in the DB. -exports.update = function (req, res) { +exports.update = function(req, res) { if (req.body._id) { delete req.body._id; } - <%= name %>.findById(req.params.id, function (err, <%= name %>) { + <%= name %>.findById(req.params.id, function(err, <%= name %>) { if (err) { return handleError(res, err); } @@ -49,7 +49,7 @@ exports.update = function (req, res) { return res.status(404).json('Not Found'); } var updated = _.merge(<%= name %>, req.body); - updated.save(function (err) { + updated.save(function(err) { if (err) { return handleError(res, err); } @@ -59,15 +59,15 @@ exports.update = function (req, res) { }; // Deletes a <%= name %> from the DB. -exports.destroy = function (req, res) { - <%= name %>.findById(req.params.id, function (err, <%= name %>) { +exports.destroy = function(req, res) { + <%= name %>.findById(req.params.id, function(err, <%= name %>) { if (err) { return handleError(res, err); } if (!<%= name %>) { return res.status(404).json('Not Found'); } - <%= name %>.remove(function (err) { + <%= name %>.remove(function(err) { if (err) { return handleError(res, err); } diff --git a/endpoint/templates/name.model.js b/endpoint/templates/name.model.js index 635460512..f8b043626 100644 --- a/endpoint/templates/name.model.js +++ b/endpoint/templates/name.model.js @@ -2,8 +2,8 @@ // Dependencies var mongoose = require('mongoose'); -var mongoose_delete = require('mongoose-delete'); -var mongoose_timestamp = require('mongoose-timestamp'); +var mongooseDelete = require('mongoose-delete'); +var mongooseTimestamp = require('mongoose-timestamp'); // Model dependencies @@ -11,10 +11,10 @@ var mongoose_timestamp = require('mongoose-timestamp'); var schema = new mongoose.Schema(require('./../../../shared/models/<%= name%>s.json')); // Schema plugins -schema.plugin(mongoose_delete, { +schema.plugin(mongooseDelete, { deletedAt: true }); -schema.plugin(mongoose_timestamp); +schema.plugin(mongooseTimestamp); // Return model module.exports = mongoose.model('<%= name %>', schema); diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 68faf76e0..3dc87b88b 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -6,38 +6,38 @@ var request = require('supertest'); var test = require('../../test'); var <%= name %> = require('./<%= name %>.model'); -describe('<%= name %>s endpoint', function () { +describe('<%= name %>s endpoint', function() { var id = 0; var token = ''; - before(function (done) { - test.auth(request, function (accessToken) { + before(function(done) { + test.auth(request, function(accessToken) { token = '?access_token=' + accessToken; done(); }); }); - after(function (done) { - <%= name %>.remove().exec().then(function () { + after(function(done) { + <%= name %>.remove().exec().then(function() { done(); }); }); - it('should respond with an empty JSON array', function (done) { + it('should respond with an empty JSON array', function(done) { request(app) .get('/api/<%= name%>s' + token) .expect(200) .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) return done(err); + .end(function(err, res) { + if (err) {return done(err);} res.body.should.be.instanceof(Array); res.body.should.have.length(0); done(); }); }); - it('should create a new <%= name %>', function (done) { + it('should create a new <%= name %>', function(done) { request(app) .post('/api/<%= name%>s' + token) .send({ @@ -51,7 +51,7 @@ describe('<%= name %>s endpoint', function () { } }, array: [1], - ofString: ["String1", "String2"], + ofString: ['String1', 'String2'], ofMixed: [1, [], 'three', { four: 5 }], @@ -61,15 +61,15 @@ describe('<%= name %>s endpoint', function () { }) .expect(201) .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) return done(err); + .end(function(err, res) { + if (err) {return done(err);} res.body.should.have.property('_id'); res.body.should.have.property('string', 'Test string'); res.body.should.have.property('boolean', true); res.body.should.have.property('age', 20); res.body.should.have.properties({ - "nested": { - "stuff": "good" + 'nested': { + 'stuff': 'good' } }); id = res.body._id; @@ -77,7 +77,7 @@ describe('<%= name %>s endpoint', function () { }); }); - it('should update a <%= name %>', function (done) { + it('should update a <%= name %>', function(done) { request(app) .put('/api/<%= name%>s/' + id + token) .send({ @@ -89,46 +89,46 @@ describe('<%= name %>s endpoint', function () { }) .expect(200) .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) return done(err); + .end(function(err, res) { + if (err) {return done(err);} res.body.should.have.property('_id', id); res.body.should.have.property('string', 'Test string updated'); res.body.should.have.property('boolean', true); res.body.should.have.property('age', 22); res.body.should.have.properties({ - "nested": { - "stuff": "bad" + 'nested': { + 'stuff': 'bad' } }); done(); }); }); - it('should get a <%= name %>', function (done) { + it('should get a <%= name %>', function(done) { request(app) .get('/api/<%= name%>s/' + id + token) .expect(200) .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) return done(err); + .end(function(err, res) { + if (err) {return done(err);} res.body.should.have.property('_id', id); res.body.should.have.property('string', 'Test string updated'); res.body.should.have.property('boolean', true); res.body.should.have.properties({ - "nested": { - "stuff": "bad" + 'nested': { + 'stuff': 'bad' } }); done(); }); }); - it('should delete a <%= name %>', function (done) { + it('should delete a <%= name %>', function(done) { request(app) .delete('/api/<%= name%>s/' + id + token) .expect(204) - .end(function (err, res) { - if (err) return done(err); + .end(function(err, res) { + if (err) {return done(err);} done(); }); }); From 0021e85f226687c937c760000f87711802f93b41 Mon Sep 17 00:00:00 2001 From: laco1221 Date: Sat, 8 Aug 2015 11:04:54 +0200 Subject: [PATCH 22/22] Added pluralname --- endpoint/index.js | 18 ++++++++++-------- endpoint/templates/index.js | 10 +++++----- endpoint/templates/name.controller.js | 6 +++--- endpoint/templates/name.model.js | 2 +- endpoint/templates/name.spec.js | 12 ++++++------ .../{names.json => pluralName.json} | 0 util.js | 3 +++ 7 files changed, 28 insertions(+), 23 deletions(-) rename endpoint/templatesJson/{names.json => pluralName.json} (100%) diff --git a/endpoint/index.js b/endpoint/index.js index 88f361230..303da1d2e 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -14,22 +14,23 @@ util.inherits(Generator, ScriptBase); Generator.prototype.askFor = function askFor() { var done = this.async(); var name = this.name; + var pluralName = this.pluralName; var base = this.config.get('routesBase') || '/api/'; if (base.charAt(base.length - 1) !== '/') { base = base + '/'; } - // pluralization defaults to true for backwards compat - if (this.config.get('pluralizeRoutes') !== false) { - name = name + 's'; - } - var prompts = [ + { + name: 'pluralName', + message: 'What will the plural name of your endpoint be?', + default: name + 's' + }, { name: 'route', message: 'What will the url of your endpoint be?', - default: base + name + default: base + name + 's' } ]; @@ -39,6 +40,7 @@ Generator.prototype.askFor = function askFor() { } this.route = props.route; + this.pluralName = props.pluralName; done(); }.bind(this)); }; @@ -58,8 +60,8 @@ Generator.prototype.registerEndpoint = function registerEndpoint() { file: this.config.get('configRolesFile'), needle: this.config.get('configRolesNeedle'), splicable: [ - "'" + this.name + "s-index', '" + this.name + "s-show', '" + this.name + "s-update', '" + - this.name + "s-create', '" + this.name + "s-destroy'," + "'" + this.pluralName + "-index', '" + this.pluralName + "-show', '" + this.pluralName + "-update', '" + + this.pluralName + "-create', '" + this.pluralName + "-destroy'," ], all: true }; diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 0f470ba2d..748358a78 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -7,10 +7,10 @@ var auth = require('../../auth/auth.service'); var router = express.Router(); -router.get('/', auth.hasRole('<%= name %>s-index'), controller.index); -router.get('/:id', auth.hasRole('<%= name %>s-show'), controller.show); -router.post('/', auth.hasRole('<%= name %>s-create'), controller.create); -router.put('/:id', auth.hasRole('<%= name %>s-update'), controller.update); -router.delete('/:id', auth.hasRole('<%= name %>s-destroy'), controller.destroy); +router.get('/', auth.hasRole('<%= pluralName %>-index'), controller.index); +router.get('/:id', auth.hasRole('<%= pluralName %>-show'), controller.show); +router.post('/', auth.hasRole('<%= pluralName %>-create'), controller.create); +router.put('/:id', auth.hasRole('<%= pluralName %>-update'), controller.update); +router.delete('/:id', auth.hasRole('<%= pluralName %>-destroy'), controller.destroy); module.exports = router; diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 7e1aea276..2327d4e90 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -3,13 +3,13 @@ var _ = require('lodash'); var <%= name %> = require('./<%= name %>.model'); -// Get list of <%= name %>s +// Get list of <%= pluralName %> exports.index = function(req, res) { - <%= name %>.find(function(err, <%= name %>s) { + <%= name %>.find(function(err, <%= pluralName %>) { if (err) { return handleError(res, err); } - return res.status(200).json(<%= name %>s); + return res.status(200).json(<%= pluralName %>); }); }; diff --git a/endpoint/templates/name.model.js b/endpoint/templates/name.model.js index f8b043626..6c7a7d75e 100644 --- a/endpoint/templates/name.model.js +++ b/endpoint/templates/name.model.js @@ -8,7 +8,7 @@ var mongooseTimestamp = require('mongoose-timestamp'); // Model dependencies // Schema -var schema = new mongoose.Schema(require('./../../../shared/models/<%= name%>s.json')); +var schema = new mongoose.Schema(require('./../../../shared/models/<%= pluralName%>.json')); // Schema plugins schema.plugin(mongooseDelete, { diff --git a/endpoint/templates/name.spec.js b/endpoint/templates/name.spec.js index 3dc87b88b..40607db2d 100644 --- a/endpoint/templates/name.spec.js +++ b/endpoint/templates/name.spec.js @@ -6,7 +6,7 @@ var request = require('supertest'); var test = require('../../test'); var <%= name %> = require('./<%= name %>.model'); -describe('<%= name %>s endpoint', function() { +describe('<%= pluralName %> endpoint', function() { var id = 0; var token = ''; @@ -26,7 +26,7 @@ describe('<%= name %>s endpoint', function() { it('should respond with an empty JSON array', function(done) { request(app) - .get('/api/<%= name%>s' + token) + .get('/api/<%= pluralName%>' + token) .expect(200) .expect('Content-Type', /json/) .end(function(err, res) { @@ -39,7 +39,7 @@ describe('<%= name %>s endpoint', function() { it('should create a new <%= name %>', function(done) { request(app) - .post('/api/<%= name%>s' + token) + .post('/api/<%= pluralName%>' + token) .send({ string: 'Test string', age: 20, @@ -79,7 +79,7 @@ describe('<%= name %>s endpoint', function() { it('should update a <%= name %>', function(done) { request(app) - .put('/api/<%= name%>s/' + id + token) + .put('/api/<%= pluralName%>/' + id + token) .send({ string: 'Test string updated', age: 22, @@ -106,7 +106,7 @@ describe('<%= name %>s endpoint', function() { it('should get a <%= name %>', function(done) { request(app) - .get('/api/<%= name%>s/' + id + token) + .get('/api/<%= pluralName%>/' + id + token) .expect(200) .expect('Content-Type', /json/) .end(function(err, res) { @@ -125,7 +125,7 @@ describe('<%= name %>s endpoint', function() { it('should delete a <%= name %>', function(done) { request(app) - .delete('/api/<%= name%>s/' + id + token) + .delete('/api/<%= pluralName%>/' + id + token) .expect(204) .end(function(err, res) { if (err) {return done(err);} diff --git a/endpoint/templatesJson/names.json b/endpoint/templatesJson/pluralName.json similarity index 100% rename from endpoint/templatesJson/names.json rename to endpoint/templatesJson/pluralName.json diff --git a/util.js b/util.js index 06eb092d5..ad1fb81c4 100644 --- a/util.js +++ b/util.js @@ -134,6 +134,9 @@ function processDirectory (self, source, destination) { if(self.name) { filteredFile.name = filteredFile.name.replace('name', self.name); } + if(self.pluralName) { + filteredFile.name = filteredFile.name.replace('pluralName', self.pluralName); + } var name = filteredFile.name; var copy = false, stripped;