From edcaa76d0e188f3c095bf1e5b7dfb3cec82fce47 Mon Sep 17 00:00:00 2001 From: Matt Kirby <MattJKirby@outlook.com> Date: Sun, 16 Apr 2023 15:34:01 +0100 Subject: [PATCH] Implemented user search endpoint --- .../user-service/controllers/appController.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend-services/user-service/controllers/appController.js b/backend-services/user-service/controllers/appController.js index ddecf556..dbabe14c 100644 --- a/backend-services/user-service/controllers/appController.js +++ b/backend-services/user-service/controllers/appController.js @@ -224,3 +224,26 @@ export const GetUserList = async (req,res) => { } } +/** + * GET /searchuser + * @param {*} req + * @param {*} res + * @returns + * + * query: {query: string} + */ + export const Search = async (req,res) => { + try { + const {query} = req.body; + + if(query === undefined || query.length === 0){ + throw new Error("Please provide a valid query!") + } + const usersWithMatchingId = await UserModel.find({"username": {"$regex": `^${query}`}}) + const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${query}`}}, {"lastName": {"$regex": `^${query}`}}]}) + return res.status(201).send({ usersById: usersWithMatchingId, usersByName: usersWithMatchingName}); + } catch(error){ + return res.status(401).send({ error: error.message }); + } +} + -- GitLab