diff --git a/backend/controllers/ratingController.js b/backend/controllers/ratingController.js new file mode 100644 index 00000000..5fa0f850 --- /dev/null +++ b/backend/controllers/ratingController.js @@ -0,0 +1,33 @@ +// controllers/ratingController.js + +const Rating = require('../models/Rating'); + +// POST /api/ratings +const createRating = async (req, res) => { + const { name, destination, rating, review, complaint } = req.body; + + // Validate rating + if (rating < 1 || rating > 5) { + return res.status(400).json({ message: 'Rating must be between 1 and 5.' }); + } + + try { + const newRating = new Rating({ + name, + destination, + rating, + review, + complaint + }); + + await newRating.save(); + res.status(201).json({ message: 'Rating submitted successfully!' }); + } catch (error) { + console.error(error); + res.status(500).json({ message: 'Internal server error.' }); + } +}; + +module.exports = { + createRating +}; diff --git a/backend/index.js b/backend/index.js index 1f42b108..0c45d109 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,11 +1,13 @@ -// Import required modules -const express = require("express"); -const connectDB = require("./config/db"); // Database connection -const authRoutes = require("./routes/authRoutes"); // Authentication routes -const ContactRoutes = require("./routes/ContactRoutes"); -const cookieParser = require("cookie-parser"); // Middleware for parsing cookies -const config = require("./config/config"); // Config file for environment variables -const cors = require("cors"); // Middleware for Cross-Origin Resource Sharing + +const express = require('express'); +const connectDB = require('./config/db'); // Database connection +const authRoutes = require('./routes/authRoutes'); // Authentication routes +const ContactRoutes = require('./routes/ContactRoutes'); +const RatingRoutes = require('./routes/RatingRoutes'); +const cookieParser = require('cookie-parser'); // Middleware for parsing cookies +const config = require('./config/config'); // Config file for environment variables +const cors = require('cors'); // Middleware for Cross-Origin Resource Sharing + // Initialize express app const app = express(); @@ -36,9 +38,11 @@ app.use(cors({ app.use(express.json()); // Parse JSON bodies app.use(cookieParser()); // Enable cookie parsing -// API routes -app.use("/api/auth", authRoutes); -app.use("/api/contact", ContactRoutes); +//API routes +app.use('/api/auth', authRoutes); +app.use('/api/contact', ContactRoutes); +app.use('/api/rating', RatingRoutes); + // Server listening on configured port const PORT = config.port || 5000; // Use config for port, default to 5000 if undefined diff --git a/backend/models/Rating.js b/backend/models/Rating.js new file mode 100644 index 00000000..1dbedefb --- /dev/null +++ b/backend/models/Rating.js @@ -0,0 +1,16 @@ +// models/Rating.js + +const mongoose = require('mongoose'); + +const ratingSchema = new mongoose.Schema({ + name: { type: String, required: true }, + destination: { type: String, required: true }, + rating: { type: Number, required: true, min: 1, max: 5 }, // Ensures rating is between 1 and 5 + review: { type: String, required: true }, + complaint: { type: String, default: '' }, // Optional field + createdAt: { type: Date, default: Date.now } // Timestamp for when the review was created +}); + +const Rating = mongoose.model('Rating', ratingSchema); + +module.exports = Rating; diff --git a/backend/routes/RatingRoutes.js b/backend/routes/RatingRoutes.js new file mode 100644 index 00000000..4c12b4ef --- /dev/null +++ b/backend/routes/RatingRoutes.js @@ -0,0 +1,9 @@ +const express = require('express'); +const { createRating } = require('../controllers/ratingController.js'); + + +const router = express.Router(); + +router.post('/', createRating); + +module.exports = router; diff --git a/index.html b/index.html index 0c0e253f..2a5d40ea 100644 --- a/index.html +++ b/index.html @@ -3245,4 +3245,4 @@