Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messageRouter = require("./routes/message");
const requestRouter = require("./routes/request");
const notificationRouter = require("./routes/notification");
const profileRouter = require("./routes/profile");
const reviewRouter = require("./routes/review");

const { json, urlencoded } = express;

Expand Down Expand Up @@ -45,6 +46,7 @@ app.use("/message", messageRouter);
app.use("/request", requestRouter);
app.use("/notification", notificationRouter);
app.use("/profile", profileRouter);
app.use("/review", reviewRouter);

if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "/client/build")));
Expand Down
57 changes: 40 additions & 17 deletions server/controllers/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
}
Comment on lines +15 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍


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,
Expand All @@ -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" } } }
]
Copy link
Contributor

Choose a reason for hiding this comment

The 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) => {
Expand Down
43 changes: 26 additions & 17 deletions server/models/Review.js
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({
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I do that

reviewerUserId: {
type: ObjectId,
ref: "user",
unique: true,
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
10 changes: 10 additions & 0 deletions server/routes/review.js
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;