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, type: String,
required: true, required: true,
}, },
slug: {
type: String,
required: true,
},
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }], 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", "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz",
"integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" "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": { "snapdragon": {
"version": "0.8.2", "version": "0.8.2",
"resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz", "resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz",

View File

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

View File

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