mirror of
				https://github.com/KevinMidboe/planetposen.git
				synced 2025-10-29 17:50:32 +00:00 
			
		
		
		
	Use slug for searching specific product.
This commit is contained in:
		| @@ -19,7 +19,7 @@ app.use(express.json()); | |||||||
| // app.use(express.urlencoded()); | // app.use(express.urlencoded()); | ||||||
|  |  | ||||||
| router.get('/products', productsController.allProducts) | router.get('/products', productsController.allProducts) | ||||||
| router.get('/product/:id', productsController.productById) | router.get('/product/:slug', productsController.productBySlug) | ||||||
| router.post('/product', productsController.addNewProduct) | router.post('/product', productsController.addNewProduct) | ||||||
|  |  | ||||||
| router.post('/variation/:id', variationsController.addNewVariationToProduct); | router.post('/variation/:id', variationsController.addNewVariationToProduct); | ||||||
|   | |||||||
| @@ -24,17 +24,17 @@ const allProducts = (req, res) => { | |||||||
|     .then(products => res.json(products)) |     .then(products => res.json(products)) | ||||||
| } | } | ||||||
|  |  | ||||||
| const productById = (req, res) => { | const productBySlug = (req, res) => { | ||||||
|   const { id } = req.params; |   const { slug } = req.params; | ||||||
|  |  | ||||||
|   if (id != null) { |   if (slug != null) { | ||||||
|     return products.getById(id) |     return products.getBySlug(slug) | ||||||
|       .then(product => handleReturnProduct(product, res)) |       .then(product => res.json({ ...product._doc })) | ||||||
|       .catch(err => handleError(err, res)) |       .catch(err => handleError(err, res)) | ||||||
|   } else { |   } else { | ||||||
|     return res.status(422).send({ |     return res.status(422).send({ | ||||||
|       success: true, |       success: true, | ||||||
|       message: 'Id must be number. Invalid request.' |       message: 'Product slug name must be included. Invalid request.' | ||||||
|     }) |     }) | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -48,6 +48,6 @@ const addNewProduct = (req, res) => { | |||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   allProducts, |   allProducts, | ||||||
|   productById, |   productBySlug, | ||||||
|   addNewProduct |   addNewProduct | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -29,8 +29,8 @@ class Products { | |||||||
|     return Product.find().populate('variations'); |     return Product.find().populate('variations'); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   getById(id) { |   getBySlug(slug) { | ||||||
|     return Product.findById(id).populate('variations'); |     return Product.findOne({ urlSlug: slug }).populate('variations'); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user