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

7873
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -17,9 +17,7 @@
"express": "^4.16.3", "express": "^4.16.3",
"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",
"slugify": "^1.3.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^4.19.1", "eslint": "^4.19.1",

View File

@@ -1,17 +0,0 @@
const mongoose = require('mongoose');
const asyncHelper = require('../asyncHelper');
const TodoList = mongoose.model('TodoList');
const { NotFoundError } = require('../errors');
// listId middleware
module.exports = asyncHelper(async (req, res, next) => {
const { slug } = req.params;
const list = await TodoList.findOne({ slug }).exec();
if (!list) {
throw new NotFoundError('cant find list');
}
res.locals.listId = list._id;
next();
});

View File

@@ -7,8 +7,6 @@ const TodoList = mongoose.model('TodoList');
const asyncHelper = require('../asyncHelper'); const asyncHelper = require('../asyncHelper');
const listIdMiddleware = require('./listIdMiddleware');
// index // index
router.get( router.get(
'/', '/',
@@ -33,10 +31,9 @@ router.post(
// delete // delete
router.delete( router.delete(
'/:slug', '/:listId',
listIdMiddleware,
asyncHelper(async (req, res) => { asyncHelper(async (req, res) => {
const { listId } = res.locals; const { listId } = req.params;
const list = await TodoList.findById(listId) const list = await TodoList.findById(listId)
.populate('todos') .populate('todos')
.exec(); .exec();
@@ -47,10 +44,9 @@ router.delete(
// update // update
router.patch( router.patch(
'/:slug', '/:listId',
listIdMiddleware,
asyncHelper(async (req, res) => { asyncHelper(async (req, res) => {
const { listId } = res.locals; const { listId } = req.params;
const { name } = req.body; const { name } = req.body;
const patch = {}; const patch = {};
if (name !== undefined) { if (name !== undefined) {
@@ -61,11 +57,17 @@ router.patch(
{ $set: patch }, { $set: patch },
{ new: true }, { new: true },
).exec(); ).exec();
await list.slugify();
await list.save(); await list.save();
res.json({ success: true, data: list.toJson() }); res.json({ success: true, data: list.toJson() });
}), }),
); );
router.use('/:slug/todos', listIdMiddleware, require('./todos')); router.use(
'/:listId/todos',
(req, res, next) => {
res.locals.listId = req.params.listId;
next();
},
require('./todos'),
);
module.exports = router; module.exports = router;