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);
|
||||
|
||||
@@ -7,11 +7,11 @@ const TodoListSchema = Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
lowercase: true,
|
||||
},
|
||||
slug: {
|
||||
type: String,
|
||||
required: true,
|
||||
lowercase: true,
|
||||
},
|
||||
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
|
||||
});
|
||||
@@ -27,11 +27,10 @@ TodoListSchema.methods.slugify = function () {
|
||||
this.slug = slugify(this.name);
|
||||
};
|
||||
|
||||
TodoListSchema.methods.removeWithTodos = function () {
|
||||
this.todos.forEach((todo) => {
|
||||
todo.remove();
|
||||
TodoListSchema.pre('remove', async function () {
|
||||
this.todos.forEach(async (todo) => {
|
||||
await todo.remove();
|
||||
});
|
||||
this.remove();
|
||||
};
|
||||
});
|
||||
|
||||
mongoose.model('TodoList', TodoListSchema);
|
||||
|
||||
Reference in New Issue
Block a user