Skip to content

Commit

Permalink
Add rate us
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Oct 25, 2024
1 parent d377142 commit 62bb5c9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions controller/Rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Rating from "../models/Rating.js";


// Function to submit rating
const submitRating = async (req, res) => {
const { rating, feedback } = req.body;

// Validate rating
if (!rating || rating < 1 || rating > 5) {
return res.status(400).json({ message: "Rating must be between 1 and 5" });
}

try {
const newRating = new Rating({
rating,
feedback,
});

await newRating.save();
res.status(201).json({ message: "Rating submitted successfully", data: newRating });
} catch (error) {
console.error("Rating submission error:", error);
res.status(500).json({ message: "Could not submit rating" });
}
};

export default submitRating
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5186,6 +5186,8 @@ <h3>Rate Us</h3>
<label for="star1" class="emoji-label">😍</label>
</div>



<div class="feedback-container">
<textarea id="feedback" placeholder="Enter your feedback..." rows="4"></textarea>
</div>
Expand All @@ -5195,6 +5197,25 @@ <h3>Rate Us</h3>
</div>
</div>

<script>
document.getElementById('submit-btn').addEventListener('click', async () => {
const rating = document.querySelector('input[name="rate"]:checked').value;
const feedback = document.getElementById('feedback').value;

const response = await fetch('http://localhost:4000/rate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ rating, feedback })
});

const data = await response.json();
alert(data.message);
});

</script>

<div id="toastMessage" class="toast-message"></div>

<script>
Expand Down
9 changes: 9 additions & 0 deletions models/Rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mongoose from'mongoose';

const RatingSchema = new mongoose.Schema({
rating: { type: Number, required: true, min: 1, max: 5 },
feedback: { type: String, maxlength: 500 },
}, { timestamps: true });

const Rating = mongoose.model('Rating', RatingSchema);
export default Rating;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ import { sendEmail } from './controller/subscribe.js';
import dotenv from 'dotenv';

import fetchBookController from "./controller/fetchBookController.js";
import submitRating from './controller/Rating.js';
dotenv.config();

const app = express();
Expand Down Expand Up @@ -308,6 +309,7 @@ dbConnect().then(() => {
});

app.use("/api" , fetchBookController);
app.post('/rate', submitRating);

const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
Expand Down

0 comments on commit 62bb5c9

Please sign in to comment.