Im about to create the Add-to-cart feature to my project but I have some questions first. Do I need a Cart schema? Because I don't think I would store products to a cart Schema.
I started with this, creating a cart into user.
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
reputation: Number,
cart: {
items: [{
productId: {
type: mongoose.Types.ObjectId,
ref: 'Product',
required: true
},
qty: {
type: Number,
required: true
}
}],
totalPrice: Number
}
})
My User controller, of course empty.
module.exports.renderCart = (req, res) => {
const product = req.body.product;
// console.log(req.body.product)
res.render('users/cart', { product })
}
It is okay to start with is? Also I want the cart session end at some point. Of course I need to create maybe a cart Class with methods like Add() remove() and so on. How can I do that and use it in the user controller?? Thank you in advance!