Flare-k

[Modified] video update and delete

......@@ -51,14 +51,47 @@ export const videoDetail = async(req, res) => {
} = req;
try {
const video = await Video.findById(id);
//console.log(video);
res.render("videoDetail", { pageTitle: "Video Detail", video });
res.render("videoDetail", { pageTitle: video.title, video });
} catch (error) {
//console.log(error);
res.redirect(routes.home);
}
};
export const editVideo = (req, res) =>
res.render("editVideo", { pageTitle: "Edit Video" });
export const deleteVideo = (req, res) =>
res.render("deleteVideo", { pageTitle: "Delete Video" });
\ No newline at end of file
export const getEditVideo = async(req, res) => {
const {
params: { id },
} = req;
try {
const video = await Video.findById(id);
//video를 받아서 render로 통해 템플릿으로 던져준다,
res.render("editVideo", { pageTitle: `Edit ${video.title}`, video });
// rendering하는 순간 템플릿에선 video의 title과 description을 던져준다.
} catch (error) {
res.redirect(routes.home);
}
};
export const postEditVideo = async(req, res) => {
const {
params: { id },
body: { title, description },
} = req;
try {
//id를 찾아서 body를 얻어와야 한다. 비디오 수정에서 제목과 설명을 가져와야 하기 때문이다.
//mongoose엔 우리의 id가 없어서 _id : id로 찾아줘야 한다.
await Video.findOneAndUpdate({ _id: id }, { title, description }); //title:title == title
//이렇게 하면 default로 얻어온 제목 및 내용을 수정하여 form을 전송하면 해당 내용으로 업데이트 된다.
res.redirect(routes.videoDetail(id));
} catch (error) {
res.redirect(routes.home);
}
};
export const deleteVideo = async(req, res) => {
const {
params: { id },
} = req;
try {
await Video.findOneAndRemove({ _id: id });
} catch (error) {}
//삭제를 실패하던 성공하던 home으로 redirect한다.
res.redirect(routes.home);
};
\ No newline at end of file
......
......@@ -4,20 +4,26 @@ import {
getUpload,
postUpload,
videoDetail,
editVideo,
getEditVideo,
postEditVideo,
deleteVideo,
} from "../controllers/videoController";
import { uploadVideo } from "../middlewares";
//export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다.
const videoRouter = express.Router();
//Upload
videoRouter.get(routes.upload, getUpload);
videoRouter.post(routes.upload, uploadVideo, postUpload);
videoRouter.get(routes.editVideo, editVideo);
// Video Detail
videoRouter.get(routes.videoDetail(), videoDetail);
videoRouter.get(routes.deleteVideo, deleteVideo);
// Video Edit
videoRouter.get(routes.editVideo(), getEditVideo);
videoRouter.post(routes.editVideo(), postEditVideo);
// Video Delete
videoRouter.get(routes.deleteVideo(), deleteVideo);
export default videoRouter;
\ No newline at end of file
......
......@@ -43,8 +43,20 @@ const routes = {
return VIDEO_DETAIL;
}
},
editVideo: EDIT_VIDEO,
deleteVideo: DELETE_VIDEO,
editVideo: (id) => {
if (id) {
return `/videos/${id}/edit`;
} else {
return EDIT_VIDEO;
}
},
deleteVideo: (id) => {
if (id) {
return `/videos/${id}/delete`;
} else {
return DELETE_VIDEO;
}
},
};
export default routes;
\ No newline at end of file
......
extends layouts/main
block content
.form-container
form(action=`/videos${routes.editVideo}`, method="post")
input(type="text", placeholder="Title", name="title")
textarea(name="description", placeholder="Description")
.form-container
form(action=routes.editVideo(video.id), method="post")
input(type="text", placeholder="Title", name="title", value=video.title)
textarea(name="description", placeholder="Description")=video.description
input(type="submit", value="Update Video")
a.form-container__link.form-container__link--delete(href=`/videos${routes.deleteVideo}`) Delete Video
\ No newline at end of file
a.form-container__link.form-container__link--delete(href=routes.deleteVideo(video.id)) Delete Video
\ No newline at end of file
......
......@@ -4,7 +4,7 @@ block content
.video__player
video(src=`/${video.fileUrl}`)
.video__info
a(href=routes.editVideo) Edit video
a(href=routes.editVideo(video.id)) Edit video
h5.video__title=video.title
span.video__views=video.views
p.video__description=video.desription
\ No newline at end of file
p.video__description=video.description
\ No newline at end of file
......