6 changed files with 308 additions and 0 deletions
@ -0,0 +1,51 @@ |
|||||
|
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.' }); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,82 @@ |
|||||
|
const { pool } = require('../config/databases'); |
||||
|
|
||||
|
exports.createEmplacement = async (req, res) => { |
||||
|
const { name, type, temperature, capacity } = req.body; |
||||
|
|
||||
|
if (!name || !type) { |
||||
|
return res.status(400).json({ message: 'Name and type are required.' }); |
||||
|
} else if (!temperature) { |
||||
|
return res.status(400).json({ message: 'Temperature is required.' }); |
||||
|
} else if (!capacity) { |
||||
|
return res.status(400).json({ message: 'Capacity is required.' }); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
const [result] = await pool.query('INSERT INTO emplacements (name, type, temperature, capacity) VALUES (?, ?, ?, ?)', [name, type, temperature, capacity]); |
||||
|
|
||||
|
res.status(201).json({ |
||||
|
message: 'Emplacement created successfully', |
||||
|
emplacement: { |
||||
|
id: result.insertId, |
||||
|
name, |
||||
|
type, |
||||
|
temperature, |
||||
|
capacity, |
||||
|
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 emplacement.' }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.getEmplacements = async (req, res) => { |
||||
|
try { |
||||
|
const [rows] = await pool.query('SELECT * FROM emplacements'); |
||||
|
res.json(rows); |
||||
|
} catch (error) { |
||||
|
console.error(error); |
||||
|
res.status(500).json({ message: 'Server error while fetching emplacements.' }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.getEmplacementById = async (req, res) => { |
||||
|
const { id } = req.params; |
||||
|
|
||||
|
try { |
||||
|
const [rows] = await pool.query('SELECT * FROM emplacements WHERE id = ?', [id]); |
||||
|
|
||||
|
if (rows.length === 0) { |
||||
|
return res.status(404).json({ message: 'Emplacement not found.' }); |
||||
|
} |
||||
|
|
||||
|
res.json(rows[0]); |
||||
|
} catch (error) { |
||||
|
console.error(error); |
||||
|
res.status(500).json({ message: 'Server error while fetching emplacement.' }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.updateEmplacement = async (req, res) => { |
||||
|
const { id } = req.params; |
||||
|
const { name, type, temperature, capacity } = req.body; |
||||
|
|
||||
|
if (!name || !type || !temperature || !capacity) { |
||||
|
return res.status(400).json({ message: 'All fields are required.' }); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
const [result] = await pool.query('UPDATE emplacements SET name = ?, type = ?, temperature = ?, capacity = ? WHERE id = ?', [name, type, temperature, capacity, id]); |
||||
|
|
||||
|
if (result.affectedRows === 0) { |
||||
|
return res.status(404).json({ message: 'Emplacement not found.' }); |
||||
|
} |
||||
|
|
||||
|
res.json({ message: 'Emplacement updated successfully' }); |
||||
|
} catch (error) { |
||||
|
console.error(error); |
||||
|
res.status(500).json({ message: 'Server error while updating emplacement.' }); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
const { pool } = require('../config/databases'); |
||||
|
|
||||
|
exports.createFournisseur = async (req, res) => { |
||||
|
const { name, category, contact_person, phone, email, status } = req.body; |
||||
|
|
||||
|
if (!name || !category || !contact_person || !phone || !email || !status) { |
||||
|
return res.status(400).json({ message: 'All fields are required.' }); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
const [result] = await pool.query('INSERT INTO fournisseurs (name, category, contact_person, phone, email, status) VALUES(?, ?, ?, ?, ?, ?)', [name, category, contact_person, phone, email, status]); |
||||
|
|
||||
|
res.status(201).json({ |
||||
|
message: 'Fournisseur created successfully', |
||||
|
fournisseur: { |
||||
|
id: result.insertId, |
||||
|
name, |
||||
|
category, |
||||
|
contact_person, |
||||
|
phone, |
||||
|
email, |
||||
|
status, |
||||
|
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.getFournisseurs = async (req, res) => { |
||||
|
try { |
||||
|
const [rows] = await pool.query('SELECT * FROM fournisseurs'); |
||||
|
res.json(rows); |
||||
|
} catch (error) { |
||||
|
console.error(error); |
||||
|
res.status(500).json({ message: 'Server error while fetching fournisseurs.' }); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
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.' }); |
||||
|
} |
||||
|
} |
||||
@ -1,12 +1,26 @@ |
|||||
const express = require('express'); |
const express = require('express'); |
||||
const authMiddleware = require('../middleware/authMiddleware'); |
const authMiddleware = require('../middleware/authMiddleware'); |
||||
const userController = require('../controllers/UserController'); |
const userController = require('../controllers/UserController'); |
||||
|
const emplacementController = require('../controllers/EmplacementController'); |
||||
|
const compartimentController = require('../controllers/CompartimentController'); |
||||
|
const stockController = require('../controllers/StockController'); |
||||
|
const fournisseurController = require('../controllers/FournisseurController'); |
||||
|
|
||||
const router = express.Router(); |
const router = express.Router(); |
||||
|
|
||||
router.get('/profile', authMiddleware(), userController.getProfile); |
router.get('/profile', authMiddleware(), userController.getProfile); |
||||
router.get('/admin', authMiddleware('admin'), userController.getAdminPage); |
router.get('/admin', authMiddleware('admin'), userController.getAdminPage); |
||||
router.post('/create', authMiddleware('admin'), userController.createUser); |
router.post('/create', authMiddleware('admin'), userController.createUser); |
||||
|
router.post('/create/emplacement', authMiddleware(), emplacementController.createEmplacement); |
||||
|
router.get('/emplacements', authMiddleware(), emplacementController.getEmplacements); |
||||
|
router.get('/emplacement/:id', authMiddleware(), emplacementController.getEmplacementById); |
||||
|
router.get('/compartiments', authMiddleware(), compartimentController.getCompartiments); |
||||
|
router.post('/create/compartiment', authMiddleware(), compartimentController.createCompartiment); |
||||
|
router.post('/create/ingredient', authMiddleware(), stockController.createIngredient); |
||||
|
router.get('/ingredients/inventaire', authMiddleware(), stockController.getIngredientsInventaire); |
||||
|
router.get('/ingredients/emplacement', authMiddleware(), stockController.getIngredientsEmplacements); |
||||
|
router.post('/create/fournisseur', authMiddleware(), fournisseurController.createFournisseur); |
||||
|
router.get('/fournisseurs', authMiddleware(), fournisseurController.getFournisseurs); |
||||
|
|
||||
|
|
||||
module.exports = router; |
module.exports = router; |
||||
|
|||||
Loading…
Reference in new issue