ozserver
Version:
API for OZ
96 lines (83 loc) • 3.16 kB
JavaScript
var Edit, EventEmitter, Post, User, colors, 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; };
mongoose = require('mongoose');
colors = require('colors');
EventEmitter = require('events').EventEmitter;
User = mongoose.model('User', require(global.home + '/script/views/db/user'));
Post = mongoose.model('Post', require(global.home + '/script/views/db/post'));
Edit = (function(_super) {
__extends(Edit, _super);
function Edit(req, res) {
var _this = this;
this.req = req;
this.res = res;
this.auth = require(global.home + '/script/controllers/auth')(this.req, this.res);
this.signin = require(global.home + '/script/controllers/signin')(this.req, this.res);
this.on('send', function() {
return _this.res.jsonp(_this.mdl.model);
});
this.on('success', function() {
_this.mdl.success();
return _this.emit('send');
});
this.on('fail', function() {
_this.mdl.fail();
return _this.emit('send');
});
this.on('password', function() {
var model, _ref;
model = ((_ref = _this.req.query) != null ? _ref.model : void 0) != null ? JSON.parse(_this.req.query.model) : {};
_this.mdl = require(global.home + '/script/models/edit/password')(model);
if (_this.mdl.check() === true) {
return _this.auth.user(function(err, user) {
if ((err != null) || (user == null)) {
return _this.emit('fail');
} else {
user.key = _this.mdl.model.key_new;
_this.signin.updateSession(user);
user.save();
return _this.emit('success');
}
});
} else {
return _this.emit('send');
}
});
this.on('personal', function() {
var model, _ref;
model = ((_ref = _this.req.query) != null ? _ref.model : void 0) != null ? JSON.parse(_this.req.query.model) : {};
_this.mdl = require(global.home + '/script/models/edit/personal')(model);
if (_this.mdl.check() === true) {
return _this.auth.user(function(err, user) {
if ((err != null) || (user == null)) {
return _this.emit('fail');
} else {
user.firstname = _this.mdl.model.firstname_new;
user.lastname = _this.mdl.model.lastname_new;
user.save();
_this.editPosts();
return _this.emit('success');
}
});
} else {
return _this.emit('send');
}
});
}
Edit.prototype.editPosts = function() {
return Post.update({
'author.id': this.mdl.model.id
}, {
'author.firstname': this.mdl.model.firstname_new,
'author.lastname': this.mdl.model.lastname_new
}, {
multi: true
}).exec();
};
return Edit;
})(EventEmitter);
exports = module.exports = function(req, res) {
return new Edit(req, res);
};
exports.Edit = Edit;