Skip to content

Instantly share code, notes, and snippets.

@kirayatail
Forked from bbraithwaite/categories.server.controller.js
Last active September 25, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kirayatail/b745f05a7b346b6c4f4d to your computer and use it in GitHub Desktop.
Save kirayatail/b745f05a7b346b6c4f4d to your computer and use it in GitHub Desktop.
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors.server.controller'),
Category = mongoose.model('Category'),
_ = require('lodash');
var responseFactory(res, status) {
if(!status) {
status = 200; // Default OK status, override by setting argument
}
// Manufacture the callback function that will be passed to mongoose
return function(err, content) {
if(err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err);
});
} else {
return res.status(status).json(content);
}
}
}
/**
* Create a Category
*/
exports.create = function(req, res) {
var category = new Category(req.body);
category.save(responseFactory(res, 201)); // Replace with callback from the factory
};
/**
* Show the current Category
*/
exports.read = function(req, res) {
res.json(req.category);
};
/**
* Update a Category
*/
exports.update = function(req, res) {
var category = req.category;
category = _.extend(category, req.body);
category.save(responseFactory(res));
};
/**
* Delete an Category
*/
exports.delete = function(req, res) {
var category = req.category;
category.remove(responseFactory(res));
};
/**
* List of Categories
*/
exports.list = function(req, res) {
Category.find().sort('name').exec(responseFactory(res));
};
/**
* Category middleware
*/
exports.categoryByID = function(req, res, next, id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).send({
message: 'Category is invalid'
});
}
Category.findById(id).exec(function(err, category) {
if (err) return next(err);
if (!category) {
return res.status(404).send({
message: 'Category not found'
});
}
req.category = category;
next();
});
};
@jimf2342
Copy link

You have a few errors here:

Running "jshint:all" (jshint) task

   app/controllers/categories.server.controller.gist.js
     11 |var responseFactory(res, status) {
                            ^ Missing semicolon.
     11 |var responseFactory(res, status) {
                                        ^ Expected an assignment or function call and instead saw an expression.
     11 |var responseFactory(res, status) {
                                         ^ Missing semicolon.
     13 |        status = 200; // Default OK status, override by setting argument
                 ^ Read only.
     19 |                message: errorHandler.getErrorMessage(err);
                                                                   ^ Expected '}' to match '{' from line 18 and instead saw ';'.
     20 |            });
                     ^ Expected ')' and instead saw '}'.
     20 |            });
                      ^ Missing semicolon.
     20 |            });
                      ^ Expected an identifier and instead saw ')'.
     20 |            });
                      ^ Expected an assignment or function call and instead saw an expression.
     24 |    }
              ^ Missing semicolon.
     11 |var responseFactory(res, status) {
                             ^ 'res' is not defined.
     18 |            return res.status(400).send({
                            ^ 'res' is not defined.
     22 |            return res.status(status).json(content);
                            ^ 'res' is not defined.

>> 13 errors in 49 files
Warning: Task "jshint:all" failed. Used --force, continuing.

Here is the fix for them: https://gist.github.com/jimf2342/fdba48e9b1df8e89278a.

Nice refactor.

@embarq
Copy link

embarq commented Sep 25, 2016

Very nice, dude ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment