use list id for routing

This commit is contained in:
2018-05-27 15:07:50 +03:00
parent 6598874331
commit 00f71bb78d
5 changed files with 1388 additions and 6546 deletions

View File

@@ -1,5 +1,4 @@
const mongoose = require('mongoose');
const slugify = require('slugify');
const { Schema } = mongoose;
@@ -8,25 +7,9 @@ const TodoListSchema = Schema({
type: String,
required: true,
},
slug: {
type: String,
required: true,
lowercase: true,
},
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
});
TodoListSchema.pre('validate', function (next) {
if (!this.slug) {
this.slugify();
}
next();
});
TodoListSchema.methods.slugify = function () {
this.slug = slugify(this.name);
};
TodoListSchema.pre('remove', async function () {
this.todos.forEach(async (todo) => {
await todo.remove();
@@ -38,7 +21,6 @@ TodoListSchema.methods.toJson = function () {
return {
id: this._id.toString(),
name: this.name,
slug: this.slug,
todos,
};
};