Flare-k

[Modified] video update and delete

...@@ -51,14 +51,47 @@ export const videoDetail = async(req, res) => { ...@@ -51,14 +51,47 @@ export const videoDetail = async(req, res) => {
51 } = req; 51 } = req;
52 try { 52 try {
53 const video = await Video.findById(id); 53 const video = await Video.findById(id);
54 - //console.log(video); 54 + res.render("videoDetail", { pageTitle: video.title, video });
55 - res.render("videoDetail", { pageTitle: "Video Detail", video });
56 } catch (error) { 55 } catch (error) {
57 - //console.log(error);
58 res.redirect(routes.home); 56 res.redirect(routes.home);
59 } 57 }
60 }; 58 };
61 -export const editVideo = (req, res) =>
62 - res.render("editVideo", { pageTitle: "Edit Video" });
63 -export const deleteVideo = (req, res) =>
64 - res.render("deleteVideo", { pageTitle: "Delete Video" });
...\ No newline at end of file ...\ No newline at end of file
59 +export const getEditVideo = async(req, res) => {
60 + const {
61 + params: { id },
62 + } = req;
63 + try {
64 + const video = await Video.findById(id);
65 + //video를 받아서 render로 통해 템플릿으로 던져준다,
66 + res.render("editVideo", { pageTitle: `Edit ${video.title}`, video });
67 + // rendering하는 순간 템플릿에선 video의 title과 description을 던져준다.
68 + } catch (error) {
69 + res.redirect(routes.home);
70 + }
71 +};
72 +export const postEditVideo = async(req, res) => {
73 + const {
74 + params: { id },
75 + body: { title, description },
76 + } = req;
77 + try {
78 + //id를 찾아서 body를 얻어와야 한다. 비디오 수정에서 제목과 설명을 가져와야 하기 때문이다.
79 + //mongoose엔 우리의 id가 없어서 _id : id로 찾아줘야 한다.
80 + await Video.findOneAndUpdate({ _id: id }, { title, description }); //title:title == title
81 + //이렇게 하면 default로 얻어온 제목 및 내용을 수정하여 form을 전송하면 해당 내용으로 업데이트 된다.
82 + res.redirect(routes.videoDetail(id));
83 + } catch (error) {
84 + res.redirect(routes.home);
85 + }
86 +};
87 +
88 +export const deleteVideo = async(req, res) => {
89 + const {
90 + params: { id },
91 + } = req;
92 + try {
93 + await Video.findOneAndRemove({ _id: id });
94 + } catch (error) {}
95 + //삭제를 실패하던 성공하던 home으로 redirect한다.
96 + res.redirect(routes.home);
97 +};
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -4,20 +4,26 @@ import { ...@@ -4,20 +4,26 @@ import {
4 getUpload, 4 getUpload,
5 postUpload, 5 postUpload,
6 videoDetail, 6 videoDetail,
7 - editVideo, 7 + getEditVideo,
8 + postEditVideo,
8 deleteVideo, 9 deleteVideo,
9 } from "../controllers/videoController"; 10 } from "../controllers/videoController";
10 import { uploadVideo } from "../middlewares"; 11 import { uploadVideo } from "../middlewares";
11 //export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다. 12 //export const videoRouter = express.Router(); 이렇게하면 이 변수만 export하게 된다.
12 const videoRouter = express.Router(); 13 const videoRouter = express.Router();
13 14
15 +//Upload
14 videoRouter.get(routes.upload, getUpload); 16 videoRouter.get(routes.upload, getUpload);
15 videoRouter.post(routes.upload, uploadVideo, postUpload); 17 videoRouter.post(routes.upload, uploadVideo, postUpload);
16 18
17 -videoRouter.get(routes.editVideo, editVideo); 19 +// Video Detail
18 -
19 videoRouter.get(routes.videoDetail(), videoDetail); 20 videoRouter.get(routes.videoDetail(), videoDetail);
20 21
21 -videoRouter.get(routes.deleteVideo, deleteVideo); 22 +// Video Edit
23 +videoRouter.get(routes.editVideo(), getEditVideo);
24 +videoRouter.post(routes.editVideo(), postEditVideo);
25 +
26 +// Video Delete
27 +videoRouter.get(routes.deleteVideo(), deleteVideo);
22 28
23 export default videoRouter; 29 export default videoRouter;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -43,8 +43,20 @@ const routes = { ...@@ -43,8 +43,20 @@ const routes = {
43 return VIDEO_DETAIL; 43 return VIDEO_DETAIL;
44 } 44 }
45 }, 45 },
46 - editVideo: EDIT_VIDEO, 46 + editVideo: (id) => {
47 - deleteVideo: DELETE_VIDEO, 47 + if (id) {
48 + return `/videos/${id}/edit`;
49 + } else {
50 + return EDIT_VIDEO;
51 + }
52 + },
53 + deleteVideo: (id) => {
54 + if (id) {
55 + return `/videos/${id}/delete`;
56 + } else {
57 + return DELETE_VIDEO;
58 + }
59 + },
48 }; 60 };
49 61
50 export default routes; 62 export default routes;
...\ No newline at end of file ...\ No newline at end of file
......
1 extends layouts/main 1 extends layouts/main
2 2
3 block content 3 block content
4 -.form-container 4 + .form-container
5 - form(action=`/videos${routes.editVideo}`, method="post") 5 + form(action=routes.editVideo(video.id), method="post")
6 - input(type="text", placeholder="Title", name="title") 6 + input(type="text", placeholder="Title", name="title", value=video.title)
7 - textarea(name="description", placeholder="Description") 7 + textarea(name="description", placeholder="Description")=video.description
8 input(type="submit", value="Update Video") 8 input(type="submit", value="Update Video")
9 - a.form-container__link.form-container__link--delete(href=`/videos${routes.deleteVideo}`) Delete Video
...\ No newline at end of file ...\ No newline at end of file
9 + a.form-container__link.form-container__link--delete(href=routes.deleteVideo(video.id)) Delete Video
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -4,7 +4,7 @@ block content ...@@ -4,7 +4,7 @@ block content
4 .video__player 4 .video__player
5 video(src=`/${video.fileUrl}`) 5 video(src=`/${video.fileUrl}`)
6 .video__info 6 .video__info
7 - a(href=routes.editVideo) Edit video 7 + a(href=routes.editVideo(video.id)) Edit video
8 h5.video__title=video.title 8 h5.video__title=video.title
9 span.video__views=video.views 9 span.video__views=video.views
10 - p.video__description=video.desription
...\ No newline at end of file ...\ No newline at end of file
10 + p.video__description=video.description
...\ No newline at end of file ...\ No newline at end of file
......