diff --git a/backend/.env.example b/backend/.env.example index f1d747d3..61078343 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -1,3 +1,4 @@ +# MONGO_URI=mongodb://localhost:27017/bloggingdb MONGO_URI=mongodb://localhost:27017/bloggingdb JWT_SECRET=scijyasfy7dsvegdffvbfbfgg435tgrsnbgfgn PORT=5000 diff --git a/backend/app.js b/backend/app.js index 45eb11cd..d82646af 100644 --- a/backend/app.js +++ b/backend/app.js @@ -3,6 +3,7 @@ import dotenv from "dotenv"; import connectDB from "./utils/db.js"; import blogRoutes from "./routes/blogRoutes.js"; import userRoutes from "./routes/userRoutes.js"; +import cors from "cors"; dotenv.config(); const app = express(); @@ -10,6 +11,9 @@ connectDB(); app.use(express.json()); +// to avoid cross origin errror +app.use(cors()); + app.use("/api/users", userRoutes); app.use("/api/blogs", blogRoutes); diff --git a/backend/controllers/blogController.js b/backend/controllers/blogController.js index 59d838ac..ea9e498b 100644 --- a/backend/controllers/blogController.js +++ b/backend/controllers/blogController.js @@ -1,4 +1,5 @@ import Blog from "../models/blog.js"; +import Comment from "../models/comment.js"; export const createBlog = async (req, res) => { try { @@ -52,3 +53,38 @@ export const getSingleBlog = async (req, res) => { res.status(500).json({ message: error.message }); } }; + +export const saveComment = async (req, resp) => { + try { + const { name, comment } = req.body; + + if (!name || !comment) { + return resp.status(400).send({ + message: "all fields are required", + success: false, + }); + } + + const newcomment = await new Comment({ + name: name, + comment: comment, + }); + + newcomment.save(); + + if (newcomment) { + return resp.status(200).send({ + success: true, + message: "new comment added", + newcomment, + }); + } + } catch (error) { + console.log(error); + return resp.status(500).send({ + success: false, + message: "internal server error", + error, + }); + } +}; diff --git a/backend/models/comment.js b/backend/models/comment.js new file mode 100644 index 00000000..88afd9d9 --- /dev/null +++ b/backend/models/comment.js @@ -0,0 +1,21 @@ +import { mongoose } from "mongoose"; + +const commentSchema = new mongoose.Schema( + { + name: { + type: String, + required: true, + trim: true, + }, + comment: { + type: String, + required: true, + trim: true, + }, + }, + { timestamps: true } +); + +const Comment = mongoose.model("Comment", commentSchema); + +export default Comment; diff --git a/backend/package-lock.json b/backend/package-lock.json index b509023c..3879d11d 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "bcryptjs": "^2.4.3", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.1", "express-validator": "^7.2.0", @@ -281,6 +282,19 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/backend/package.json b/backend/package.json index c8f0fbf8..c261628b 100644 --- a/backend/package.json +++ b/backend/package.json @@ -13,6 +13,7 @@ "description": "", "dependencies": { "bcryptjs": "^2.4.3", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.1", "express-validator": "^7.2.0", diff --git a/backend/routes/blogRoutes.js b/backend/routes/blogRoutes.js index 8eb150ee..dbf89f32 100644 --- a/backend/routes/blogRoutes.js +++ b/backend/routes/blogRoutes.js @@ -5,6 +5,7 @@ import { deleteBlog, getAllBlogs, getSingleBlog, + saveComment, } from "../controllers/blogController.js"; import authMiddleware from "../middlewares/authMiddleware.js"; import adminMiddleware from "../middlewares/adminMiddleware.js"; @@ -23,4 +24,7 @@ router.put("/:id", authMiddleware, blogValidation, updateBlog); // Admin-only routes router.delete("/:id", authMiddleware, adminMiddleware, deleteBlog); +// save user comments +router.post("/savecomment", saveComment); + export default router; diff --git a/blog-comment.js b/blog-comment.js index 00e89a04..f2fbb8b9 100644 --- a/blog-comment.js +++ b/blog-comment.js @@ -139,7 +139,7 @@ function showToast(message, type) { toast.style.transition = "opacity 0.5s, transform 0.5s"; toast.style.boxShadow = "0 4px 20px rgba(0, 0, 0, 0.2)"; - // Apply different styles based on the type of toast + // Apply different styles based on the type of toast if (type === "success") { toast.style.backgroundColor = "rgba(40, 167, 69, 0.75)"; // Slightly transparent green } else if (type === "error") { @@ -165,3 +165,45 @@ function showToast(message, type) { }, 500); }, 3000); } +async function saveComment(event) { + event.preventDefault(); // Prevent the default form submission + + // Get values from the form inputs + const name = document.getElementById("commenterName").value.trim(); + const comment = document.getElementById("commentText").value.trim(); + + // Check if both name and comment fields are filled + if (!name || !comment) { + alert("All fields are required."); + return; + } + + // Prepare data to send to the server + const data = { name, comment }; + + try { + // Make the API request to save the comment + const response = await fetch( + "http://localhost:5000/api/blogs/savecomment", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + } + ); + + // Handle the response + const result = await response.json(); + if (result.success) { + alert(result.message); // Show success message + document.getElementById("commentForm").reset(); // Clear the form + } else { + alert(result.message); // Show error message from server + } + } catch (error) { + console.error("Error:", error); + alert("An error occurred. Please try again later."); + } +} diff --git a/blog.html b/blog.html index dc19ef20..5756b98d 100644 --- a/blog.html +++ b/blog.html @@ -539,7 +539,7 @@