-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
301 additions
and
0 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
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
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,29 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const reviewSchema = new mongoose.Schema({ | ||
user_id: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true | ||
}, | ||
product_id: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Product', | ||
required: true | ||
}, | ||
rating: { | ||
type: Number, | ||
required: true, | ||
min: 1, | ||
max: 5 | ||
}, | ||
review_text: { | ||
type: String, | ||
required: true | ||
} | ||
}, { | ||
timestamps: true | ||
}); | ||
|
||
const Review = mongoose.model('Review', reviewSchema); | ||
module.exports = Review; |
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,22 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const { auth, studentAuth } = require('../middlewares/auth'); | ||
const Review = require('../models/review'); | ||
|
||
// POST /api/v1/reviews - Create a review | ||
router.post('/reviews', auth, studentAuth, async (req, res) => { | ||
try { | ||
const { product_id, rating, review_text } = req.body; | ||
const user_id = req.user.id; // Assuming req.user contains the authenticated user's details | ||
|
||
const newReview = new Review({ user_id, product_id, rating, review_text }); | ||
await newReview.save(); | ||
|
||
res.status(201).json({ success: true, message: 'Review created successfully' }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ success: false, message: 'Error creating review' }); | ||
} | ||
}); | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { useState } from 'react'; | ||
|
||
const AddReview = ({ productId }) => { | ||
const [rating, setRating] = useState(1); | ||
const [reviewText, setReviewText] = useState(''); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
const response = await fetch('/reviews', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${yourAuthToken}` // Replace with your auth token logic | ||
}, | ||
body: JSON.stringify({ product_id: productId, rating, review_text: reviewText }) | ||
}); | ||
|
||
if (response.ok) { | ||
// Handle success (e.g., refresh reviews) | ||
} else { | ||
// Handle error | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<label> | ||
Rating: | ||
<select value={rating} onChange={(e) => setRating(e.target.value)}> | ||
{[1, 2, 3, 4, 5].map(n => <option key={n} value={n}>{n}</option>)} | ||
</select> | ||
</label> | ||
<label> | ||
Review: | ||
<textarea value={reviewText} onChange={(e) => setReviewText(e.target.value)} /> | ||
</label> | ||
<button type="submit">Submit</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default AddReview; |
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,34 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
const Reviews = ({ productId }) => { | ||
const [reviews, setReviews] = useState([]); | ||
const [rating, setRating] = useState(0); | ||
|
||
useEffect(() => { | ||
fetch(`/reviews/${productId}`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
setReviews(data); | ||
if (data.length > 0) { | ||
const avgRating = data.reduce((acc, review) => acc + review.rating, 0) / data.length; | ||
setRating(avgRating); | ||
} | ||
}); | ||
}, [productId]); | ||
|
||
return ( | ||
<div> | ||
<h2>Average Rating: {rating.toFixed(1)} / 5</h2> | ||
<ul> | ||
{reviews.map(review => ( | ||
<li key={review.review_id}> | ||
<p><strong>Rating:</strong> {review.rating} / 5</p> | ||
<p>{review.review_text}</p> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Reviews; |
Oops, something went wrong.