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] });
|
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) => {
|
router.get('/:id', async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ const getAllFiltered = async (query) => {
|
||||||
const db = await connection();
|
const db = await connection();
|
||||||
|
|
||||||
const products = await db.collection('products')
|
const products = await db.collection('products')
|
||||||
.find({ name: { $regex: `/${query}/i` } }).toArray();
|
.find({ name: { $regex: `${query}`, $options: 'i' } }).toArray();
|
||||||
|
|
||||||
return products;
|
return products;
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,9 +50,9 @@ const getAll = async () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAllFiltered = async (query) => {
|
const getAllFiltered = async (query) => {
|
||||||
const products = await models.getAll(query);
|
const products = await models.getAllFiltered(query);
|
||||||
|
|
||||||
if (!products) {
|
if (products.length === 0) {
|
||||||
return {
|
return {
|
||||||
status: STATUS_NOT_FOUND,
|
status: STATUS_NOT_FOUND,
|
||||||
message: MSG_NOT_FOUND,
|
message: MSG_NOT_FOUND,
|
||||||
|
|
Loading…
Reference in a new issue