Create a gist now

Instantly share code, notes, and snippets.

Embed
restify sequelize
function RestifyController(model, name) {
this.model = model;
this.name = name
}
RestifyController.prototype.respondError = function(error, code) {
}
RestifyController.prototype.respond = function(value) {
}
RestifyController.prototype.create = function(req, res, next) {
var _this = this;
_this.model.create(req.body)
.then(function(instance) {
_this.respond(instance);
})
.catch(_this.respondError);
}
RestifyController.prototype.index = function(req, res, next) {
var _this = this;
_this.model.create(req.body)
.then(function(instances) {
_this.respond(instances);
})
.catch(_this.respondError);
}
RestifyController.prototype.show = function(req, res, next) {
var _this = this;
_this.model.find(req.params.id)
.then(function(instance) {
_this.respond(instance);
})
.catch(_this.respondError);
}
RestifyController.prototype.update = function(req, res, next) {
var _this = this;
_this.model.find(req.body)
.then(function(instance) {
if (instance) {
delete req.body.id;
return _this.updateAttributes(req.body)
.then(_this.respond);
} else {
_this.respondError(instance, 404);
}
_this.respond(instance);
})
.catch(_this.respondError);
}
RestifyController.prototype.destroy = function(req, res, next) {
var _this = this;
_this.model.find(req.params.id)
.then(function(instance) {
if (instance) {
return _this.instance.destroy().then(respond(null, 200));
} else {
_this.respondError(null, 404);
}
})
.catch(_this.respondError);
}
function Restify(model, name) {
var controller = RestifyController(model, name);
var router = new express.Router();
router.post('/', controller.create)
router.get('/', controller.index)
router.get('/:id', controller.show)
router.put('/:id', controller.update)
router.delete('/:id', controller.destroy)
return new express.Router();
}
app.use('/bots', new Restify(Bot, 'bots'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment