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 all commits
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
58 changes: 40 additions & 18 deletions server/controllers/review.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
const ObjectId = require("mongoose").Types.ObjectId;
const asyncHandler = require("express-async-handler");
const Profile = require("../models/Profile");
const User = require("../models/User");
const Review = require("../models/Review");

// @route POST /review/:id
// @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 +44,26 @@ exports.createReview = asyncHandler(async (req, res, next) => {
}
});

exports.getAvgUserReview = asyncHandler(async (req, res, next) => {
Review.aggregate(
[
{ $match: { userId: req.user.id } },
{ $group: { _id: "$userId", avgRating: { $avg: "$rating" } } }
]
).exec((error, reviews) => {
if (error) {
return res.status(400).json({
error: "error in getting reviews",
message: error.message
})
}

req.reviews = reviews;
next();

})
})

// @route GET /review/:id
// @Given profile id, get all reviews
exports.getAllReviews = asyncHandler(async (req, res, next) => {
Expand Down
44 changes: 26 additions & 18 deletions server/models/Review.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
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",
requried: true
},
userId: {
type: ObjectId,
ref: "user",
required: true
},
rating: {
type: Number,
min: 1,
max: 5,
required: true,
},
requestId: {
type: ObjectId,
ref: "request",
required: true
},
{
timestamps: true,
}
);
message: String
},{ timestamps: true });

module.exports = Review = mongoose.model("review", reviewSchema);
9 changes: 9 additions & 0 deletions server/routes/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();

const protect = require('../middleware/auth');
const { createReview } = require('../controllers/review');

router.post("/create", protect, createReview);

module.exports = router;