mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 07:37:49 +01:00
use list id for routing
This commit is contained in:
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
7873
package-lock.json
generated
7873
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -17,9 +17,7 @@
|
||||
"express": "^4.16.3",
|
||||
"method-override": "^2.3.10",
|
||||
"mongoose": "^5.1.1",
|
||||
"morgan": "^1.9.0",
|
||||
"npm": "^6.0.1",
|
||||
"slugify": "^1.3.0"
|
||||
"morgan": "^1.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^4.19.1",
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
@@ -7,8 +7,6 @@ const TodoList = mongoose.model('TodoList');
|
||||
|
||||
const asyncHelper = require('../asyncHelper');
|
||||
|
||||
const listIdMiddleware = require('./listIdMiddleware');
|
||||
|
||||
// index
|
||||
router.get(
|
||||
'/',
|
||||
@@ -33,10 +31,9 @@ router.post(
|
||||
|
||||
// delete
|
||||
router.delete(
|
||||
'/:slug',
|
||||
listIdMiddleware,
|
||||
'/:listId',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { listId } = res.locals;
|
||||
const { listId } = req.params;
|
||||
const list = await TodoList.findById(listId)
|
||||
.populate('todos')
|
||||
.exec();
|
||||
@@ -47,10 +44,9 @@ router.delete(
|
||||
|
||||
// update
|
||||
router.patch(
|
||||
'/:slug',
|
||||
listIdMiddleware,
|
||||
'/:listId',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { listId } = res.locals;
|
||||
const { listId } = req.params;
|
||||
const { name } = req.body;
|
||||
const patch = {};
|
||||
if (name !== undefined) {
|
||||
@@ -61,11 +57,17 @@ router.patch(
|
||||
{ $set: patch },
|
||||
{ new: true },
|
||||
).exec();
|
||||
await list.slugify();
|
||||
await list.save();
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user