From 2679d4c3de4ca7d92cabbf0d4f1ff527c21598df Mon Sep 17 00:00:00 2001 From: Mitanshi Kshatriya <42690596+MitanshiKshatriya@users.noreply.github.com> Date: Mon, 10 Jan 2022 19:37:45 +0530 Subject: [PATCH] Added support for search parameters (#55) (#57) * added support for search parameters * error message update --- src/controllers/certificate.js | 13 ++++++++++++- src/controllers/template.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/controllers/certificate.js b/src/controllers/certificate.js index 6be5a61..b467bd0 100644 --- a/src/controllers/certificate.js +++ b/src/controllers/certificate.js @@ -198,7 +198,18 @@ const getSingle = async (req, res) => { * @type {RequestHandler} */ const getAll = async (req, res) => { - const certificates = (await Certificate.find({}, { + if(req.query.tags){ + try{ + const formData = req.query.tags; + const decodedData = decodeURIComponent(formData); + const jsonObject = JSON.parse(decodedData); + req.query['tags'] = {'$in': jsonObject}; + }catch(e){ + return res.status(statusCode.BAD_REQUEST).send(`Failed to get certificates: Bad fromat ${e.message}`); + } + } + + const certificates = (await Certificate.find(req.query, { _id: 0, uid: 1 })).map(t => t.uid); diff --git a/src/controllers/template.js b/src/controllers/template.js index a257c39..cad7c66 100644 --- a/src/controllers/template.js +++ b/src/controllers/template.js @@ -207,7 +207,18 @@ const getSingle = async (req, res) => { * @type {RequestHandler} */ const getAll = async (req, res) => { - const templates = (await Template.find({}, { + if(req.query.tags){ + try{ + const formData = req.query.tags; + const decodedData = decodeURIComponent(formData); + const jsonObject = JSON.parse(decodedData); + req.query['tags'] = {'$in': jsonObject}; + }catch(e){ + return res.status(statusCode.BAD_REQUEST).send(`Failed to get templates: Bad fromat ${e.message}`); + } + } + + const templates = (await Template.find(req.query, { _id: 0, name: 1 })).map(t => t.name);