From 4ab023be486b75fde0f1543c5c9d0ee9f3ebd416 Mon Sep 17 00:00:00 2001 From: Michel Pereira Date: Mon, 27 Dec 2021 08:40:59 -0300 Subject: [PATCH] feat: Add 'getAllFiltered' in CONTROLLER and MODEL --- controllers/products.js | 4 ++-- models/products.js | 14 ++++++++++++-- services/products.js | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/controllers/products.js b/controllers/products.js index f1ff38a..e951761 100644 --- a/controllers/products.js +++ b/controllers/products.js @@ -27,7 +27,7 @@ router.get('/:id', async (req, res) => { const { _id, name, quantity, price, thumbnail, description } = result; const productData = { id: _id, name, quantity, price, thumbnail, description }; - res.status(200).json(productData); + res.status(OK).json(productData); }); router.post('/', rescue(async (req, res) => { @@ -81,7 +81,7 @@ router.delete('/:id', rescue(async (req, res) => { const { _id, name, quantity } = result; - res.status(200).json({ id: _id, name, quantity }); + res.status(OK).json({ id: _id, name, quantity }); })); module.exports = router; \ No newline at end of file diff --git a/models/products.js b/models/products.js index 5fdbdfe..cb559cf 100644 --- a/models/products.js +++ b/models/products.js @@ -18,11 +18,20 @@ const getByName = async (name) => connection() const getAll = async () => connection() .then(async (db) => { - const productS = await db.collection('products').find().toArray(); + const products = await db.collection('products').find().toArray(); - return productS; + return products; }); +const getAllFiltered = async (query) => { + const db = await connection(); + + const products = await db.collection('products') + .find({ name: { $regex: `/${query}/i` } }).toArray(); + + return products; +}; + const getById = async (id) => connection() .then(async (db) => { const product = await db.collection('products').findOne(new ObjectId(id)); @@ -62,4 +71,5 @@ module.exports = { update, updateQtd, deleteIt, + getAllFiltered, }; diff --git a/services/products.js b/services/products.js index 200abde..1e3745a 100644 --- a/services/products.js +++ b/services/products.js @@ -3,7 +3,10 @@ const validators = require('../utils/validators'); const STATUS_OK = 200; const STATUS_UNPROCESSABLE_ENTITY = 422; +const STATUS_NOT_FOUND = 404; const CODE_INVALID_DATA = 'invalid_data'; +const MSG_NOT_FOUND = 'No match found'; +const MSG_FOUND = 'Match found'; const MSG_WRONG_ID = 'Wrong id format'; const create = async (productData) => { @@ -46,6 +49,20 @@ const getAll = async () => { return { status: STATUS_OK, productS }; }; +const getAllFiltered = async (query) => { + const products = await models.getAll(query); + + if (!products) { + return { + status: STATUS_NOT_FOUND, + message: MSG_NOT_FOUND, + products, + }; + } + + return { status: STATUS_OK, message: MSG_FOUND, products }; +}; + const getById = async (id) => { // "id" validation: @@ -137,4 +154,5 @@ module.exports = { getById, update, deleteIt, + getAllFiltered, }; \ No newline at end of file