[Comment.js]
import mongoose from "mongoose";
const commentSchema = new mongoose.Schema({
text: { type: String, required: true },
owner: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "User" },
video: { type: mongoose.Schema.Types.ObjectId, required: true, ref: "Video" },
createdAt: { type: Date, required: true, default: Date.now },
});
const Comment = mongoose.model("Comment", commentSchema);
export default Comment;
==========================
[Video.js]
...
comments: [{ type: mongoose.Schema.Types.ObjectId, required:true, ref: "Comment" }],
...
==========================
[User.js]
...
comments: [{ type: mongoose.Schema.Types.ObjectId, required:true, ref: "Comment" }],
...
==========================
[webpack.config.js]
const BASE_JS = "./src/client/js/";
module.exports = {
entry: {
main: BASE_JS + "main.js", // 변경할 src code위치
videoPlayer: BASE_JS + "videoPlayer.js",
recorder: BASE_JS + "recorder.js",
commentSection: BASE_JS + "commentSection.js",
},
...
=================================
[watch.pug]
block content
div#videoContainer(data-id=video._id)
...
if loggedIn
div.video__comments
form.video__comment-form#commentForm
textarea(cols="30", rows="10", placeholder="Write a nice commment...")
button Add Comment
block scripts
script(src="/static/js/videoPlayer.js")
script(src="/static/js/commentSection.js")
=================================
[src/client/js/commentSection.js]
const videoContainer = document.getElementById("videoContainer");
const form = document.getElementById("commentForm");
const textarea = form.querySelector("textarea");
const btn = form.querySelector("button");
const handleSubmit = (event) => {
event.preventDefault();
const text = textarea.value;
const video = videoContainer.dataset.id;
};
form.addEventListener("submit", handleSubmit);
[watch.pug]
block scripts
script(src="/static/js/videoPlayer.js")
if loggedIn
script(src="/static/js/commentSection.js")
========================
[commentSection.js]
const videoContainer = document.getElementById("videoContainer");
const form = document.getElementById("commentForm");
const handleSubmit = (event) => {
event.preventDefault();
const textarea = form.querySelector("textarea");
const text = textarea.value;
const video = videoContainer.dataset.id;
};
if (form) {
form.addEventListener("submit", handleSubmit);
}
[commentSections.js]
const handleSubmit = (event) => {
...
if (text === "") {
return;
}
fetch(`/api/videos/${videoId}/comment`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ text}),
});
};
====================================
[apiRouter.js]
apiRouter.post("/videos/:id([0-9a-f]{24})/comment", createComment);
====================================
[videoController.js]
export const createComment = (req, res) => {
console.log(req.params);
console.log(req.body);
return res.end();
};
==================
[server.js]
app.use(express.json());
export const createComment = async (req, res) => {
const {
session: { user },
body: { text },
params: { id },
} = req;
const video = await Video.findById(id);
if (!video) {
return res.sendStatus(404);
}
const comment = await Comment.create({
text,
owner: user._id,
video: id,
});
return res.sendStatus(201);
};
[videoController.js]
export const watch = async (req, res) => {
..
const video = await Video.findById(id).populate("owner").populate("comments");
..
}
export const createComment = async (req, res) => {
...
video.comments.push(comment._id);
video.save();
..
};
[watch.pug]
div.video__comments
ul
each comment in video.comments.reverse()
li.video__comment
i.fas.fa-comment
| #{comment.text}
===========================
[commentSection.js]
async
.. await fetch
..
window.location.reload();
>> 댓글새로 달릴때마다 리로드 But 부하걸리니 다른방법으로 할거임
[commentSection.js]
const addComment = (text) => {
const videoComments = document.querySelector(".video__comments ul");
const newComment = document.createElement("li");
newComment.className = "video__comment";
const icon = document.createElement("i");
icon.className = "fas fa-comment";
const span = document.createElement("span");
span.innerText = `${text}`;
newComment.appendChild(icon);
newComment.appendChild(span);
videoComments.prepend(newComment);
};
const handleSubmit = async (event) => {
...
const { status } = await fetch(`/api/videos/${videoId}/comment`, {
...
});
if (status === 201) {
addComment(text);
}...
[videoController.js]
export const createComment = async (req, res) => {
...
return res.status(201).json({ newCommentId: comment._id });
};
============================
[commentSection.js]
const addComment = (text, id) => {
...
newComment.dataset.id = id; << << <<
...
const span2 = document.createElement("span");
span2.innerText = "❌";
..
newComment.appendChild(span2);
videoComments.prepend(newComment);
};
const handleSubmit = async (event) => {
const response = await fetch(`/api/videos/${videoId}/comment`, {
...
});
if (response.status === 201) {
...
const { newCommentId } = await response.json();
addComment(text, newCommentId);
}
};
ch17. deployment (0) | 2022.09.08 |
---|---|
ch15. flash message (0) | 2022.09.05 |
ch14. webassembly video transcode (0) | 2022.09.02 |
ch13. video recorder (0) | 2022.09.01 |
ch12. Views api << 템플릿 렌더링 X (0) | 2022.09.01 |
댓글 영역