Skip to content

Commit

Permalink
Delete & single story
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Traversy authored and Brad Traversy committed Jun 19, 2020
1 parent b3e0f8d commit 96be737
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
31 changes: 31 additions & 0 deletions routes/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ router.get('/', ensureAuth, async (req, res) => {
}
})

// @desc Show single story
// @route GET /stories/:id
router.get('/:id', ensureAuth, async (req, res) => {
try {
let story = await Story.findById(req.params.id).populate('user').lean()

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

res.render('stories/show', {
story,
})
} catch (err) {
console.error(err)
res.render('error/404')
}
})

// @desc Show edit page
// @route GET /stories/edit/:id
router.get('/edit/:id', ensureAuth, async (req, res) => {
Expand Down Expand Up @@ -92,4 +111,16 @@ router.put('/:id', ensureAuth, async (req, res) => {
}
})

// @desc Delete story
// @route DELETE /stories/:id
router.delete('/:id', ensureAuth, async (req, res) => {
try {
await Story.remove({ _id: req.params.id })
res.redirect('/dashboard')
} catch (err) {
console.error(err)
return res.render('error/500')
}
})

module.exports = router
24 changes: 24 additions & 0 deletions views/stories/show.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="row">
<div class="col s12 m8">
<h3>{{story.title}}
<small>{{{editIcon story.user user story._id false}}}</small>
</h3>
<div class="card story">
<div class="card-content">
<span class="card-title">{{formatDate date 'MMMM Do YYYY, h:mm:ss a'}}</span>
{{{story.body}}}
</div>
</div>
</div>
<div class="col s12 m4">
<div class="card center-align">
<div class="card-content">
<span class="card-title">{{story.user.displayName}}</span>
<img src="{{story.user.image}}" class="circle responsive-img img-small">
</div>
<div class="card-action">
<a href="/stories/user/{{story.user._id}}">More From {{story.user.firstName}}</a>
</div>
</div>
</div>
</div>

0 comments on commit 96be737

Please sign in to comment.