remove lists

This commit is contained in:
2018-05-17 12:13:14 +03:00
parent 5486b4582f
commit 8255ec05b9
4 changed files with 43 additions and 9 deletions

View File

@@ -3,6 +3,8 @@ const mongoose = require('mongoose');
const router = express.Router();
const { NotFoundError } = require('../errors');
const TodoList = mongoose.model('TodoList');
const asyncHelper = require('../asyncHelper');
@@ -29,4 +31,21 @@ router.post(
}),
);
router.delete(
'/:id',
asyncHelper(async (req, res) => {
const { id } = req.params;
const list = await TodoList.findById(id)
.populate('todos')
.exec();
if (!list) {
throw new NotFoundError();
}
list.todos.forEach((todo) => {
todo.remove();
});
list.remove();
res.json({ success: true });
}),
);
module.exports = router;