Skip to content

Instantly share code, notes, and snippets.

@crueber
Last active August 29, 2015 14:00
Show Gist options
  • Save crueber/11158241 to your computer and use it in GitHub Desktop.
Save crueber/11158241 to your computer and use it in GitHub Desktop.
A simple and very generic controller for my RESTful-API node module
mongoose = require 'mongoose'
Model = mongoose.model 'Room'
single_model = 'room'
plural_model = 'rooms'
# exports.before_filters = []
# exports.after_filters = []
# exports.secure = (req, is_nested, is_secure_callback) -> is_secure_callback(null, true)
exports.finder = (req, ident, is_index, callback) ->
if is_index
Model.find({}, callback)
else
Model.findOne({_id: ident}, callback)
exports.index = (req, res, data) -> data null, req[plural_model]
exports.read = (req, res, data) -> data null, req[single_model]
exports.create = (req, res, data) -> Model.create req.body, data
exports.delete = (req, res, data) -> Model.delete { _id: req[single_model]._id }, data
exports.update = (req, res, data) ->
req[single_model].set req.body
req[single_model].save (err) -> data(err, req[single_model])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment