2013-04-09 18:33:29 +00:00
|
|
|
/**
|
|
|
|
* Expose `Collection`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-04-10 17:55:13 +00:00
|
|
|
var Resource = require('resource')
|
2013-04-09 18:33:29 +00:00
|
|
|
, debug = require('debug')('collection')
|
|
|
|
, util = require('util')
|
|
|
|
, inherits = util.inherits
|
|
|
|
, assert = require('assert');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new `Collection` with the given `options`.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Collection}
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Collection(options) {
|
|
|
|
Resource.apply(this, arguments);
|
|
|
|
|
|
|
|
this.options = options;
|
|
|
|
|
2013-04-10 17:55:13 +00:00
|
|
|
// model
|
|
|
|
this.model = this.dependencies.model.schema;
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
// setup http routes
|
2013-04-10 17:55:13 +00:00
|
|
|
this.setupRoutes(this.app);
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
debug('created with options', options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit from `Resource`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inherits(Collection, Resource);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Collection.dependencies = {
|
2013-04-10 17:55:13 +00:00
|
|
|
'model': 'model'
|
2013-04-09 18:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Collection.prototype.setupRoutes = function (app) {
|
2013-04-10 17:55:13 +00:00
|
|
|
var Model = this.model;
|
|
|
|
var ctx;
|
|
|
|
var collection = this;
|
|
|
|
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
ctx = collection.createContext(req, res, next);
|
|
|
|
|
|
|
|
ctx.emit('request', function () {
|
|
|
|
ctx.emit(req.method.toLowerCase(), function () {
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
// create
|
|
|
|
app.post('/', create);
|
|
|
|
app.post('/new', create);
|
|
|
|
|
|
|
|
function create(req, res, next) {
|
2013-04-10 17:55:13 +00:00
|
|
|
ctx.emit('create', function (done) {
|
|
|
|
Model.create(req.body, done);
|
2013-04-09 18:33:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// read
|
|
|
|
app.get('/:id', query);
|
|
|
|
app.get('/', query);
|
|
|
|
|
|
|
|
function query(req, res, next) {
|
|
|
|
var query = req.query;
|
|
|
|
query.where = query.where || {};
|
|
|
|
|
|
|
|
if(req.param('id')) {
|
|
|
|
query.where.id = req.param('id');
|
|
|
|
}
|
|
|
|
if(query.where.id) {
|
|
|
|
query.limit = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-10 17:55:13 +00:00
|
|
|
if(query.limit) query.limit = Number(query.limit);
|
|
|
|
if(query.skip) query.skip = Number(query.skip);
|
|
|
|
|
|
|
|
ctx.emit('query', query, function (done) {
|
|
|
|
Model.all(query, done);
|
2013-04-09 18:33:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
app.put('/:id', function (req, res) {
|
2013-04-10 17:55:13 +00:00
|
|
|
var body = req.body || {};
|
|
|
|
var id = req.param('id');
|
|
|
|
|
|
|
|
if(Number(id) == id) {
|
|
|
|
id = Number(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.emit('find', function () {
|
|
|
|
Model.find(id, function (err, m) {
|
|
|
|
if(err) {
|
|
|
|
ctx.done(err);
|
|
|
|
} else {
|
|
|
|
ctx.emit('validate', body, function () {
|
|
|
|
ctx.emit('update', body, function (done) {
|
|
|
|
m.updateAttributes(body, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-04-09 18:33:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// delete
|
2013-04-10 17:55:13 +00:00
|
|
|
app.del('/:id', function () {
|
2013-04-09 18:33:29 +00:00
|
|
|
var id = req.param('id');
|
|
|
|
|
2013-04-10 17:55:13 +00:00
|
|
|
ctx.emit('delete', id, function (done) {
|
|
|
|
Model.destroy(id, done);
|
2013-04-09 18:33:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|