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.

87 lines
2.3 KiB
JavaScript

const Product = require ("../models/Product")
const User = require("../models/User");
const Order = require("../models/Order");
const bcrypt = require("bcrypt");
// Auth
const auth = require("../auth");
module.exports.checkoutOrder = async (req, res) => {
try {
console.log(req.body);
const { userId, products, price, totalAmount, description } = req.body;
const newOrder = new Order({
userId,
products,
price,
totalAmount,
description
});
const savedOrder = await newOrder.save();
res.status(200).json({ message: 'Order created successfully!', order: savedOrder });
} catch (error) {
console.log('Error creating the order:', error)
res.status(500).json({ error: 'Error creating the order.' });
}
};
// checkoutOrder.getOrderDetails = (req, res) => {
// const orderId = req.params.orderId;
// Order.findById(orderId)
// .then(order => {
// if (!order) {
// return res.status(404).json({ error: 'Order not found.' });
// }
// res.status(200).json({ order });
// })
// .catch(error => {
// res.status(500).json({ error: 'Error fetching order details.' });
// });
// };
// checkoutOrder.updateOrder = (req, res) => {
// const orderId = req.params.orderId;
// const updateData = req.body;
// Order.findByIdAndUpdate(orderId, updateData, { new: true })
// .then(updatedOrder => {
// if (!updatedOrder) {
// return res.status(404).json({ error: 'Order not found.' });
// }
// res.status(200).json({ message: 'Order updated successfully!', order: updatedOrder });
// })
// .catch(error => {
// res.status(500).json({ error: 'Error updating the order.' });
// });
// };
exports.getAllOrders = async (req, res) => {
try {
const orders = await Order.find({});
res.json(orders);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
// Controller for retrieving order history
module.exports.getOrderHistory = async (req, res) => {
try {
const userId = req.params.userId;
// Fetch orders for the specified user
const orders = await Order.find({ userId });
res.status(200).json(orders);
} catch (error) {
console.error('Error fetching order history:', error);
res.status(500).json({ error: 'Error fetching order history.' });
}
};