Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Ratings and Feedback #1692

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
46 changes: 46 additions & 0 deletions backend/controllers/ratingController.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
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: '[email protected]',
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
15 changes: 15 additions & 0 deletions backend/models/rating.js
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions backend/routes/ratingRoutes.js
Original file line number Diff line number Diff line change
@@ -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
Loading