diff --git a/backend/controllers/addBlogController.js b/backend/controllers/addBlogController.js index 232f2fcf..3497bde4 100644 --- a/backend/controllers/addBlogController.js +++ b/backend/controllers/addBlogController.js @@ -20,7 +20,7 @@ const upload = multer({ storage: storage }); // Function to save a new blog post export async function saveBlog(req, res) { try { - const { title, category, summary, excerpt, tags, publish, featuredImage } = req.body; + const { title, category, summary, excerpt, tags, publish, likes, featuredImage } = req.body; // If an image is uploaded, use its path let imagePath = req.file ? req.file.path : null; @@ -56,6 +56,7 @@ export async function saveBlog(req, res) { excerpt, tags, publish, + likes, featuredImage: imagePath // Use the determined image path }); @@ -104,4 +105,35 @@ export async function getBlog(req, res) { } } + + +export async function updateLikes(req, res) { + const { postId, liked } = req.body; // Destructure liked from the body + console.log(postId + " " + liked); + + try { + // Find the post by ID + const post = await BlogPost.findById(postId); + console.log(post); + + if (!post) { + return res.status(404).json({ message: "Post not found" }); + } + + // Update the likes count based on the "liked" value + post.likes = liked ? post.likes + 1 : Math.max(post.likes - 1, 0); // Update post.likes, not BlogPost.likes + + // Save the updated post + await post.save(); + + // Respond with the new likes count + return res.status(200).json({ likesCount: post.likes }); // Use post.likes + } catch (error) { + console.error("Error updating likes:", error); + return res.status(500).json({ message: "Failed to update likes" }); + } +} + + + export { upload }; \ No newline at end of file diff --git a/backend/models/addBlog.js b/backend/models/addBlog.js index 99c64c4d..334874e1 100644 --- a/backend/models/addBlog.js +++ b/backend/models/addBlog.js @@ -33,6 +33,10 @@ const blogPostSchema = new mongoose.Schema({ type: String, default: null }, + likes: { + type: Number, + default: 0 + }, createdAt: { type: Date, default: Date.now diff --git a/backend/routes/addBlogRoutes.js b/backend/routes/addBlogRoutes.js index 8524bd0a..52c549b9 100644 --- a/backend/routes/addBlogRoutes.js +++ b/backend/routes/addBlogRoutes.js @@ -1,9 +1,10 @@ import express from "express"; -import { getAllBlog, getBlog, saveBlog, upload } from "../controllers/addBlogController.js"; +import { getAllBlog, getBlog, saveBlog, upload, updateLikes } from "../controllers/addBlogController.js"; const router = express.Router(); router.post("/saveBlog", upload.single('featuredImage'), saveBlog); router.get("/getAllBlog", getAllBlog); +router.patch("/updateLikes", updateLikes); router.get("/getBlog/:id", getBlog); export default router; diff --git a/frontend/src/pages/AddBlog.js b/frontend/src/pages/AddBlog.js index d1098e8d..7741c509 100644 --- a/frontend/src/pages/AddBlog.js +++ b/frontend/src/pages/AddBlog.js @@ -95,6 +95,7 @@ export function renderAddBlog(container) { formData.append('excerpt', excerpt); formData.append('tags', tags); formData.append('publish', publish); + formData.append('likes', 0); if (featuredImage) { formData.append('featuredImage', featuredImage); // Ensure key matches backend } diff --git a/frontend/src/pages/Blogs.js b/frontend/src/pages/Blogs.js index 686047d3..86972301 100644 --- a/frontend/src/pages/Blogs.js +++ b/frontend/src/pages/Blogs.js @@ -86,10 +86,10 @@ function setupCategoryFilter(blogPosts) { } function renderBlogPosts(posts) { - return posts.map(post => renderBlogPost(post.id, post.title, post.excerpt, post.date, post.tags, post.featuredImage, post.publish)).join(''); + return posts.map(post => renderBlogPost(post._id, post.title, post.excerpt, post.createdAt, post.tags, post.featuredImage, post.publish, post.likes)).join(''); } -function renderBlogPost(id, title, excerpt, date, tags, imageUrl, publish) { +function renderBlogPost(id, title, excerpt, date, tags, imageUrl, publish, likes) { if (!publish) return ''; let imagePath = ""; if (imageUrl) { @@ -106,18 +106,91 @@ function renderBlogPost(id, title, excerpt, date, tags, imageUrl, publish) {
+