mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
refactoring, fix todo removal from lists
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
class NotFoundError extends Error {
|
class NotFoundError extends Error {
|
||||||
constructor(...args) {
|
constructor(text, ...args) {
|
||||||
super(...args);
|
super(...args);
|
||||||
Error.captureStackTrace(this, NotFoundError);
|
Error.captureStackTrace(this, NotFoundError);
|
||||||
this.name = 'NotFound';
|
this.name = 'NotFound';
|
||||||
|
this.text = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BadRequestError extends Error {
|
class BadRequestError extends Error {
|
||||||
constructor(...args) {
|
constructor(text, ...args) {
|
||||||
super(...args);
|
super(...args);
|
||||||
Error.captureStackTrace(this, NotFoundError);
|
Error.captureStackTrace(this, NotFoundError);
|
||||||
this.name = 'BadRequest';
|
this.name = 'BadRequest';
|
||||||
|
this.text = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,22 @@ const TodoSchema = Schema({
|
|||||||
|
|
||||||
TodoSchema.pre('save', async function () {
|
TodoSchema.pre('save', async function () {
|
||||||
const list = await TodoList.findById(this.list);
|
const list = await TodoList.findById(this.list);
|
||||||
list.todos.push(this.id);
|
list.todos.push(this._id);
|
||||||
await list.save();
|
await list.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
TodoSchema.pre('remove', async function () {
|
TodoSchema.pre('remove', async function () {
|
||||||
const list = await TodoList.findById(this.list);
|
const list = await TodoList.findById(this.list);
|
||||||
list.todos.remove(this.id);
|
list.todos.splice(list.todos.indexOf(this._id), 1);
|
||||||
await list.save();
|
await list.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
TodoSchema.methods.toJson = function () {
|
||||||
|
return {
|
||||||
|
id: this._id,
|
||||||
|
text: this.text,
|
||||||
|
list: this.list,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
mongoose.model('Todo', TodoSchema);
|
mongoose.model('Todo', TodoSchema);
|
||||||
|
|||||||
@@ -33,4 +33,14 @@ TodoListSchema.pre('remove', async function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
TodoListSchema.methods.toJson = function () {
|
||||||
|
const todos = this.populated('todos') ? this.todos.map(todo => todo.toJson()) : this.todos;
|
||||||
|
return {
|
||||||
|
id: this._id,
|
||||||
|
name: this.name,
|
||||||
|
slug: this.slug,
|
||||||
|
todos,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
mongoose.model('TodoList', TodoListSchema);
|
mongoose.model('TodoList', TodoListSchema);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ module.exports = asyncHelper(async (req, res, next) => {
|
|||||||
const { slug } = req.params;
|
const { slug } = req.params;
|
||||||
const list = await TodoList.findOne({ slug }).exec();
|
const list = await TodoList.findOne({ slug }).exec();
|
||||||
if (!list) {
|
if (!list) {
|
||||||
throw new NotFoundError();
|
throw new NotFoundError('cant find list');
|
||||||
}
|
}
|
||||||
res.locals.listId = list._id;
|
res.locals.listId = list._id;
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ router.get(
|
|||||||
const lists = await TodoList.find({})
|
const lists = await TodoList.find({})
|
||||||
.populate('todos')
|
.populate('todos')
|
||||||
.exec();
|
.exec();
|
||||||
res.json(lists);
|
res.json({ success: true, data: lists.map(list => list.toJson()) });
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,14 @@ const Todo = mongoose.model('Todo');
|
|||||||
|
|
||||||
const asyncHelper = require('../asyncHelper');
|
const asyncHelper = require('../asyncHelper');
|
||||||
|
|
||||||
|
const { NotFoundError } = require('../errors');
|
||||||
// index
|
// index
|
||||||
router.get(
|
router.get(
|
||||||
'/',
|
'/',
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { listId } = res.locals;
|
const { listId } = res.locals;
|
||||||
const todos = await Todo.find({ list: listId }).exec();
|
const todos = await Todo.find({ list: listId }).exec();
|
||||||
res.json(todos);
|
res.json({ success: true, data: todos.map(todo => todo.toJson()) });
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -52,7 +53,11 @@ router.delete(
|
|||||||
'/:todoId',
|
'/:todoId',
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { todoId } = req.params;
|
const { todoId } = req.params;
|
||||||
await Todo.findByIdAndRemove(todoId).exec();
|
const todo = await Todo.findById(todoId).exec();
|
||||||
|
if (!todo) {
|
||||||
|
throw new NotFoundError('cant find todo');
|
||||||
|
}
|
||||||
|
await todo.remove();
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
22
tests/integration/lists.test.js
Normal file
22
tests/integration/lists.test.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
const server = require('../../app.js');
|
||||||
|
|
||||||
|
const request = require('supertest');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await mongoose.connection.dropDatabase();
|
||||||
|
await mongoose.disconnect();
|
||||||
|
await server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('test lists', () => {
|
||||||
|
test('index lists', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get('/lists')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBe(true);
|
||||||
|
expect(response.body.data).toBeInstanceOf(Array);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user