import products from "../models/products.js";

export const addProduct = async (req, res) => {
  try {
    const { ProductCode, ProductName, Category, QuantityInStock, UnitPrice, SupplierName } = req.body;
    const productExists = await products.findOne({ ProductCode });
    if (productExists) {
      return res.status(400).json({ message: 'product already inserted' });
    }
    await new products({ ProductCode, ProductName, Category, QuantityInStock, UnitPrice, SupplierName }).save();
    res.status(201).json({ message: 'new product added successfully' });
  } catch (error) {
    res.status(500).json({ message: 'internal server error', error: error.message });
  }
};

export const getAllProducts = async (req, res) => {
  try {
    const result = await products.find();
    res.status(200).json(result);
  } catch (error) {
    res.status(500).json({ message: 'internal server error', error: error.message });
  }
};