mirror of https://gitee.com/bigwinds/arangodb
Generate controllers
This commit is contained in:
parent
4f4b4c59da
commit
597e61b928
|
@ -35,6 +35,10 @@
|
||||||
_.each(this.models, function (model) {
|
_.each(this.models, function (model) {
|
||||||
fs.write(fs.join(this.folder, model.path), this.buildModel());
|
fs.write(fs.join(this.folder, model.path), this.buildModel());
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
_.each(this.controllers, function (controller) {
|
||||||
|
fs.write(fs.join(this.folder, controller.path), this.buildController(controller));
|
||||||
|
}, this);
|
||||||
},
|
},
|
||||||
|
|
||||||
template: function(name) {
|
template: function(name) {
|
||||||
|
@ -48,18 +52,27 @@
|
||||||
this.models = [];
|
this.models = [];
|
||||||
|
|
||||||
_.each(collectionNames, function (collectionName) {
|
_.each(collectionNames, function (collectionName) {
|
||||||
var modelName = i.singularize(collectionName);
|
var modelName = i.singularize(collectionName),
|
||||||
|
repositoryPath = 'repositories/' + collectionName + '.js',
|
||||||
|
modelPath = 'models/' + modelName + '.js';
|
||||||
|
|
||||||
this.collectionNames.push(collectionName);
|
this.collectionNames.push(collectionName);
|
||||||
this.controllers.push({
|
this.controllers.push({
|
||||||
url: '/' + collectionName,
|
url: '/' + collectionName,
|
||||||
path: 'controllers/' + collectionName + '.js'
|
path: 'controllers/' + collectionName + '.js',
|
||||||
|
repositoryInstance: collectionName,
|
||||||
|
repository: i.titleize(collectionName),
|
||||||
|
repositoryPath: repositoryPath,
|
||||||
|
model: i.titleize(modelName),
|
||||||
|
modelPath: modelPath,
|
||||||
|
basePath: collectionName,
|
||||||
|
collectionName: collectionName
|
||||||
});
|
});
|
||||||
this.repositories.push({
|
this.repositories.push({
|
||||||
path: 'repositories/' + collectionName + '.js'
|
path: repositoryPath
|
||||||
});
|
});
|
||||||
this.models.push({
|
this.models.push({
|
||||||
path: 'models/' + modelName + '.js'
|
path: modelPath
|
||||||
});
|
});
|
||||||
}, this);
|
}, this);
|
||||||
},
|
},
|
||||||
|
@ -76,6 +89,13 @@
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
buildController: function(controller) {
|
||||||
|
var manifest = this.template("controller.js.tmpl");
|
||||||
|
|
||||||
|
return manifest(controller);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
buildRepository: function() {
|
buildRepository: function() {
|
||||||
var manifest = this.template("repository.js.tmpl");
|
var manifest = this.template("repository.js.tmpl");
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
var Foxx = require('org/arangodb/foxx'),
|
||||||
|
ArangoError = require('org/arangodb').ArangoError,
|
||||||
|
<%= repository %> = require('<%= repositoryPath %>').Repository,
|
||||||
|
<%= model %> = require('<%= modelPath %>').Model,
|
||||||
|
_ = require('underscore'),
|
||||||
|
controller,
|
||||||
|
todos;
|
||||||
|
|
||||||
|
controller = new Foxx.Controller(applicationContext);
|
||||||
|
|
||||||
|
<%= repositoryInstance %> = new <%= repository %>(controller.collection('<%= collectionName %>'), {
|
||||||
|
model: <%= model %>
|
||||||
|
});
|
||||||
|
|
||||||
|
/** Lists of all <%= model %>
|
||||||
|
*
|
||||||
|
* This function simply returns the list of all <%= model %>.
|
||||||
|
*/
|
||||||
|
controller.get('/<%= basePath %>', function (req, res) {
|
||||||
|
res.json(_.map(<%= repositoryInstance %>.all(), function (model) {
|
||||||
|
return model.forClient();
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
/** Creates a new <%= model %>
|
||||||
|
*
|
||||||
|
* Creates a new <%= model %>-Item. The information has to be in the
|
||||||
|
* requestBody.
|
||||||
|
*/
|
||||||
|
controller.post('/<%= basePath %>', function (req, res) {
|
||||||
|
var todo = req.params('todo');
|
||||||
|
res.json(<%= repositoryInstance %>.save(todo).forClient());
|
||||||
|
}).bodyParam('todo', 'The <%= model %> you want to create', <%= model %>);
|
||||||
|
|
||||||
|
/** Updates a <%= model %>
|
||||||
|
*
|
||||||
|
* Changes a <%= model %>-Item. The information has to be in the
|
||||||
|
* requestBody.
|
||||||
|
*/
|
||||||
|
controller.put('/<%= basePath %>/:id', function (req, res) {
|
||||||
|
var id = req.params('id'),
|
||||||
|
todo = req.params('todo');
|
||||||
|
res.json(<%= repositoryInstance %>.replaceById(id, todo));
|
||||||
|
}).pathParam('id', {
|
||||||
|
description: 'The id of the <%= model %>-Item',
|
||||||
|
type: 'string'
|
||||||
|
}).bodyParam('todo', 'The <%= model %> you want your old one to be replaced with', <%= model %>);
|
||||||
|
|
||||||
|
/** Removes a <%= model %>
|
||||||
|
*
|
||||||
|
* Removes a <%= model %>-Item.
|
||||||
|
*/
|
||||||
|
controller.del('/<%= basePath %>/:id', function (req, res) {
|
||||||
|
var id = req.params('id');
|
||||||
|
<%= repositoryInstance %>.removeById(id);
|
||||||
|
res.json({ success: true });
|
||||||
|
}).pathParam('id', {
|
||||||
|
description: 'The ID of the <%= model %>-Item',
|
||||||
|
type: 'string'
|
||||||
|
}).errorResponse(ArangoError, 404, 'The document could not be found');
|
||||||
|
}());
|
||||||
|
|
Loading…
Reference in New Issue