mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
refactoring
This commit is contained in:
@@ -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