Permalink
Browse files

Routes: restful refactoring route handling

  • Loading branch information...
1 parent c29adad commit d2f0c5ab81d4d1d101c59fa8357f1887706e9728 @goliatone committed Jul 19, 2014
Showing with 45 additions and 26 deletions.
  1. +45 −26 routes/restful.js
View
71 routes/restful.js
@@ -18,14 +18,27 @@ Template.precompileAction = function(action) {
Template[action] = Template.compile(tsrc);
};
Template.precompileAction('list');
+Template.precompileAction('result');
db.idAttribute = '_index';
db.resourcesPath = 'resources/';
-var routes = {};
+var Routes = {
+ messages: {
+ list: 'Resources',
+ read: 'Resource',
+ create: 'Resource created',
+ update: 'Resource updated',
+ destroy: 'Resource destroyed'
-routes.resultHandler = function(action, res, err, result) {
- var out = Template.list({
+ }
+};
+
+Routes.resultHandler = function(action, resource, res, err, result) {
+ var out = Template.result({
+ action: action,
+ resource: resource,
+ message: Routes.messages[action],
count: result.length || 1,
data: result
});
@@ -36,71 +49,77 @@ routes.resultHandler = function(action, res, err, result) {
// GET /:resource
-routes.list = function(req, res, next) {
+Routes.list = function(req, res, next) {
// var where = JSON.parse(req.param('where'));
// console.log(where);
// console.log('HERE')
- db(req.params.resource).find(routes.resultHandler.bind(routes, 'list', res));
+ var resource = req.params.resource;
+ db(resource).find(Routes.resultHandler.bind(null, 'list', resource, res));
};
// POST /:resource
-routes.create = function(req, res, next) {
+Routes.create = function(req, res, next) {
db(req.params.resource).insert(req.body, function(err, resource) {
res.jsonp(resource);
});
};
// GET /:resource/:id
-routes.read = function(req, res, next) {
+Routes.read = function(req, res, next) {
// res.send('read=> resource:' + req.params.resource + ' id: ' + req.params.id);
- db(req.params.resource).findOne(+req.params.id, routes.resultHandler.bind(routes, 'read', res));
+ var resource = req.params.resource,
+ id = +req.params.id;
+ db(resource).findOne(id, Routes.resultHandler.bind(null, 'read', resource, res));
};
// PUT /:resource/:id
// PATCH /:resource/:id
-routes.update = function(req, res, next) {
- db(req.params.resource).insert(req.body, function(err, resource) {
- res.jsonp(resource);
- });
+Routes.update = function(req, res, next) {
+ var resource = req.params.resource,
+ attributes = req.body;
+ db(resource).insert(attributes, Routes.resultHandler.bind(null, 'update', resource, res));
};
// DELETE /:resource/:id
-routes.destroy = function(req, res, next) {
- db(req.params.resource).remove(req.params.id, function(err, resource) {
- res.jsonp(resource);
- });
+Routes.destroy = function(req, res, next) {
+ var resource = req.params.resource,
+ id = +req.params.id;
+ db(resource).remove(id, Routes.resultHandler.bind(null, 'delete', resource, res));
};
// GET /resources
-routes.listResources = function(req, res) {
+Routes.listResources = function(req, res) {
var _resources = db.resourcesPath;
fs.readdir(_resources, function(err, files) {
if (err) throw err;
var out = {
- files: []
+ resources: []
};
files.filter(function(file) {
return fs.statSync(path.join(_resources, file)).isFile();
}).forEach(function(file) {
console.log("%s (%s)", file, path.extname(file));
- out.files.push(file.replace(path.extname(file), ''));
+ out.resources.push(file.replace(path.extname(file), ''));
});
res.jsonp(out);
});
};
-router.get('/:resource', routes.list);
-router.get('/:resource/:id', routes.read);
-router.get('/resources', routes.listResources);
-router.get('/:parent/:parentId/:resource', routes.list);
+router.get('/resources', Routes.listResources);
+
+router.get('/:resource', Routes.list);
+router.get('/:resource/:id', Routes.read);
+router.get('/:parent/:parentId/:resource', Routes.list);
+
+router.post('/:resource', Routes.create);
-router.post('/:resource', routes.create);
+router.put('/:resource/:id', Routes.update);
+router.patch('/:resource/:id', Routes.update);
-router.put('/:resource/:id', routes.update);
-router.patch('/:resource/:id', routes.update);
+router.delete('/:resource/:id', Routes.destroy);
//TODO: Make take config object to add a resource path
//so that we can namespace resources and we can take in

0 comments on commit d2f0c5a

Please sign in to comment.