const Student = require('../models/Student')




exports.addstudent = async (req,res) => {
    try {
     const newstudent= new Student(req.body)
     await newstudent.save()
     res.status(200).json({msg:"Student added successful"})
    } catch (error) {
       res.status(500).json({msg:"Failed to add student",error:error.message}) 
    }
}


exports.getstudentbyid= async (req,res) =>{
try{
    const singlestudent = await Student.findById(req.params.id) 

    if(!singlestudent) return res.status(404).json({msg:"Student not found"})

    res.status(200).json(singlestudent)
}catch{
res.status(500).json({msg:"failed to fetch"})
}
}

exports.getstudent= async (req,res) => {
    try {
        const getstudent= await Student.find()
        res.status(200).json(getstudent)
    } catch (error) {
        res.status(500).json({error:error.message})
    }
}

exports.updatestudent = async(req,res)=>{
    try {
        const updatestudent = await Student.findByIdAndUpdate(
            req.params.id,
            req.body,
            { new: true }
        )

       if(!updatestudent) return res.status(404).json({msg:"Student not found"})

        res.status(200).json({msg:"updated successfully"})
    } catch (error) {
        res.status(500).json({msg:"Failed to update",error:error.message})
    }
}

exports.deletestudent = async(req,res) =>{
    try {
        const deletestudent = await Student.findByIdAndDelete(
            req.params.id
        )

        if(!deletestudent) return res.status(404).json({msg:"Student not found"})

        res.status(200).json({msg:"Deleted successfully"})
    } catch (error) {
        res.status(500).json({msg:"Failed to delete",error:error.message})
    }
}
