1. κ²μλ¬Ό μ’μμ λ° μ’μμ μ·¨μ κΈ°λ₯ ꡬννκΈ°
- μ΄λ² ν¬μ€ν μμλ κ²μλ¬Ό μ’μμ λ° μ’μμ μ·¨μ κΈ°λ₯μ ꡬνν¨.
- λν κ²μλ¬Ό μ‘°ν μ μ’μμ κ°μλ₯Ό 보μ¬μ£Όλ λ°©μλ ν¨κ» ꡬνν¨.
1) Like μ€ν€λ§ μ μ(Like.js)
- like.js νμΌμμλ μ’μμ λ°μ΄ν°λ₯Ό μ μ₯νλ like μ€ν€λ§λ₯Ό μ μνμ.
- μ΄ μ€ν€λ§λ user_idμ, μ’μμλ₯Ό λλ₯Έ λμ(post_id λλ comment_id)μ IDλ₯Ό μ μ₯ν¨.
// Like.js
const mongoose = require("mongoose");
const likeSchema = new mongoose.Schema({
user_id: {
type: String,
required: true,
},
post_id: {
type: String,
required: false,
},
comment_id: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
},
});
const Like = mongoose.model("Like", likeSchema);
module.exports = { Like };
2) κΈ°μ‘΄μ Post μ€ν€λ§ μμ (Post.js)
- likes νλλ₯Ό μΆκ°ν΄ νΉμ κ²μλ¬Όμ μ’μμλ₯Ό λλ₯Έ μ¬μ©μ ID λͺ©λ‘μ μ μ₯νλ λ°°μ΄λ‘ μ€μ ν¨.
// post.js
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
...
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
});
const Post = mongoose.model("Post", postSchema);
module.exports = { Post };
3) κ²μλ¬Ό μ’μμ λ° μ’μμ μ·¨μ API(likePost.js)
- μ¬μ©μκ° μ΄λ―Έ μ’μμλ₯Ό λλ₯Έ κ²½μ° μ·¨μνκ³ , κ·Έλ μ§ μμΌλ©΄ μ’μμλ₯Ό μΆκ°νλ λ°©μμΌλ‘ μλ
- κΈ°λ³Έ λ‘μ§:
- μμ²λ postId λ₯Ό κΈ°λ°μΌλ‘ κ²μλ¬Ό κ²μ
- likes λ°°μ΄μ μ¬μ©μμ IDκ° μ΄λ―Έ μ‘΄μ¬νλ©΄ λ°°μ΄μμ ν΄λΉ IDλ₯Ό μ κ±°νκ³ "μ’μμ μ·¨μ" μ€ν
- μ¬μ©μμ IDκ° μ‘΄μ¬νμ§ μμΌλ©΄ likes λ°°μ΄μ IDλ₯Ό μΆκ°νκ³ "μ’μμ μΆκ°" μ€ν
// likePost.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 { Post } = require("../../models/Post");
// κ²μλ¬Ό μ’μμ API
router.post("/:postId/like", auth, async (req, res) => {
const { postId } = req.params;
const userId = req.user.id;
try {
const post = await Post.findById(postId);
if (!post) {
return res.status(404).json({ message: "κ²μλ¬Όμ μ°Ύμ μ μμ΅λλ€." });
}
const existingLikeIndex = post.likes.indexOf(userId);
if (existingLikeIndex !== -1) {
post.likes.splice(existingLikeIndex, 1);
await post.save();
return res.status(200).json({ message: "μ’μμκ° μ·¨μλμμ΅λλ€." });
} else {
post.likes.push(userId);
await post.save();
return res.status(201).json({ message: "μ’μμκ° μΆκ°λμμ΅λλ€." });
}
} catch (error) {
console.error(error);
return res.status(500).json({ message: "μλ² μ€λ₯κ° λ°μνμ΅λλ€." });
}
});
module.exports = router;
4) κΈ°μ‘΄ κ²μλ¬Ό μ‘°ν API μμ (getPost.js)
- νΉμ κ²μλ¬Ό μ‘°νν λ ν΄λΉ κ²μλ¬Όμ μ’μμ κ°μλ λ°ννλλ‘ μ€μ .
- ν΅μ¬ κΈ°λ₯:
- post.likes.length λ₯Ό μ¬μ©ν΄ μ’μμ μ κ²μ°
- post κ°μ²΄μ μ’μμ μλ₯Ό μλ΅μΌλ‘ λ°ν
// getPost.js
const express = require("express");
const router = express.Router();
const { Post } = require("../../models/Post");
router.get("/:id", async (req, res) => {
...
const likesCount = post.likes.length;
return res.status(200).json({
post: {
...post.toObject(),
likesCount,
},
});
} catch (error) {
return res.status(500).json({
message: "κ²μλ¬Ό μ‘°ν μ€ μ€λ₯κ° λ°μνμ΅λλ€.",
error: error.message,
});
}
});
module.exports = router;
2. κ²°κ³Ό π
'Backend π > Node.js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Node]Instagram Clone - 11. λκΈ μ’μμ, μ’μμ μ·¨μ API (0) | 2024.11.08 |
---|---|
[Node] ν°λ―Έλμ ν΅ν΄ Socket ν΅μ (0) | 2024.11.07 |
[Node]Instagram Clone - 9. νΉμ νμ μ‘°ν API (0) | 2024.11.05 |
[Node]Instagram Clone - 8. λκΈ μμ API (0) | 2024.11.04 |
[Node]Instagram Clone - 7. λκΈ μμ± API (0) | 2024.11.03 |