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

feat: add to cart api added #938

Merged
merged 14 commits into from
Aug 10, 2023
25 changes: 25 additions & 0 deletions server/routes/shop/Products.js
Copy link
Member

Choose a reason for hiding this comment

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

Also there are instances of Missing semicolon. please do fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you getting Errors ?

Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,29 @@ router.get("/:productSlug", async (req, res) => {
}
});

// Route 4 - Add to Cart
/**
* @description Add a product to the cart
* @route POST /cart/add
* @access Public
* @requires email (string)
* @requires productId (string)
* @returns Updated cart object (JSON)
*/
router.post("/cart/add", async (req, res) => {
try{
const {email,productId} = req.body;
const response = await User.updateOne({email:email},{$push:{cart:{id:productId}}})
Copy link
Member

Choose a reason for hiding this comment

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

Have you defined the User before ? It says User not defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const User = require("../../schema/user/UserSchema");
on top it is already defined it is working fine (No New Errors)

if(response.modifiedCount === 1){
return res.send('Product added successfully');
}else{
res.status(404).json({message:"User not Found"})
}
}
catch(err){
res.status(500).json({ message: "Failed to add product to cart" });
console.log(err)
}
});

module.exports = router;
3 changes: 3 additions & 0 deletions server/schema/shop/ProductSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const mongoose = require("mongoose");

const ProductSchema = new mongoose.Schema(
{
id:{
type:String,
},
productType: {
type: String,
required: true,
Expand Down
1 change: 1 addition & 0 deletions server/schema/user/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const UserSchema = mongoose.Schema({
type: String,
required: false,
},
cart:[{id:{type:String}}]
PatilHarshh marked this conversation as resolved.
Show resolved Hide resolved
});

module.exports = mongoose.model("user", UserSchema);
Loading