fix get comments to unknown user

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-01-19 22:35:22 +03:00
parent 0d5dc115b6
commit 3d935af6f1

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