1. λκΈ μ’μμ λ° μ’μμ μ·¨μ κΈ°λ₯ ꡬννκΈ°
- μ΄λ² ν¬μ€νΈμμλ λκΈ μ’μμ κΈ°λ₯μ ꡬννκ³ , κ²μλ¬Ό μ‘°ν μ λκΈ μ’μμ μ νμλ₯Ό ꡬννκ² μ.
1) κΈ°μ‘΄μ Comment μ€ν€λ§ μμ (Comment.js)
- Comment μ€ν€λ§μ μ’μμ λλ₯Έ μ¬μ©μ ID μ μ₯νλ likes νλ μΆκ°.
- likes νλλ User μ€ν€λ§ λͺ¨λΈμ ObjecctId λ°°μ΄λ‘ μ€μ ν΄ κ° λκΈμ μ’μμ μ¬μ©μ μ 보λ₯Ό μ μ₯ν¨.
// comment.js
const mongoose = require("mongoose");
const commentSchema = new mongoose.Schema({
...
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User", // μ’μμλ₯Ό λλ₯Έ μ¬μ©μ ID
},
],
});
const Comment = mongoose.model("Comment", commentSchema);
module.exports = { Comment };
2) λκΈ μ’μμ λ° μ’μμ μ·¨μ API
- λκΈμ μ’μμμ μ’μμ μ·¨μ κΈ°λ₯ ꡬν(맀컀λμ¦μ κ²μλ¬Ό μ’μμ λ° μ’μμ μ·¨μμ λμΌ)
- μμ² κ²½λ‘μ commentIdλ₯Ό κ°μ Έμ ν΄λΉ λκΈμ μ κ·Όνλ©°, likes λ°°μ΄μμ μ΄λ―Έ μ’μμκ° λλ Έλμ§ νμΈν¨.
- μ’μμκ° λλ¦° κ²½μ°: likes λ°°μ΄μμ μ¬μ©μ IDλ₯Ό μ κ±°νμ¬ μ’μμλ₯Ό μ·¨μνκ³ μν μ½λλ₯Ό 200μΌλ‘ λ°ν.
- μ’μμκ° μλ κ²½μ°: μ¬μ©μ IDλ₯Ό likes λ°°μ΄μ μΆκ°νκ³ μν μ½λλ₯Ό 201λ‘ λ°ν.
// likeComment.js
const express = require("express");
const router = express.Router();
router.use(express.json());
const { auth } = require("../auth");
const cookieParser = require("cookie-parser");
router.use(cookieParser());
const { Comment } = require("../../models/Comment");
router.post("/:commentId/like", auth, async (req, res) => {
const { commentId } = req.params;
const userId = req.user.id;
try {
const comment = await Comment.findById(commentId);
if (!comment) {
return res.status(404).json({ message: "λκΈμ μ°Ύμ μ μμ΅λλ€." });
}
const existingLikeIndex = comment.likes.indexOf(userId);
if (existingLikeIndex !== -1) {
// μ΄λ―Έ μ’μμκ° λλ¦° κ²½μ° μ’μμ μ·¨μ
comment.likes.splice(existingLikeIndex, 1);
await comment.save();
return res.status(200).json({ message: "λκΈ μ’μμκ° μ·¨μλμμ΅λλ€." });
} else {
// μ’μμ μΆκ°
comment.likes.push(userId);
await comment.save();
return res.status(201).json({ message: "λκΈ μ’μμκ° μΆκ°λμμ΅λλ€." });
}
} catch (error) {
console.error(error);
return res.status(500).json({ message: "μλ² μ€λ₯κ° λ°μνμ΅λλ€." });
}
});
module.exports = router;
3) κ²μλ¬Ό μ‘°ν μ λκΈμ μ’μμ μ νμ
- getPost.js μμ κ²μ무 λ¨κ±΄ μ‘°ν μ κ° λκΈμ μ’μμ μ λ₯Ό ν¨κ» νμνλλ‘ μμ ν¨.
- commentsWithLikeCount λ°°μ΄μ ν΅ν΄ κ° λκΈμ μ’μμ μλ₯Ό ν¬ν¨ν μ 보λ₯Ό κ°μ Έμ€κ³ ,
- λκΈ likes λ°°μ΄μ κΈΈμ΄λ₯Ό κ³μ°νμ¬ likesCountλ₯Ό μΆκ°ν ν, μ΅μ’ μλ΅μμ post κ°μ²΄ μμ likesCountμ ν¨κ» ν¬ν¨λ¨.
// getPost.js
const express = require("express");
const router = express.Router();
const { Post } = require("../../models/Post");
router.get("/:id", async (req, res) => {
const { id } = req.params;
try {
const post = await Post.findById(id)
.populate({
path: "comments",
populate: { path: "user", select: "username" }, // λκΈ μμ±μ μ 보 ν¬ν¨
})
.populate("user", "username");
if (!post) {
return res.status(404).json({ message: "κ²μλ¬Όμ μ°Ύμ μ μμ΅λλ€." });
}
...
// κ° λκΈμ λν μ’μμ μ μΆκ°
const commentsWithLikeCount = post.comments.map((comment) => ({
...comment.toObject(),
likesCount: comment.likes.length, // λκΈμ μ’μμ μ
}));
return res.status(200).json({
post: {
...post.toObject(),
likesCount,
comments: commentsWithLikeCount,
},
});
...
});
module.exports = router;
2. κ²°κ³Ό π