mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
remove lists
This commit is contained in:
18
app.js
18
app.js
@@ -44,15 +44,21 @@ app.use((req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// handle errors
|
// handle errors
|
||||||
app.use((err, req, res, next) => {
|
app.use((error, req, res, next) => {
|
||||||
if (err.name === 'ValidationError') {
|
switch (error.name) {
|
||||||
|
case 'ValidationError':
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.json({ success: false, error: err });
|
res.json({ success: false, error });
|
||||||
} else {
|
break;
|
||||||
|
case 'NotFound':
|
||||||
|
res.status(404);
|
||||||
|
res.json({ success: false, error });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
res.status(500);
|
res.status(500);
|
||||||
res.json({ success: false, error: err });
|
res.json({ success: false, error });
|
||||||
}
|
}
|
||||||
next(err);
|
next(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
|
|||||||
9
errors/index.js
Normal file
9
errors/index.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
class NotFoundError extends Error {
|
||||||
|
constructor(...args) {
|
||||||
|
super(...args);
|
||||||
|
Error.captureStackTrace(this, NotFoundError);
|
||||||
|
this.name = 'NotFound';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { NotFoundError };
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
"main": "app.js",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./app.js",
|
"start": "node ./app.js",
|
||||||
"start-mon": "npx nodemon --inspect ./app.js",
|
"debug": "npx nodemon --inspect ./app.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ const mongoose = require('mongoose');
|
|||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const { NotFoundError } = require('../errors');
|
||||||
|
|
||||||
const TodoList = mongoose.model('TodoList');
|
const TodoList = mongoose.model('TodoList');
|
||||||
|
|
||||||
const asyncHelper = require('../asyncHelper');
|
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;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user