Inspiré de Stephen Grider Microservice with Node JS and React
Les schémas utilisées dans ce workshop sont disponible sous forme zip forme de fichier zip. Télécharger ici
Téléchargez le fichier et décompressez-le quelque part sur votre ordinateur.
Vous pouverez ici tout les petits bout de code de l'application
const dotenv = require('dotenv');
dotenv.config()
const config = {
INVENTORY_SERVICE: process.env.INVENTORY_SERVICE_URL,
PAYEMENT_SERVICE: process.env.PAYEMENT_SERVICE_URL,
}
module.exports = config;
export default App;
router.post('/api/order', async (req, res) => {
try {
// Step1: Check Inventory
const { productId, quantity, id, amount, userId, orderDate, status } = req.body
const responseInventory = await axios.get(`${config.INVENTORY_SERVICE}/api/inventory/${productId}`);
if (responseInventory.data.availableQuantity < quantity) {
return res.status(400).send({ message: "Insufficient stock" });
}
// // step2: Process payment
// const responsePayement = await axios.get(`${config.PAYEMENT_SERVICE}/api/payment`, {
// "orderId": id,
// "amount": amount,
// "userId": userId
// });
// if (!responsePayement.status == '201') {
// return res.status(402).send({ message: "Echec du payement " });
// }
// step3: Create Order
const order = new Order(req.body);
await order.save();
//step4: Update Inventory
await axios.put(`${config.INVENTORY_SERVICE}/api/inventory/${productId}`, {
quantity: -quantity
});
res.status(201).send(order);
} catch (error) {
console.log(error)
res.status(500).error
}
});
PRODUCT_SERVICE_URL=http://localhost:3000
INVENTORY_SERVICE_URL=http://localhost:3002
PAYEMENT_SERVICE_URL=http://localhost:3003
ORDER_SERVICE_URL=http://localhost:3003
PAYEMENT_SERVICE_URL=http://localhost:3003