ozserver
Version:
API for OZ
126 lines (113 loc) • 3.05 kB
JavaScript
var Action, Actions, EventEmitter, exports, mongoose,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
mongoose = require('mongoose');
Action = mongoose.model('Action', require(global.home + '/script/views/db/action'));
Actions = (function(_super) {
__extends(Actions, _super);
function Actions(options) {
var _this = this;
this.options = options != null ? options : {};
this.on('get', function(req, res) {
if (req.route.params.aid != null) {
return _this.get(req.route.params.aid.toString(), function(err, data) {
if (err) {
return res.jsonp(err);
} else {
return res.jsonp(data);
}
});
} else {
return res.jsonp({});
}
});
}
Actions.prototype.set = function(aid, data, fn) {
var _this = this;
return Action.findOne({
aid: aid
}, function(err, action) {
if (err) {
if (fn) {
return fn && fn(err);
}
} else {
if (action != null) {
action.post_dt = new Date();
action.data = data;
return action.save(function() {
if (fn) {
return fn && fn(null, action.toJSON());
}
});
} else {
action = new Action({
aid: aid,
data: data
});
return action.save(function() {
if (fn) {
return fn && fn(null, action.toJSON());
}
});
}
}
});
};
Actions.prototype.get = function(aid, fn) {
return Action.findOne({
aid: aid
}, function(err, action) {
var json, res;
if (err) {
if (fn) {
return fn(err);
}
} else {
if (action != null) {
json = action.toJSON();
if (json.data != null) {
res = json.data;
} else {
res = {};
}
return action.remove(function() {
if (fn) {
return fn && fn(null, res);
}
});
} else {
if (fn) {
return fn && fn(null, null);
}
}
}
});
};
Actions.prototype.destroy = function(aid, fn) {
return Action.findOne({
aid: aid
}, function(err, action) {
if (err) {
if (fn) {
return fn && fn(err);
}
} else {
return action.remove(function() {
if (fn) {
return fn();
}
});
}
});
};
return Actions;
})(EventEmitter);
exports = module.exports = function(options) {
if (options == null) {
options = {};
}
return new Actions(options);
};
exports.Actions = Actions;