Skip to content

Commit

Permalink
Comment function
Browse files Browse the repository at this point in the history
  • Loading branch information
DUY PHAM committed Jul 7, 2021
1 parent 4774b83 commit bbd29b1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
10 changes: 0 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ passport.use(
)
);

app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Origin,X-Requested-With,Content-Type,Accept,content-type,application/json"
);
next();
});
//routes
app.get(
"/auth/facebook",
Expand Down
43 changes: 43 additions & 0 deletions controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
categoriesToQuery,
} = require("./functions");
const { Order } = require("../models/order");
const { Comment } = require("../models/comment");

cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
Expand Down Expand Up @@ -200,6 +201,20 @@ exports.getItemBySellerId = async function getItemBySellerId(req, res) {
}
} catch (error) {}
};
exports.getComment = async function getComment(req, res) {
try {
// ??id product
const { id } = await req.params;
if (!id) throw new Error(`Missing id product value 😢`);
const cmts = await Comment.find({ productId: id });
res.status(200);
res.send(cmts);
} catch (error) {
console.log(error);
res.status(200);
res.send({ error: error.message });
}
};

//POST
exports.uploadImage = async function uploadImage(req, res) {
Expand Down Expand Up @@ -228,6 +243,34 @@ exports.uploadImage = async function uploadImage(req, res) {
res.send({ error });
}
};
exports.createComment = async function createComment(req, res) {
try {
const { id } = await req.params;
const { userId } = await req.user;
const { content, media } = await req.body;
console.log(content);
if (!content) throw new Error(`Content cannot blank`);
if (!id) throw new Error(`Id product not found ⚠️`);
const user = await User.findById(userId);
const newCmt = {
productId: id,
user,
content,
media: media || [],
};
const cmt = new Comment(newCmt);
cmt.save().then((rs) => {
if (rs) {
res.status(200);
res.send({ success: `Created success 😃` });
}
});
} catch (error) {
res.status(500);
console.log(error);
res.send({ error: error.message });
}
};
exports.destroyImage = async function destroyImage(req, res) {
try {
// const { userId } = await req.user;
Expand Down
22 changes: 22 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require("mongoose");
const { userSchema } = require("./user");
const Schema = mongoose.Schema;

const commentSchema = new Schema(
{
productId: {
type: String,
required: true,
},
user: userSchema,
content: String,
media: [],
},
{ timestamps: true }
);
let Comment = mongoose.model("Comments", commentSchema);

module.exports = {
commentSchema,
Comment,
};
6 changes: 6 additions & 0 deletions routes/productRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ router.get(
router.get("/seller/:id", jwtAuth, productController.getBySellerId);
router.get("/in-cart", jwtAuth, productController.getItemInCart);
router.get("/item-seller/:id", productController.getItemBySellerId);

//Comment
router.get("/comment/:id", productController.getComment);

//POST
//handle images with different endpoints
//response id img to client when they send file uploads
Expand All @@ -53,6 +57,8 @@ router.post(
res.send({ error: err.message });
}
);
//Comment
router.post("/comment/:id", jwtAuth, productController.createComment);
//endpoint destroy image on cloud when client edit
router.post("/image/destroy/:id", productController.destroyImage);

Expand Down

0 comments on commit bbd29b1

Please sign in to comment.