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

export const createSale = async (req,res) => {
    try{
const sale = await Sale.create(req.body);
res.status(201).json(sale);
    }catch(error){
res.status(500).json({message:error.message});
    }
};

export const getAllSale = async (req,res) => {
    try{
const sale = await Sale.find()
.populate("user")
.populate("customer")
.populate("product")
res.status(200).json(sale);
    }catch(error){
res.status(500).json({message:error.message});
    }
};

export const getSale = async (req,res) => {
    try{
const sale = await Sale.findById(req.params.id);
res.status(200).json(sale);
    }catch(error){
res.status(500).json({message:error.message});
    }
};

export const updateSale = async (req,res) => {
    try{
const sale = await Sale.findByIdAndUpdate(req.params.id, req.body, {new:true});
res.status(200).json(sale);
    }catch(error){
res.status(500).json({message:error.message});
    }
};

export const deleteSale = async (req,res) => {
    try{
const sale = await Sale.findByIdAndDelete(req.params.id);
res.status(200).json (sale);
    }catch(error){
res.status(500).json({message:error.message});
    }
};