diff --git a/backend/app.js b/backend/app.js index 3ead9710..0ecefb29 100644 --- a/backend/app.js +++ b/backend/app.js @@ -5,6 +5,7 @@ import blogRoutes from "./routes/blogRoutes.js"; import userRoutes from "./routes/userRoutes.js"; import feedbackRoutes from "./routes/feedbackRoute.js"; import contactRoutes from "./routes/contactRoute.js"; +import ratingRoutes from "./routes/ratingRoutes.js"; import getInTouch from "./routes/getInTouchRoutes.js"; import addBlog from "./routes/addBlogRoutes.js"; import cors from "cors"; @@ -30,6 +31,7 @@ app.use("/uploads", express.static(path.join(__dirname, "uploads"))); // Adjust app.use("/api/users", userRoutes); app.use("/api/blogs", blogRoutes); app.use("/api/feedback", feedbackRoutes); +app.use("/api/rating", ratingRoutes); app.use("/api/contact", contactRoutes); app.use("/api/getInTouch", getInTouch); app.use("/api/addBlog", addBlog); diff --git a/backend/controllers/ratingController.js b/backend/controllers/ratingController.js new file mode 100644 index 00000000..3511366c --- /dev/null +++ b/backend/controllers/ratingController.js @@ -0,0 +1,46 @@ +// controllers/feedbackController.js + +import Rating from '../models/rating.js'; +import nodemailer from 'nodemailer'; + +// Configure nodemailer transporter +const transporter = nodemailer.createTransport({ + service: 'gmail', // Example with Gmail, customize as needed + auth: { + user: 'your-email@gmail.com', + pass: 'your-email-password', + }, +}); + +const saveRating = async (req, res) => { + const { name, email, message, rating } = req.body; + + try { + // Save feedback to the database + const feedback = new Rating({ name, email, message, rating }); + await feedback.save(); + + // Send confirmation email to the user + const mailOptions = { + from: 'your-email@gmail.com', + to: email, + subject: 'Thank you for your feedback!', + text: `Dear ${name},\n\nThank you for your valuable feedback!\n\nYour Feedback:\n"${message}"\n\nRating: ${rating} stars\n\nBest regards,\nYour Company`, + }; + + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + console.error('Error sending email:', error); + return res.status(500).json({ message: 'Feedback saved but failed to send email' }); + } else { + console.log('Email sent:', info.response); + return res.status(200).json({ message: 'Feedback saved and email sent successfully!' }); + } + }); + } catch (error) { + console.error('Error saving feedback:', error); + res.status(500).json({ message: 'Failed to save feedback' }); + } +}; + +export default saveRating \ No newline at end of file diff --git a/backend/models/rating.js b/backend/models/rating.js new file mode 100644 index 00000000..ac5ce833 --- /dev/null +++ b/backend/models/rating.js @@ -0,0 +1,15 @@ +// models/Feedback.js + +import mongoose from 'mongoose'; + +const ratingSchema = new mongoose.Schema({ + name: { type: String, required: true }, + email: { type: String, required: true }, + message: { type: String, required: true }, + rating: { type: Number, required: true, min: 1, max: 5 }, + createdAt: { type: Date, default: Date.now } +}); + +const Rating = mongoose.model('Rating', ratingSchema); + +export default Rating diff --git a/backend/routes/ratingRoutes.js b/backend/routes/ratingRoutes.js new file mode 100644 index 00000000..a0cd93c6 --- /dev/null +++ b/backend/routes/ratingRoutes.js @@ -0,0 +1,11 @@ +// routes/feedbackRoutes.js + +import express from 'express'; +import saveRating from '../controllers/ratingController.js'; + +const router = express.Router(); + +// Route to save feedback +router.post('/', saveRating); + +export default router