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

Add review and get product reviews routes #9

Merged
merged 1 commit into from
Jun 11, 2022
Merged
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
46 changes: 46 additions & 0 deletions controllers/reviewController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const Review = require("../models/reviewModel");
const JWT = require("../utilities/jwt");
const { verifyAuthorization } = require("../utilities/verifyAuthorization");
const ReviewFactory = require("../utilities/ReviewFactory");

module.exports.addReview = async (req, res) => {
if (!verifyAuthorization(req.headers, ["NORMAL", "ADMIN"])) {
return res.status(403).json({
message: "Unauthorized",
});
}

let { uid } = JWT.verify(req.headers.authorization);

let review = ReviewFactory.createReview({ ...req.body, user: uid });

try {
await review.save();
res.status(200).json({
message: "Review added successfully",
data: review,
});
} catch (error) {
res.status(500).json({
message: "Something went while adding your review",
});
}
};

module.exports.getProductReviews = (req, res) => {
let productId = req.params.product_id;

Review.find({ product: productId })
.populate("user")
.exec((err, reviews) => {
if (err) {
return res.status(400).json({
message: "Something went wrong with getting the reviews",
});
}
return res.status(200).json({
message: "Retrieved reviews successfully",
data: reviews,
});
});
};
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let authRoutes = require("./routes/auth");
let productRoutes = require("./routes/prouductRoutes");
let userRoutes = require("./routes/users");
let orderRoutes = require("./routes/orderRoutes");
let reviewRoutes = require("./routes/reviewRoutes");

const app = express();

Expand All @@ -33,6 +34,7 @@ app.use("/", authRoutes);
app.use("/", userRoutes);
app.use("/products", productRoutes);
app.use("/orders", orderRoutes);
app.use("/reviews", reviewRoutes);

const mongo = mongoose.connect(process.env.DB_PATH, config.DB_OPTIONS);

Expand Down
11 changes: 11 additions & 0 deletions routes/reviewRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {
addReview,
getProductReviews,
} = require("../controllers/reviewController");

let router = require("express").Router();

router.post("/", addReview);
router.get("/:product_id", getProductReviews);

module.exports = router;
12 changes: 12 additions & 0 deletions utilities/ReviewFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Review = require("../models/reviewModel");
const { updateEntity } = require("./updateEntity");

class ReviewFactory {
static createReview(body) {
let review = new Review();
updateEntity(review, body);
return review;
}
}

module.exports = ReviewFactory;