clean up,

tests,
fixes
This commit is contained in:
2018-06-01 01:06:00 +03:00
parent db46f1a2b4
commit 2f48681944
11 changed files with 277 additions and 71 deletions

View File

@@ -13,13 +13,15 @@ const TodoSchema = Schema({
});
TodoSchema.pre('save', async function () {
const user = await this.model('User').findById(this.user);
user.todos.push(this._id);
await user.save();
if (this.isNew) {
const user = await this.model('User').findById(this.user);
user.todos.push(this._id);
await user.save();
const list = await this.model('TodoList').findById(this.list);
list.todos.push(this._id);
await list.save();
const list = await this.model('TodoList').findById(this.list);
list.todos.push(this._id);
await list.save();
}
});
TodoSchema.pre('remove', async function () {

View File

@@ -12,16 +12,22 @@ const TodoListSchema = Schema({
});
TodoListSchema.pre('save', async function () {
const user = await this.model('User').findById(this.user);
user.lists.push(this._id);
await user.save();
if (this.isNew) {
const user = await this.model('User').findById(this.user);
user.lists.push(this._id);
await user.save();
}
});
TodoListSchema.pre('remove', async function () {
const user = await this.model('User').findById(this.user);
user.lists.splice(user.todos.indexOf(this._id), 1);
user.lists.splice(user.lists.indexOf(this._id), 1);
await user.save();
await this.model('Todo').remove({ list: this._id });
const todos = await this.model('Todo')
.find({ list: this._id })
.exec();
await Promise.all(todos.map(todo => todo.remove()));
});
TodoListSchema.methods.toJson = function () {

View File

@@ -22,8 +22,15 @@ UserSchema.plugin(passportLocalMongoose);
UserSchema.plugin(uniqueValidator);
UserSchema.pre('remove', async function () {
await this.model('TodoList').remove({ user: this._id });
await this.model('Todo').remove({ user: this._id });
await this.model('TodoList')
.find({ user: this._id })
.remove()
.exec();
await this.model('Todo')
.find({ user: this._id })
.remove()
.exec();
});
UserSchema.methods.generateJwt = function () {