Skip to content

Commit

Permalink
Merge pull request #1665 from haseebzaki-07/new_branch_3
Browse files Browse the repository at this point in the history
Add ratings
  • Loading branch information
PriyaGhosal authored Oct 31, 2024
2 parents 09150a2 + c9c65f4 commit d0aa3cf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
33 changes: 33 additions & 0 deletions backend/controllers/ratingController.js
Original file line number Diff line number Diff line change
@@ -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
};
26 changes: 15 additions & 11 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions backend/models/Rating.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions backend/routes/RatingRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const { createRating } = require('../controllers/ratingController.js');


const router = express.Router();

router.post('/', createRating);

module.exports = router;
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3245,4 +3245,4 @@ <h2>Exclusive Deals and Offers!</h2>

</body>

</html>
</html>

0 comments on commit d0aa3cf

Please sign in to comment.