Created
November 21, 2014 23:20
-
-
Save owenkellogg/5da0c3da4f36396eba51 to your computer and use it in GitHub Desktop.
restify sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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