Skip to content

Commit

Permalink
Merge pull request bradtraversy#1 from mellonis/patch-1
Browse files Browse the repository at this point in the history
PR: DELETE /stories/:id improving
  • Loading branch information
bradtraversy committed Jun 20, 2020
2 parents 57f25bb + 1c15fd6 commit 87711ab
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions routes/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ router.put('/:id', ensureAuth, async (req, res) => {

res.redirect('/dashboard')
}
} catch (error) {
} catch (err) {
console.error(err)
return res.render('error/500')
}
Expand All @@ -115,8 +115,18 @@ router.put('/:id', ensureAuth, async (req, res) => {
// @route DELETE /stories/:id
router.delete('/:id', ensureAuth, async (req, res) => {
try {
await Story.remove({ _id: req.params.id })
res.redirect('/dashboard')
let story = await Story.findById(req.params.id).lean()

if (!story) {
return res.render('error/404')
}

if (story.user != req.user.id) {
res.redirect('/stories')
} else {
await Story.remove({ _id: req.params.id })
res.redirect('/dashboard')
}
} catch (err) {
console.error(err)
return res.render('error/500')
Expand Down

0 comments on commit 87711ab

Please sign in to comment.