const db = require('../config/db');

exports.getAllCours = async (req, res) => {
    const [rows] = await db.query('SELECT * FROM cours');
    res.json(rows);
};

exports.getCoursById = async (req, res) => {
    const [rows] = await db.query('SELECT * FROM cours WHERE id = ?', [req.params.id]);
    if (rows.length === 0) return res.status(404).json({ error: 'Cours not found' });
    res.json(rows[0]);
};

exports.createCours = async (req, res) => {
    const { name, description } = req.body;
    await db.query('INSERT INTO cours (name, description) VALUES (?, ?)', [name, description]);
    res.status(201).json({ message: 'Cours created' });
};

exports.updateCours = async (req, res) => {
    const { name, description } = req.body;
    await db.query('UPDATE cours SET name = ?, description = ? WHERE id = ?', [name, description, req.params.id]);
    res.json({ message: 'Cours updated' });
};

exports.deleteCours = async (req, res) => {
    await db.query('DELETE FROM cours WHERE id = ?', [req.params.id]);
    res.status(204).send();
};
