Done Capstone 2
parent
4b003da8da
commit
a7656ebf9e
@ -0,0 +1,72 @@
|
||||
// userController.js
|
||||
const User = require('../model/Cart');
|
||||
|
||||
exports.addToCart = async (req, res) => {
|
||||
try {
|
||||
const { userId, productId, quantity } = req.body;
|
||||
|
||||
// Check if the user exists
|
||||
const user = await User.findById(userId);
|
||||
if (!user) {
|
||||
return res.status(404).json({ message: 'User not found.' });
|
||||
}
|
||||
|
||||
// Check if the product exists
|
||||
const product = await Product.findById(productId);
|
||||
if (!product) {
|
||||
return res.status(404).json({ message: 'Product not found.' });
|
||||
}
|
||||
|
||||
// Check if the product is active
|
||||
if (!product.isActive) {
|
||||
return res.status(400).json({ message: 'Product is not active.' });
|
||||
}
|
||||
|
||||
// Check if the quantity is valid
|
||||
if (quantity <= 0) {
|
||||
return res.status(400).json({ message: 'Invalid quantity.' });
|
||||
}
|
||||
|
||||
// Check if the user already has a cart
|
||||
let cart = await Cart.findOne({ userId });
|
||||
|
||||
// If the user doesn't have a cart, create a new one
|
||||
if (!cart) {
|
||||
cart = new Cart({ userId, products: [], totalAmount: 0 });
|
||||
}
|
||||
|
||||
// Check if the product is already in the cart
|
||||
const existingProduct = cart.products.find(
|
||||
(cartProduct) => cartProduct.productId.toString() === productId
|
||||
);
|
||||
|
||||
// If the product is already in the cart, update the quantity and subtotal
|
||||
if (existingProduct) {
|
||||
existingProduct.quantity += quantity;
|
||||
existingProduct.subtotal = existingProduct.quantity * existingProduct.price;
|
||||
} else {
|
||||
// If the product is not in the cart, add it
|
||||
cart.products.push({
|
||||
productId,
|
||||
productName: product.name,
|
||||
quantity,
|
||||
price: product.price,
|
||||
subtotal: quantity * product.price,
|
||||
});
|
||||
}
|
||||
|
||||
// Update the total amount in the cart
|
||||
cart.totalAmount = cart.products.reduce(
|
||||
(total, product) => total + product.subtotal,
|
||||
0
|
||||
);
|
||||
|
||||
// Save the updated cart
|
||||
await cart.save();
|
||||
|
||||
return res.status(200).json({ message: 'Product added to cart successfully.' });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(500).json({ message: 'Internal server error.' });
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const cartProductSchema = new mongoose.Schema({
|
||||
productId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Product',
|
||||
required: true,
|
||||
},
|
||||
productName: { type: String, required: true },
|
||||
quantity: { type: Number, required: true },
|
||||
price: { type: Number, required: true },
|
||||
subtotal: { type: Number, required: true },
|
||||
});
|
||||
|
||||
const cartSchema = new mongoose.Schema({
|
||||
userId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'User',
|
||||
required: true,
|
||||
},
|
||||
products: [cartProductSchema],
|
||||
totalAmount: { type: Number, required: true },
|
||||
});
|
||||
|
||||
const Cart = mongoose.model('Cart', cartSchema);
|
||||
|
||||
module.exports = Cart;
|
@ -0,0 +1,10 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const cart = require('../controllers/cart');
|
||||
const auth = require("../auth")
|
||||
|
||||
const { authenticateToken, } = auth
|
||||
// Add route to handle adding a product to the cart
|
||||
router.post('/add-to-cart', authenticateToken, cart.addToCart);
|
||||
|
||||
module.exports = router;
|
Loading…
Reference in New Issue