Flare-k

[Add] comment

1 /* eslint-disable no-console */ 1 /* eslint-disable no-console */
2 import routes from "../routes"; 2 import routes from "../routes";
3 import Video from "../models/Video"; 3 import Video from "../models/Video";
4 +import Comment from "../models/Comment";
4 5
5 // db를 import 해주고 home에 async를 달아준다. async는 기다려주는 역할을 한다. 6 // db를 import 해주고 home에 async를 달아준다. async는 기다려주는 역할을 한다.
6 // javascript가 db를 다 못보고 그냥 지나갈 수도 있기 때문이다. 7 // javascript가 db를 다 못보고 그냥 지나갈 수도 있기 때문이다.
...@@ -62,7 +63,9 @@ export const videoDetail = async (req, res) => { ...@@ -62,7 +63,9 @@ export const videoDetail = async (req, res) => {
62 params: { id }, 63 params: { id },
63 } = req; 64 } = req;
64 try { 65 try {
65 - const video = await Video.findById(id).populate("creator"); 66 + const video = await Video.findById(id)
67 + .populate("creator")
68 + .populate("comments");
66 res.render("videoDetail", { pageTitle: video.title, video }); 69 res.render("videoDetail", { pageTitle: video.title, video });
67 } catch (error) { 70 } catch (error) {
68 res.redirect(routes.home); 71 res.redirect(routes.home);
...@@ -136,3 +139,25 @@ export const postRegisterView = async (req, res) => { ...@@ -136,3 +139,25 @@ export const postRegisterView = async (req, res) => {
136 res.end(); 139 res.end();
137 } 140 }
138 }; 141 };
142 +
143 +// 댓글 부분
144 +export const postAddComment = async (req, res) => {
145 + const {
146 + params: { id }, // URL에서 가져옴
147 + body: { comment },
148 + user,
149 + } = req;
150 + try {
151 + const video = await Video.findById(id);
152 + const newComment = await Comment.create({
153 + text: comment,
154 + creator: user.id,
155 + });
156 + video.comments.push(newComment.id);
157 + video.save();
158 + } catch (error) {
159 + res.status(400);
160 + } finally {
161 + res.end();
162 + }
163 +};
......
1 import express from "express"; 1 import express from "express";
2 import routes from "../routes"; 2 import routes from "../routes";
3 -import { postRegisterView } from "../controllers/videoController"; 3 +import {
4 + postRegisterView,
5 + postAddComment,
6 +} from "../controllers/videoController";
4 7
5 const apiRouter = express.Router(); 8 const apiRouter = express.Router();
6 9
7 apiRouter.post(routes.registerView, postRegisterView); 10 apiRouter.post(routes.registerView, postRegisterView);
8 - 11 +apiRouter.post(routes.addComment, postAddComment);
9 export default apiRouter; 12 export default apiRouter;
......
...@@ -31,6 +31,9 @@ const FB_CALLBACK = "/auth/facebook/callback"; ...@@ -31,6 +31,9 @@ const FB_CALLBACK = "/auth/facebook/callback";
31 const API = "/api"; 31 const API = "/api";
32 const REGISTER_VIEW = "/:id/view"; 32 const REGISTER_VIEW = "/:id/view";
33 33
34 +// Comment
35 +const ADD_COMMENT = "/:id/comment";
36 +
34 const routes = { 37 const routes = {
35 home: HOME, 38 home: HOME,
36 join: JOIN, 39 join: JOIN,
...@@ -78,6 +81,7 @@ const routes = { ...@@ -78,6 +81,7 @@ const routes = {
78 81
79 api: API, 82 api: API,
80 registerView: REGISTER_VIEW, 83 registerView: REGISTER_VIEW,
84 + addComment: ADD_COMMENT,
81 }; 85 };
82 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다. 86 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
83 export default routes; 87 export default routes;
......
...@@ -23,4 +23,9 @@ block content ...@@ -23,4 +23,9 @@ block content
23 if video.comments.length === 1 23 if video.comments.length === 1
24 span.video__comment-number 1 comment 24 span.video__comment-number 1 comment
25 else 25 else
26 - span.video__comment-number #{video.comments.length} comments
...\ No newline at end of file ...\ No newline at end of file
26 + span.video__comment-number #{video.comments.length} comments
27 + form.add__comment#jsAddComment
28 + input(type="text", placeholder="Add a comment", name="comment")
29 + ul.video__comments-list
30 + each comment in video.comments
31 + span comment.text
...\ No newline at end of file ...\ No newline at end of file
......