diff --git a/backend-services/user-service/controllers/appController.js b/backend-services/user-service/controllers/appController.js index ddecf556e03e4e6c7f304bdce15629f49f7d7973..dbabe14c2e129f82a92ac05a91c49cc72ee46b44 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 }); + } +} +