feat: migrate Post and Comment models from MongoDB to CouchDB

- Replace mongoose Post model with CouchDB-based class using couchdbService
- Replace mongoose Comment model with CouchDB-based class using couchdbService
- Update posts route to use new CouchDB models with embedded user data
- Update comments route to use new CouchDB models with embedded user/post data
- Maintain all existing API endpoints and functionality
- Add like/unlike functionality for posts
- Handle image uploads with Cloudinary integration
- Preserve Socket.IO events for real-time updates
- Use denormalized structure for better performance with embedded data

🤖 Generated with [AI Assistant]

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
William Valentin
2025-11-01 13:23:07 -07:00
parent cb05a4eb4b
commit addff83bda
4 changed files with 439 additions and 212 deletions

View File

@@ -1,5 +1,4 @@
const express = require("express");
const mongoose = require("mongoose");
const Comment = require("../models/Comment");
const Post = require("../models/Post");
const auth = require("../middleware/auth");
@@ -26,13 +25,9 @@ router.get(
}
// Get comments with pagination
const comments = await Comment.find({ post: postId })
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.populate("user", ["name", "profilePicture"]);
const comments = await Comment.findByPostId(postId, { skip, limit });
const totalCount = await Comment.countDocuments({ post: postId });
const totalCount = await Comment.countDocuments({ "post.postId": postId });
res.json(buildPaginatedResponse(comments, totalCount, page, limit));
})
@@ -49,50 +44,39 @@ router.post(
const { postId } = req.params;
const { content } = req.body;
// Validate content
if (!content || content.trim().length === 0) {
return res.status(400).json({ msg: "Comment content is required" });
}
try {
// Validate content
await Comment.validateContent(content);
if (content.length > 500) {
return res
.status(400)
.json({ msg: "Comment content must be 500 characters or less" });
}
// Verify post exists
const post = await Post.findById(postId);
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
// Verify post exists
const post = await Post.findById(postId);
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
// Create comment
const newComment = new Comment({
user: req.user.id,
post: postId,
content: content.trim(),
});
const comment = await newComment.save();
// Update post's comment count
await Post.findByIdAndUpdate(postId, {
$inc: { commentsCount: 1 },
});
// Populate user data before sending response
await comment.populate("user", ["name", "profilePicture"]);
// Emit Socket.IO event for new comment
const io = req.app.get("io");
if (io) {
io.to(`post_${postId}`).emit("newComment", {
postId,
comment,
// Create comment
const comment = await Comment.create({
user: req.user.id,
post: postId,
content,
});
}
res.status(201).json(comment);
// Emit Socket.IO event for new comment
const io = req.app.get("io");
if (io) {
io.to(`post_${postId}`).emit("newComment", {
postId,
comment,
});
}
res.status(201).json(comment);
} catch (error) {
if (error.message.includes("required") || error.message.includes("characters")) {
return res.status(400).json({ msg: error.message });
}
throw error;
}
})
);
@@ -113,22 +97,19 @@ router.delete(
}
// Verify comment belongs to the post
if (comment.post.toString() !== postId) {
const belongsToPost = await Comment.belongsToPost(commentId, postId);
if (!belongsToPost) {
return res.status(400).json({ msg: "Comment does not belong to this post" });
}
// Verify user owns the comment
if (comment.user.toString() !== req.user.id) {
const isOwnedByUser = await Comment.isOwnedByUser(commentId, req.user.id);
if (!isOwnedByUser) {
return res.status(403).json({ msg: "Not authorized to delete this comment" });
}
// Delete comment
await Comment.findByIdAndDelete(commentId);
// Update post's comment count
await Post.findByIdAndUpdate(postId, {
$inc: { commentsCount: -1 },
});
await Comment.deleteComment(commentId);
// Emit Socket.IO event for deleted comment
const io = req.app.get("io");