You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

51 lines
1.8 KiB

const { pool } = require('../config/databases');
exports.createCompartiment = async (req, res) => {
const {name, capacity, uniter, id_emplacement,} = req.body;
if (!name) {
return res.status(400).json({ message: 'Name are required.' });
} else if (!capacity) {
return res.status(400).json({ message: 'Capacity is required.' });
} else if (!uniter) {
return res.status(400).json({ message: 'Uniter is required.' });
}
// verify if emplacement exists
try {
const [emplacement] = await pool.query('SELECT * FROM emplacements WHERE id = ?', [id_emplacement]);
if (emplacement.length === 0) {
return res.status(404).json({ message: 'Emplacement not found.' });
}
// Insert compartiment
const [result] = await pool.query('INSERT INTO compartiments (name, capacity, uniter, id_emplacement) VALUES (?, ?, ?, ?)', [name, capacity, uniter, id_emplacement]);
res.status(201).json({
message: 'Compartiment created successfully',
compartiment: {
id: result.insertId,
name,
capacity,
uniter,
id_emplacement,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
}
});
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'Server error while creating compartiment.' });
}
}
exports.getCompartiments = async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM compartiments');
res.json(rows);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error while fetching compartiments.' });
}
}