From 2f3fde3b4e580ef7035580cea13d6d8c3d73d150 Mon Sep 17 00:00:00 2001 From: Ayush Date: Mon, 28 Oct 2024 20:44:48 +0530 Subject: [PATCH 1/2] added backend in feedback form --- backend/controllers/ContactController.js | 46 ++++++++++++++++++++---- backend/index.js | 2 +- backend/models/Feedback.js | 31 ++++++++++++++++ backend/routes/ContactRoutes.js | 11 +++--- index.html | 46 +++++++++++++++++++++++- 5 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 backend/models/Feedback.js diff --git a/backend/controllers/ContactController.js b/backend/controllers/ContactController.js index bfb0e5c9..d2124597 100644 --- a/backend/controllers/ContactController.js +++ b/backend/controllers/ContactController.js @@ -1,15 +1,49 @@ - -const ContactUs = require('../models/ContactUs'); - +const ContactUs = require("../models/ContactUs"); +const FeedbackModal = require("../models/Feedback"); exports.submitContactForm = async (req, res) => { const { name, email, message } = req.body; try { const newContact = new ContactUs({ name, email, message }); await newContact.save(); - return res.status(201).json({ message: 'Message sent successfully!' }); + return res.status(201).json({ message: "Message sent successfully!" }); + } catch (error) { + console.error("Error saving contact form:", error); + return res + .status(500) + .json({ message: "Failed to send message. Please try again later." }); + } +}; + +exports.userfeedback = async (req, res) => { + try { + const { Name, Destination, Rating, Review, Complaint } = req.body; + if (!Name || !Destination || !Rating || !Review) { + return res.status(400).send({ + message: "All fields are required", + }); + } + const response = new FeedbackModal({ + name: Name, + destination: Destination, + rating: Rating, + review: Review, + complaint: Complaint, + }); + + const feedback = await response.save(); + if (response) { + res.status(201).send({ + success: true, + message: "feedback recorded ", + feedback, + }); + } } catch (error) { - console.error('Error saving contact form:', error); - return res.status(500).json({ message: 'Failed to send message. Please try again later.' }); + console.log(error); + res.status(500).send({ + success: false, + message: "internal server error ", + }); } }; diff --git a/backend/index.js b/backend/index.js index 09a7bba3..fd6b9245 100644 --- a/backend/index.js +++ b/backend/index.js @@ -15,7 +15,7 @@ connectDB(); // CORS configuration app.use(cors({ - origin: 'http://127.0.0.1:5504', // Correct: specify the base URL only + origin: 'http://127.0.0.1:5505', // Correct: specify the base URL only credentials: true // Allow credentials (cookies) to be sent with requests })); diff --git a/backend/models/Feedback.js b/backend/models/Feedback.js new file mode 100644 index 00000000..6f6d37a7 --- /dev/null +++ b/backend/models/Feedback.js @@ -0,0 +1,31 @@ +const mongoose = require("mongoose"); + +const FeedbackSchema = new mongoose.Schema( + { + name: { + type: String, + required: true, + trim: true, + }, + destination: { + type: String, + trim: true, + }, + rating: { + type: Number, + required: true, + }, + review: { + type: String, + required: true, + trim: true, + }, + complaint: { + type: String, + trim: true, + }, + }, + { timestamps: true } +); + +module.exports = mongoose.model("Feedback", FeedbackSchema); diff --git a/backend/routes/ContactRoutes.js b/backend/routes/ContactRoutes.js index f1e713e6..29901218 100644 --- a/backend/routes/ContactRoutes.js +++ b/backend/routes/ContactRoutes.js @@ -1,9 +1,12 @@ -const express = require('express'); -const { submitContactForm } = require('../controllers/ContactController.js'); - +const express = require("express"); +const { + submitContactForm, + userfeedback, +} = require("../controllers/ContactController.js"); const router = express.Router(); -router.post('/', submitContactForm); +router.post("/", submitContactForm); +router.post("/feedback", userfeedback); module.exports = router; diff --git a/index.html b/index.html index cebf2953..39af4f7d 100644 --- a/index.html +++ b/index.html @@ -2560,7 +2560,7 @@

Rate your experience

placeholder="Describe any issue with the staff here. Please provide details">
- +
@@ -2574,6 +2574,50 @@

Rate your experience

+ +