use slugs for lists

This commit is contained in:
2018-05-17 12:28:03 +03:00
parent 8255ec05b9
commit 1317015a0d
4 changed files with 16 additions and 4 deletions

View File

@@ -7,6 +7,10 @@ const todoListSchema = Schema({
type: String,
required: true,
},
slug: {
type: String,
required: true,
},
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
});

5
package-lock.json generated
View File

@@ -9709,6 +9709,11 @@
"resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz",
"integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E="
},
"slugify": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.3.0.tgz",
"integrity": "sha512-vvz+9ANt7CtdTHwJpfrsHOnGkgxky+CUPnvtzDZBZYFo/H/CdZkd5lJL7z7RqtH/x9QW/ItYYfHlcGf38CBK1w=="
},
"snapdragon": {
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz",

View File

@@ -18,7 +18,8 @@
"method-override": "^2.3.10",
"mongoose": "^5.1.1",
"morgan": "^1.9.0",
"npm": "^6.0.1"
"npm": "^6.0.1",
"slugify": "^1.3.0"
},
"devDependencies": {
"eslint": "^4.19.1",

View File

@@ -1,5 +1,6 @@
const express = require('express');
const mongoose = require('mongoose');
const slugify = require('slugify');
const router = express.Router();
@@ -26,16 +27,17 @@ router.post(
asyncHelper(async (req, res) => {
const { name } = req.body;
const newList = new TodoList({ name });
newList.slug = slugify(name);
await newList.save();
res.json({ success: true });
}),
);
router.delete(
'/:id',
'/:slug',
asyncHelper(async (req, res) => {
const { id } = req.params;
const list = await TodoList.findById(id)
const { slug } = req.params;
const list = await TodoList.findOne({ slug })
.populate('todos')
.exec();
if (!list) {