import SparePart from "../models/SparePartModel.js";

export const createSparePart = async (req, res) => {
    try{
        const { name,  category, quantity, unitPrice}= req.body;

        if(!name || !category || !quantity || !unitPrice ){
            return res.status(400).json({message:"Fill out all fields"});
        }

        const totalPrice = quantity * unitPrice;

        const sparePart = await SparePart.create({
            name,
            category,
            quantity,
            unitPrice,
            totalPrice
        });

        res.status(201).json(sparePart);

    }catch (err){
        res.status(500).json({message: err.message});
    }
};

// GET ALL SPARE PARTS
export const getSpareParts = async (req, res) => {
    try {
      const parts = await SparePart.find();
      res.status(200).json(parts);
    } catch (err) {
      res.status(500).json({ message: err.message });
    }
  };

