feat: Add 'getAllFiltered' in SERVICES and fix it in MODEL
getAllFiltered is a funtion to handle with query string requests
This commit is contained in:
parent
4ab023be48
commit
0d7e00420c
|
@ -10,6 +10,14 @@ router.get('/', rescue(async (_req, res) => {
|
|||
res.status(result.status).json({ products: [...result.productS] });
|
||||
}));
|
||||
|
||||
router.get('/search', rescue(async (req, res) => {
|
||||
const { name } = req.query;
|
||||
const result = await products.getAllFiltered(name);
|
||||
|
||||
res.status(result.status).json({ products: [...result.products],
|
||||
message: result.message });
|
||||
}));
|
||||
|
||||
router.get('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@ const getAllFiltered = async (query) => {
|
|||
const db = await connection();
|
||||
|
||||
const products = await db.collection('products')
|
||||
.find({ name: { $regex: `/${query}/i` } }).toArray();
|
||||
|
||||
.find({ name: { $regex: `${query}`, $options: 'i' } }).toArray();
|
||||
|
||||
return products;
|
||||
};
|
||||
|
||||
|
|
|
@ -50,10 +50,10 @@ const getAll = async () => {
|
|||
};
|
||||
|
||||
const getAllFiltered = async (query) => {
|
||||
const products = await models.getAll(query);
|
||||
|
||||
if (!products) {
|
||||
return {
|
||||
const products = await models.getAllFiltered(query);
|
||||
|
||||
if (products.length === 0) {
|
||||
return {
|
||||
status: STATUS_NOT_FOUND,
|
||||
message: MSG_NOT_FOUND,
|
||||
products,
|
||||
|
|
Loading…
Reference in a new issue