-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1665 from haseebzaki-07/new_branch_3
Add ratings
- Loading branch information
Showing
5 changed files
with
74 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3245,4 +3245,4 @@ <h2>Exclusive Deals and Offers!</h2> | |
|
||
</body> | ||
|
||
</html> | ||
</html> |