From 1c552cbf2e310b29e4c2dc914e1c9563b884cd4a Mon Sep 17 00:00:00 2001 From: ShikharGoel04 <48277817+ShikharGoel04@users.noreply.github.com> Date: Fri, 2 Oct 2020 11:04:17 +0530 Subject: [PATCH] Added routes to productRoute.js I have added routes to delete all products with admin access. Added route for the user to get all the reviews for specific product. --- backend/routes/productRoute.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/routes/productRoute.js b/backend/routes/productRoute.js index 8bc2799..6dc0c2f 100644 --- a/backend/routes/productRoute.js +++ b/backend/routes/productRoute.js @@ -24,6 +24,13 @@ router.get('/', async (req, res) => { ); res.send(products); }); +router.delete('/',isAuth,isAdmin,async(req,res) => { + const product = await Product.remove({}); + res.status(200).send({ + data: product, + message: 'All the products are deleted successfully' + }); +}); router.get('/:id', async (req, res) => { const product = await Product.findOne({ _id: req.params.id }); @@ -55,6 +62,18 @@ router.post('/:id/reviews', isAuth, async (req, res) => { res.status(404).send({ message: 'Product Not Found' }); } }); +router.get('/:id/reviews', isAuth, async (req, res) => { + const product = await Product.findById(req.params.id); + if (product) { + res.status(200).send({ + data: product.reviews, + message: 'Reviews for the product with product id :' + req.params.id + }); + } else { + res.status(404).send({ message: 'Product Not Found' }); + } +}); + router.put('/:id', isAuth, isAdmin, async (req, res) => { const productId = req.params.id; const product = await Product.findById(productId); @@ -86,6 +105,7 @@ router.delete('/:id', isAuth, isAdmin, async (req, res) => { } }); + router.post('/', isAuth, isAdmin, async (req, res) => { const product = new Product({ name: req.body.name,