-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reviews_BE #66
base: main
Are you sure you want to change the base?
Reviews_BE #66
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,29 +8,32 @@ const Review = require("../models/Review"); | |
// @Given parameters passed in, create a review. | ||
exports.createReview = asyncHandler(async (req, res, next) => { | ||
const userId = req.user.id; | ||
const profileId = req.params.id; | ||
const reviewData = req.body; | ||
|
||
|
||
if (!ObjectId(userId)) { | ||
return res.status(400).send(Error("User id is invalid")); | ||
} | ||
|
||
if (userId === req.body.userId) { | ||
return res.status(401).json({ | ||
error: "User is not authorized for reviewing their own profile" | ||
}); | ||
} | ||
|
||
try { | ||
const user = await User.findById( | ||
userId | ||
).populate("profile"); | ||
const sitterProfile = await Profile.findById(profileId); | ||
const newReview = new Review({ | ||
...reviewData, | ||
user: { | ||
firstName: user.profile.firstName, | ||
lastName: user.profile.lastName, | ||
profileImg: user.profile.profileImg, | ||
}, | ||
const newReview = new Review(req.body); | ||
newReview.save((error, review) => { | ||
if (error) { | ||
return res.status(400).json({ | ||
error: "Error in saving the review", | ||
message: error.message | ||
}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: "Review is submitted successfully." | ||
}) | ||
|
||
}); | ||
sitterProfile.review.push(newReview.id); | ||
await newReview.save(); | ||
await sitterProfile.save(); | ||
res.status(200).json({ | ||
success: { | ||
review: newReview, | ||
|
@@ -42,6 +45,26 @@ exports.createReview = asyncHandler(async (req, res, next) => { | |
} | ||
}); | ||
|
||
exports.getAvgUserReview = asyncHandler(async (req, res, next) => { | ||
Review.aggregate( | ||
[ | ||
{ $group: { _id: "$userId", avgRating: { $avg: "$rating" } } } | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is getting the average rating of all users, this is not the best approach. I don't think having a specific route for this is ideal, would be better to have a way to populate this value in the profile when fetching the profile |
||
).exec((error, reviews) => { | ||
if (error) { | ||
return res.status(400).json({ | ||
error: "error in getting reviews", | ||
message: error.message | ||
}) | ||
} | ||
|
||
return res.status(200).json({ | ||
success: "Retrieved successfully", | ||
reviews | ||
}) | ||
}) | ||
}) | ||
|
||
// @route GET /review/:id | ||
// @Given profile id, get all reviews | ||
exports.getAllReviews = asyncHandler(async (req, res, next) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const reviewSchema = new mongoose.Schema( | ||
{ | ||
user: { | ||
firstName: String, | ||
lastName: String, | ||
profileImg: String, | ||
}, | ||
rating: { | ||
type: Number, | ||
min: 1, | ||
max: 5, | ||
required: true, | ||
}, | ||
message: String, | ||
const { Schema } = mongoose; | ||
const { ObjectId } = Schema; | ||
|
||
const reviewSchema = new mongoose.Schema({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a field for a written review and not require it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I do that |
||
reviewerUserId: { | ||
type: ObjectId, | ||
ref: "user", | ||
unique: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means a user can only write one review ever, if you're trying to prevent having multiple reviews of and by the same user you could either do that in the controller or have a separate unique value that combines the two ids There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. |
||
requried: true | ||
}, | ||
userId: { | ||
type: ObjectId, | ||
ref: "user", | ||
unique: true, | ||
required: true | ||
}, | ||
rating: { | ||
type: Number, | ||
min: 1, | ||
max: 5, | ||
required: true, | ||
}, | ||
{ | ||
timestamps: true, | ||
requestId: { | ||
type: ObjectId, | ||
ref: "request", | ||
required: true | ||
} | ||
); | ||
},{ timestamps: true }); | ||
|
||
module.exports = Review = mongoose.model("review", reviewSchema); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const protect = require('../middleware/auth'); | ||
const { createReview, getAvgUserReview } = require('../controllers/review'); | ||
|
||
router.post("/create", protect, createReview); | ||
router.get("/avg", protect, getAvgUserReview); | ||
|
||
module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍