Created
December 8, 2013 20:02
-
-
Save jacekd/7863147 to your computer and use it in GitHub Desktop.
basic REST API
This file contains hidden or 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
mongoose = require 'mongoose' | |
model = require '../models/mongo' | |
Mongo = mongoose.model('Mongo', model.mongoSchema) | |
exports.list = (req, res) -> | |
Mongo.find (err, object) -> | |
return res.json 500, { status: '500', message: 'Intestnal Exception Error' } if (err) | |
res.json 200, { status: '200', message: object } | |
exports.create = (req, res) -> | |
newMongo = new Mongo req.name, req.msg | |
newMongo.save (err) -> | |
return res.json 500, { status: '500', message: 'Intestnal Exception Error' } if (err) | |
res.json 201, { status: '201', message: newMongo } | |
exports.getOne = (req, res) -> | |
Mongo.find { _id: req.params.id }, (err, object) -> | |
return res.json 404, { status: '404', message: 'Object not found' } if (err) | |
res.json 200, { status: '201', message: object } | |
exports.update = (req, res) -> | |
Mongo.find { _id: req.params.id }, (err, object) -> | |
return res.json 404, { status: '404', message: 'Object not found' } if (err) | |
object.args = req.args for args in object | |
object.save (err) -> | |
return res.json '304' { status: '304', message: 'Object not modified' } | |
res.json 200, { status: '200', message: object } | |
exports.destroy = (req, res) -> | |
Mongo.find { _id: req.params.id }, (err, object) -> | |
return res.json 404, { status: '404', message: 'Object not found' } if (err) | |
object.remove (err) -> | |
return res.json 500, { status: '500', message: 'Intestnal Exception Error' } if (err) | |
res.json 410 { status: '410', message: 'Object Removed' } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment