mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-29 08:07:48 +01:00
refactoring
This commit is contained in:
17
routes/listIdMiddleware.js
Normal file
17
routes/listIdMiddleware.js
Normal file
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
||||
res.locals.listId = list._id;
|
||||
next();
|
||||
});
|
||||
@@ -9,6 +9,8 @@ const TodoList = mongoose.model('TodoList');
|
||||
|
||||
const asyncHelper = require('../asyncHelper');
|
||||
|
||||
const listIdMiddleware = require('./listIdMiddleware');
|
||||
|
||||
// index
|
||||
router.get(
|
||||
'/',
|
||||
@@ -34,16 +36,17 @@ router.post(
|
||||
// delete
|
||||
router.delete(
|
||||
'/:slug',
|
||||
listIdMiddleware,
|
||||
asyncHelper(async (req, res) => {
|
||||
const { slug } = req.params;
|
||||
const list = await TodoList.findOne({ slug })
|
||||
const { listId } = res.locals;
|
||||
const list = await TodoList.findById(listId)
|
||||
.populate('todos')
|
||||
.exec();
|
||||
if (!list) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
await list.removeWithTodos();
|
||||
await list.remove();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
|
||||
router.use('/:slug/todos', listIdMiddleware, require('./todos'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -3,7 +3,6 @@ const mongoose = require('mongoose');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const TodoList = mongoose.model('TodoList');
|
||||
const Todo = mongoose.model('Todo');
|
||||
|
||||
const asyncHelper = require('../asyncHelper');
|
||||
@@ -12,7 +11,8 @@ const asyncHelper = require('../asyncHelper');
|
||||
router.get(
|
||||
'/',
|
||||
asyncHelper(async (req, res) => {
|
||||
const todos = await Todo.find({}).exec();
|
||||
const { listId } = res.locals;
|
||||
const todos = await Todo.find({ list: listId }).exec();
|
||||
res.json(todos);
|
||||
}),
|
||||
);
|
||||
@@ -21,12 +21,20 @@ router.get(
|
||||
router.post(
|
||||
'/',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { text, listId } = req.body;
|
||||
const list = await TodoList.findById(listId);
|
||||
const todo = new Todo({ text, list: list._id });
|
||||
const { listId } = res.locals;
|
||||
const { text } = req.body;
|
||||
const todo = new Todo({ text, list: listId });
|
||||
await todo.save();
|
||||
list.todos.push(todo.id);
|
||||
await list.save();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/:todoId',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { todoId } = req.params;
|
||||
console.log(todoId);
|
||||
await Todo.findByIdAndRemove(todoId).exec();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user