From 418c1da9acd13dda2109135999cc3cefddd14f43 Mon Sep 17 00:00:00 2001 From: patrickjieraldjuan Date: Tue, 6 Feb 2024 21:38:18 +0800 Subject: [PATCH] Added router and controller for getProductName --- controllers/productControllers.js | 19 +++++++++++++++++++ routes/productRoutes.js | 3 +++ 2 files changed, 22 insertions(+) diff --git a/controllers/productControllers.js b/controllers/productControllers.js index bc892bf..df12e8f 100644 --- a/controllers/productControllers.js +++ b/controllers/productControllers.js @@ -87,6 +87,25 @@ module.exports.getAllProducts = async (req, res) => { } }; +// getting product name by id +exports.getProductName = async (req, res) => { + try { + const { productId } = req.body; + + // Find the product by productId + const product = await Product.findOne({ _id: productId }).select('name'); + + if (!product) { + return res.status(404).json({ error: 'Product not found' }); + } + + // Send the product name in the response + res.json(product); + } catch (error) { + console.error('Error getting product name:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } +}; // retrieve all active products controller exports.getAllActiveProducts = async (req, res) => { diff --git a/routes/productRoutes.js b/routes/productRoutes.js index 5935aae..c33afb3 100644 --- a/routes/productRoutes.js +++ b/routes/productRoutes.js @@ -38,6 +38,9 @@ router.post(`/create-order`, verify, verifyNonAdmin, productControllers.createOr // Route for getting the information of specific product router.get("/:productId", productControllers.getProduct); +// Route for getting product name by id +router.post('/getProductName', productControllers.getProductName); + // My Cart router.post('/add-to-cart', verify, verifyNonAdmin, productControllers.addProductToCart);