refactoring

This commit is contained in:
2018-05-19 18:10:56 +03:00
parent c0af83f3f1
commit 8f0509b8f2
8 changed files with 668 additions and 21 deletions

View File

@@ -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 });
}),
);