mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
refactoring
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const TodoList = mongoose.model('TodoList');
|
||||
const { Schema } = mongoose;
|
||||
|
||||
const TodoSchema = Schema({
|
||||
@@ -11,4 +12,16 @@ const TodoSchema = Schema({
|
||||
completed: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
TodoSchema.pre('save', async function () {
|
||||
const list = await TodoList.findById(this.list);
|
||||
list.todos.push(this.id);
|
||||
await list.save();
|
||||
});
|
||||
|
||||
TodoSchema.pre('remove', async function () {
|
||||
const list = await TodoList.findById(this.list);
|
||||
list.todos.remove(this.id);
|
||||
await list.save();
|
||||
});
|
||||
|
||||
mongoose.model('Todo', TodoSchema);
|
||||
|
||||
Reference in New Issue
Block a user