Skip to content
Snippets Groups Projects
Commit edcaa76d authored by Matt Kirby's avatar Matt Kirby
Browse files

Implemented user search endpoint

parent 01e5685f
No related branches found
No related tags found
1 merge request!16Add additional GET endpoints to user service
......@@ -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 });
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment