들어가기
비디오 지우기~
delete는 get으로 작동한다는 것을 명심한다.
videoRouter
.route('/:id([0-9a-f]{24})/delete')
.all(protectorMiddleware)
.get(deleteVideo)
export const deleteVideo = async (req, res) => {
const { id } = req.params
const {
user: { _id },
} = req.session
const video = await Video.findById(id)
if(!video){
return res.render('404', { pageTitle: 'Video not found' })
}
if (String(video.owner) !== String(_id)) {
return res.status(400).redirect('/')
}
await Video.findByIdAndDelete(id)
return res.redirect('/')
}
///findByIdAndDelete를 사용하면 한번에 처리할 수 있다.
///defensive는 자동적으로 해 주어야함.
extends base.pug
//- block head
//- title Watch | JmTube
block content
video(src='/'+video.fileUrl, controls, width=600, height=500)
h1=video.title
div
p=video.description
small=video.createdAt
div
small=video.owner.email
br
small Uploaded by
a(href=`/users/${video.owner._id}`) #{video.owner.username}
each hashtag in video.hashtags
li=hashtag
h3 #{video.views} #{video.views === 1 ? "view" : "views"}
if String(video.owner._id) === String(loggedInUser._id)
a(href=`${video.id}/edit`) Edit Video →
br
a(href=`${video.id}/delete`) Delete Video →
///path를 적어주면, 위 path의 controller(deleteVideo)가 작동함.