You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
634 B
JavaScript
31 lines
634 B
JavaScript
const User = require("../model/User")
|
|
|
|
exports.createOrder = async (req, res) => {
|
|
try {
|
|
const { userId, products, totalAmount } = req.body
|
|
|
|
const user = await User.findById(userId)
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ message: "User not found" })
|
|
}
|
|
|
|
const newOrder = {
|
|
products: products,
|
|
totalAmount: totalAmount,
|
|
purchaseOn: Date.now(),
|
|
}
|
|
|
|
user.orderedProducts.push(newOrder)
|
|
await user.save()
|
|
|
|
res.status(201).json({
|
|
message: "Order created successfully",
|
|
order: newOrder,
|
|
})
|
|
} catch (error) {
|
|
console.error(error)
|
|
res.status(500).json({ message: "Internal Server Error" })
|
|
}
|
|
}
|