clean up,

tests,
fixes
This commit is contained in:
2018-06-01 01:06:00 +03:00
parent db46f1a2b4
commit 2f48681944
11 changed files with 277 additions and 71 deletions

View File

@@ -6,7 +6,6 @@ const router = express.Router();
const TodoList = mongoose.model('TodoList');
const asyncHelper = require('../asyncHelper');
const auth = require('./auth');
const { NotFoundError } = require('../errors');
// index
@@ -36,9 +35,7 @@ router.delete(
'/:listId',
asyncHelper(async (req, res) => {
const { listId } = req.params;
const list = await TodoList.find({ _id: listId, user: req.user.id })
.populate('todos')
.exec();
const list = await TodoList.findOne({ _id: listId, user: req.user.id }).exec();
await list.remove();
res.json({ success: true });
}),
@@ -50,7 +47,7 @@ router.patch(
asyncHelper(async (req, res) => {
const { listId } = req.params;
const { name } = req.body;
const list = await TodoList.find({ _id: listId, user: req.user.id });
const list = await TodoList.findOne({ _id: listId, user: req.user.id });
if (!list) {
throw new NotFoundError("can't find list");
}

View File

@@ -36,7 +36,7 @@ router.patch(
asyncHelper(async (req, res) => {
const { todoId } = req.params;
const { text, completed } = req.body;
const todo = await Todo.find({ _id: todoId, user: req.user.id });
const todo = await Todo.findOne({ _id: todoId, user: req.user.id });
if (!todo) {
throw new NotFoundError("can't find todo");
}
@@ -46,6 +46,7 @@ router.patch(
if (completed !== undefined) {
todo.completed = completed;
}
await todo.save();
res.json({ success: true, data: todo.toJson() });
}),
);
@@ -55,7 +56,7 @@ router.delete(
'/:todoId',
asyncHelper(async (req, res) => {
const { todoId } = req.params;
const todo = await Todo.find({ _id: todoId, user: req.user.id }).exec();
const todo = await Todo.findOne({ _id: todoId, user: req.user.id }).exec();
if (!todo) {
throw new NotFoundError(`can't find todo with id ${todoId}`);
}

View File

@@ -51,7 +51,8 @@ router.delete(
'/user',
auth.required,
asyncHelper(async (req, res) => {
await User.findByIdAndRemove(req.user.id).exec();
const user = await User.findById(req.user.id).exec();
await user.remove();
res.json({ success: true });
}),
);