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.
 

58 lines
2.8 KiB

const { pool } = require('../config/databases');
exports.createIngredient = async (req, res) => {
const { articles, quantity, uniter, price_unit, id_emplacement, id_compartiment } = req.body;
if (!articles || !quantity || !uniter || !price_unit) {
return res.status(400).json({ message: 'All fields are required.' });
}
// verify if compartiment exists
try {
const [compartiment] = await pool.query('SELECT * FROM compartiments WHERE id = ?', [id_compartiment]);
if (compartiment.length === 0) {
return res.status(404).json({ message: 'Compartiment not found.' });
}
// Insert ingredient
const [result] = await pool.query('INSERT INTO stocks (articles, quantity, uniter, price_unit, id_emplacement, id_compartiment) VALUES (?, ?, ?, ?, ?, ?)', [articles, quantity, uniter, price_unit, id_emplacement, id_compartiment]);
res.status(201).json({
message: 'Ingredient created successfully',
ingredient: {
id: result.insertId,
articles,
quantity,
uniter,
price_unit,
id_compartiment,
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 ingredient.' });
}
}
exports.getIngredientsInventaire = async (req, res) => {
try {
const [rows] = await pool.query('SELECT emplacements.name, stocks.id as stock_id, stocks.articles, stocks.uniter, stocks.quantity, stocks.id_emplacement, stocks.id_compartiment FROM emplacements JOIN stocks ON emplacements.id = stocks.id_emplacement');
res.json(rows);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error while fetching ingredients.' });
}
}
exports.getIngredientsEmplacements = async (req, res) => {
try {
const [rows] = await pool.query('SELECT emplacements.name AS emplacement_name, emplacements.id AS emplacement_id, emplacements.type AS emplacement_type, emplacements.temperature, emplacements.capacity AS emplacement_capacity, compartiments.id AS compartiment_id, compartiments.name AS compartiment_name, compartiments.capacity AS compartiment_capacity, compartiments.uniter AS compartiment_uniter, stocks.id AS stock_id, stocks.articles, stocks.quantity, stocks.uniter AS stock_uniter, stocks.price_unit FROM emplacements JOIN compartiments ON emplacements.id = compartiments.id_emplacement JOIN stocks ON compartiments.id = stocks.id_compartiment');
res.json(rows);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error while fetching ingredients.' });
}
}