mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
update
This commit is contained in:
4
app.js
4
app.js
@@ -37,6 +37,10 @@ app.use((error, req, res, next) => {
|
||||
res.status(404);
|
||||
res.json({ success: false, error });
|
||||
break;
|
||||
case 'BadRequestError':
|
||||
res.status(400);
|
||||
res.json({ success: false, error });
|
||||
break;
|
||||
default:
|
||||
res.status(500);
|
||||
res.json({ success: false, error });
|
||||
|
||||
@@ -6,4 +6,12 @@ class NotFoundError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { NotFoundError };
|
||||
class BadRequestError extends Error {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
Error.captureStackTrace(this, NotFoundError);
|
||||
this.name = 'BadRequest';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { NotFoundError, BadRequestError };
|
||||
|
||||
@@ -3,8 +3,6 @@ const mongoose = require('mongoose');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const { NotFoundError } = require('../errors');
|
||||
|
||||
const TodoList = mongoose.model('TodoList');
|
||||
|
||||
const asyncHelper = require('../asyncHelper');
|
||||
@@ -47,6 +45,27 @@ router.delete(
|
||||
}),
|
||||
);
|
||||
|
||||
// update
|
||||
router.patch(
|
||||
'/:slug',
|
||||
listIdMiddleware,
|
||||
asyncHelper(async (req, res) => {
|
||||
const { listId } = res.locals;
|
||||
const { name } = req.body;
|
||||
const patch = {};
|
||||
if (name) {
|
||||
patch.name = name;
|
||||
}
|
||||
const list = await TodoList.findByIdAndUpdate(
|
||||
{ _id: listId },
|
||||
{ $set: patch },
|
||||
{ new: true },
|
||||
).exec();
|
||||
await list.slugify();
|
||||
await list.save();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
router.use('/:slug/todos', listIdMiddleware, require('./todos'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -29,11 +29,29 @@ router.post(
|
||||
}),
|
||||
);
|
||||
|
||||
// update
|
||||
router.patch(
|
||||
'/:todoId',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { todoId } = req.params;
|
||||
const { text, completed } = req.body;
|
||||
const patch = {};
|
||||
if (text) {
|
||||
patch.text = text;
|
||||
}
|
||||
if (completed) {
|
||||
patch.completed = completed;
|
||||
}
|
||||
await Todo.update({ _id: todoId }, { $set: patch }).exec();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
|
||||
// delete
|
||||
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