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());
|
||||
|
||||
router.get('/products', productsController.allProducts)
|
||||
router.get('/product/:id', productsController.productById)
|
||||
router.get('/product/:slug', productsController.productBySlug)
|
||||
router.post('/product', productsController.addNewProduct)
|
||||
|
||||
router.post('/variation/:id', variationsController.addNewVariationToProduct);
|
||||
|
||||
@@ -24,17 +24,17 @@ const allProducts = (req, res) => {
|
||||
.then(products => res.json(products))
|
||||
}
|
||||
|
||||
const productById = (req, res) => {
|
||||
const { id } = req.params;
|
||||
const productBySlug = (req, res) => {
|
||||
const { slug } = req.params;
|
||||
|
||||
if (id != null) {
|
||||
return products.getById(id)
|
||||
.then(product => handleReturnProduct(product, res))
|
||||
if (slug != null) {
|
||||
return products.getBySlug(slug)
|
||||
.then(product => res.json({ ...product._doc }))
|
||||
.catch(err => handleError(err, res))
|
||||
} else {
|
||||
return res.status(422).send({
|
||||
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 = {
|
||||
allProducts,
|
||||
productById,
|
||||
productBySlug,
|
||||
addNewProduct
|
||||
};
|
||||
|
||||
@@ -29,8 +29,8 @@ class Products {
|
||||
return Product.find().populate('variations');
|
||||
}
|
||||
|
||||
getById(id) {
|
||||
return Product.findById(id).populate('variations');
|
||||
getBySlug(slug) {
|
||||
return Product.findOne({ urlSlug: slug }).populate('variations');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user