Compare commits

..
3 Commits
4 changed files with 52 additions and 19 deletions

No files matched your search

+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "multi-stub",
"version": "1.1.2",
"version": "1.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "multi-stub",
"version": "1.1.2",
"version": "1.2.0",
"license": "MIT",
"dependencies": {
"axios": "^1.7.9",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "multi-stub",
"version": "1.1.2",
"version": "1.2.0",
"description": "",
"main": "index.js",
"scripts": {
+16 -16
View File
@@ -10,6 +10,22 @@ const { TOKEN_KEY } = require('./const')
const router = Router()
router.get('/:todoId/:itemId', async (req, res) => {
const { todoId, itemId } = req.params
const todo = await ListModel.findById(todoId)
if (!todo) {
return res.send(getAnswer(new Error('no such todo')))
}
const item = await ItemModel.findById(itemId).populate({ path: 'comments', populate: { path: 'author' } }).exec()
if (!item) {
return res.send(getAnswer(new Error('no such item')))
}
res.send(getAnswer(null, item))
})
router.use(expressjwt({ secret: TOKEN_KEY, algorithms: ['HS256'] }))
router.post('/:todoId/:itemId', async (req, res) => {
@@ -34,20 +50,4 @@ router.post('/:todoId/:itemId', async (req, res) => {
res.send(getAnswer(null, comment))
})
router.get('/:todoId/:itemId', async (req, res) => {
const { todoId, itemId } = req.params
const todo = await ListModel.findById(todoId)
if (!todo) {
return res.send(getAnswer(new Error('no such todo')))
}
const item = await ItemModel.findById(itemId).populate({ path: 'comments', populate: { path: 'author' } }).exec()
if (!item) {
return res.send(getAnswer(new Error('no such item')))
}
res.send(getAnswer(null, item))
})
module.exports = router
+33
View File
@@ -63,5 +63,38 @@ router.post('/item', requiredValidate('todoId', 'title'), async (req, res) => {
res.send(getAnswer(null, item))
})
// closed = new Date().toISOString()
router.patch('/item/:itemId', async (req, res) => {
const { itemId } = req.params
const { title, done } = req.body
const item = await ItemModel.findById(itemId)
if (!item) {
throw new Error('item not found')
}
if (title) {
item.title = title
}
if (done) {
item.done = done
item.closed = done ? new Date().toISOString() : null
}
await item.save()
res.send(getAnswer(null, item))
})
router.delete('/item/:itemId', async (req, res) => {
const { itemId } = req.params
await ItemModel.findByIdAndDelete(itemId)
res.send(getAnswer(null, { ok: true }))
})
module.exports = router